Ryan Claxton Ryan Claxton - 5 months ago 10
HTML Question

Form image submit not detected

Can someone please spot what I am doing wrong? I copied this over from a different page on my site where it works but I can't get it to work on this one. There are text inputs also that I have left out that are working as expected. Thanks.

<?php
if (isset($_POST["save"])) {

if (isset($_POST['pic'])){
//isset never detected when image is uploaded in html
}

}


<html>
<form class="form-horizontal" onsubmit="return checkSize(2097152)" enctype="multipart/form-data" data-toggle="validator" method="POST" action="profile.php">
<input id="pic" name="pic" type="file" accept="image/*" class="text-center center-block well well-sm">

<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<button class="btn btn-primary" name="save" id="save">Save Changes</button>
<a href="profile.php" class="btn btn-default" role="button">Cancel</a>
</div>
</div>

</form>

Answer

To accept a file from a HTML form you need to use the $_FILES instead of the $_POST. For example you can use $_FILES["pic"]["name"] to get the file name.

From there you can move the picture to a new location like this:
move_uploaded_file($_FILES["pic"]["tmp_name"], "/var/tmp/pic.jpg")

More info is available at http://www.w3schools.com/php/php_file_upload.asp

Comments