HTML 5 Camera access and file upload using php

I am using below code to access html 5 camera and upload 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. When I test the code showing "File upload error, please try again!" Can anyone help me figure out where the problem is?

Below code works correctly 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 the same as above.

+3


source to share


2 answers


In the first case your input file name is called "image", while in the second case "uploadedfile". Your PHP expects "uploadedfile".

To fix this problem, 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>

      

+7


source


If you want to use HTML5 and device camera, there is HTML5 function for that: Example: <input type="file" accept="image/*" capture="camera" />

from here: https://coderwall.com/p/epwmoa



It works on chrome for Android (with Samsung Galaxy S2).

+3


source







All Articles