简体   繁体   English

从自定义表单HTML上传文件

[英]Uploading File from Custom Form HTML

I'm trying to upload a file from my custom HTML form to a PHP script. 我正在尝试将文件从我的自定义HTML表单上传到PHP脚本。 Here is the code that I have in my HTML. 这是我的HTML中的代码。

<form action="test.php" method="post" enctype="multipart/form-data">
    <input id="txt" class="button" type = "text" value = "Choose File" onclick       ="javascript:document.getElementById('file').click();">
    <input id = "file" type="file" style='visibility: hidden;' name="img"    onchange="ChangeText(this, 'txt');"/>
   <input type="submit">
</form>

Here is the test.php file: 这是test.php文件:

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

$target = "i/";
if (move_uploaded_file($_FILES['img']['tmp_name'], $target)) {
     echo "The file " . basename($_FILES['img']['name']) . "has been uploaded";
} 

Here's the error on the page: 这是页面上的错误:

Warning: move_uploaded_file(): The second argument to copy() function cannot be a directory in /var/www/BLOCKED/test.php on line 7 警告:move_uploaded_file():copy()函数的第二个参数不能是第7行的/var/www/BLOCKED/test.php中的目录

Warning: move_uploaded_file(): Unable to move '/tmp/phpNxxB72' to 'i/' in /var/www/BLOCKED/test.php on line 7. Yes it's permissions of the directory are 777. 警告:move_uploaded_file():在第7行的/var/www/BLOCKED/test.php中无法将'/ tmp / phpNxxB72'移至'i /'。是的,目录的权限为777。

move_uploaded_file的第二个参数应该是文件名(带有路径)而不是目录

$target = "i/".basename($_FILES['img']['name']);

The error is correct. 错误是正确的。 The 2nd parameter to move_uploaded_file() is not a directory, it should be a file. move_uploaded_file()的第二个参数不是目录,它应该是文件。 You just need to append the file's name to $target in the 2nd parameter. 您只需要在第二个参数中将文件名附加到$target For example: 例如:

if (move_uploaded_file($_FILES['img']['tmp_name'], $target.$_FILES['img']['name'])) {
    echo "The file " . basename($_FILES['img']['name']) . "has been uploaded";
}

move_uploaded_file function should be like this: move_uploaded_file函数应如下所示:

move_uploaded_file($_FILES['img']['tmp_name'], $target. $_FILES["img"]["name"])

add file name at end, as $target is a directory/folder only. 在文件末尾添加文件名,因为$ target仅是目录/文件夹。

Tantibus, as you can read here: Tantibus,您可以在这里阅读:

http://php.net/manual/en/function.move-uploaded-file.php http://php.net/manual/zh/function.move-uploaded-file.php

You must specify the file name to your target file. 您必须为目标文件指定文件名。

$target = "i/".basename($_FILES['img']['name']);

您可能错过了这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM