使用 php 的 HTML 5 相机访问和上传文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14920737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:13:00  来源:igfitidea点击:

HTML 5 Camera access & upload file using php

phphtmlfile-uploadcamera

提问by Susim Samanta

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

我正在使用以下代码进行 html 5 相机访问并将图像上传到服务器。

HTML Code

HTML代码

<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代码

<?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 :

HTML代码:

<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.

PHP 代码与上面相同。

回答by Thanasis Pap

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

在第一种情况下,您的文件输入名称被命名为“image”,而在第二种情况下,您的文件输入名称被命名为“uploadedfile”。您的 PHP 正在等待 '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>

回答by paaacman

If you want to use HTML5 and the camera of the device, there is an HTML5 feature for this: Exemple:

如果你想使用 HTML5 和设备的摄像头,有一个 HTML5 功能:例如:

<input type="file" accept="image/*" capture="camera" />

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

从这里:https: //coderwall.com/p/epwmoa

It's working on chrome for Android (with a Samsung Galaxy S2).

它正在开发适用于 Android 的 chrome(使用三星 Galaxy S2)。