Search code examples
phphtmlfile-uploadcamera

HTML 5 Camera access & upload file using php


I am using below code for html 5 camera access and uploading the image to server.

HTML Code

<form action="upload.php" method="post" enctype="multipart/form-data">
  <input type="file" name="image" accept="image/*" capture>
  <input type="submit" value="Upload">
</form>

upload.php code

<?php 
$target_path = "upload/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
?>

Problem is When I am testing the code it's showing "There was an error uploading the file, please try again!" . Can anyone help me to figure it out where the problem ?

Below code is working properly for me.

HTML Code :

<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

PHP code is same as above.


Solution

  • IN the first case your file input name is named 'image' when on the second case is named 'uploadedfile'. Your PHP is expecting 'uploadedfile'.

    To solve this you need to change your code (first case) to:

    <form action="upload.php" method="post" enctype="multipart/form-data">  
       <input type="file" name="uploadedfile" accept="image/*" capture>  
       <input type="submit" value="Upload">  
    </form>