首页 > 解决方案 > 自动上传文件

问题描述

我创建了一个带有 html 和 javascript 的网站,可以访问您的网络摄像头,当用户按下拍照按钮时,一张照片就会被捕获。我将如何直接将其上传到存储它的某个云文件夹?

谢谢

标签: javascripthtml

解决方案


在按钮所在的页面上,:

  1. 创建拍摄照片的按钮(我不知道这是如何完成的)

<button>Snap photo!</button>

  1. 创建一个具有任何值的表单(不可见的表单)......任何东西都可以是值,只要确保它不为空
  2. 使用javascript,以便在单击按钮时将表单提交到php页面
  3. 将表单的目标设置为您创建的 iframe,这样当他们拍摄照片时,它会指向同一页面,而无需重新加载页面] 这是我刚才所说的代码:




    折断

    </form>

<script>

函数 rad(){ document.getElementById("废话").submit();

</div>
  1. 使用以下代码创建文件处理程序:

    $errors = []; // Store all foreseen and unforseen errors here $fileExtensions = ['jpeg','jpg','png']; // Get all the file extensions $fileName = $_FILES['myfile']['name']; $fileSize = $_FILES['myfile']['size']; $fileTmpName = $_FILES['myfile']['tmp_name']; $fileType = $_FILES['myfile']['type']; $fileExtension = strtolower(end(explode('.',$fileName))); $uploadPath = $currentDir . $uploadDirectory . basename($fileName); if (isset($_POST['submit'])) { if (! in_array($fileExtension,$fileExtensions)) { $errors[] = "This file extension is not allowed. Please upload a JPEG or PNG file"; } if ($fileSize > 1000000000) { $errors[] = "This file is more than 1GB. Sorry, it has to be less than or equal to 1GB"; } if (empty($errors)) { $didUpload = move_uploaded_file($fileTmpName, $uploadPath); if ($didUpload) { echo basename($fileName) ; } else { echo "An error occurred somewhere. Try again or contact the admin"; } } else { foreach ($errors as $error) { echo $error . "These are the errors" . "\n"; } } } ?>

唯一的问题是,这会将图像上传到您的服务器而不是“云文件夹”......但它是一个文件夹,只需将云文件夹放在您的网络服务器中,然后在$uploadDirectory = "/uploads/";ie 中指定云文件夹的名称,将“上传”更改为您的云文件夹的名称....

希望这会有所帮助


推荐阅读