csv file won't upload through php uploader
我在WordPress中运行了这个自定义上传脚本,因为我不想/不需要使用内置的WordPress脚本。如果我上传一个csv文件,错误是:
这是剧本。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <?php $updir = 'wp-content/themes/ddpp/upload_csv/'; $max_size = 100; $allowtype = array('csv'); if (isset($_FILES['field_name'])) { if ($_FILES['field_name']['error'] > 0) { echo 'Error: '. $_FILES['field_name']['error']. '<br />'; } else { // get the name, size (in kb) and type (the extension) of the file $fname = $_FILES['field_name']['name']; $fsize = $_FILES['field_name']['size'] / 1024; $ftype = end(explode('.', strtolower($fname))); // checks if the file already exists if (file_exists($updir. $fname)) { echo 'The file: '. $fname. ' already exists'; } else { // if the file not exists, check its type (by extension) and size if (in_array($ftype, $allowtype)) { // check the size if ($fsize <= $max_size) { // uses function to copy the file from temporary folder to $updir if (!move_uploaded_file ($_FILES['field_name']['tmp_name'], $updir. $fname)) { echo 'The file '. $fname. ' could not be copied, try again'; } else { echo $fname. ' ('. $fsize. ' kb) was successfully uploaded'; } } else { echo 'The file '. $fname. ' exceeds the maximum permitted size, '. $max_size. ' KB'; } } else { echo $fname. ' - invalid file type'; } } } } ?> <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="field_name" /><br /> <input type="submit" name="submit" value="Submit" /> </form> |
更新以下是在尝试上载后创建的一些警告
1 2 3 4 5 | Strict Standards: Only variables should be passed by reference in /Applications/MAMP/htdocs/ddpp/wp-content/plugins/ddpp_csv_upload/index.php on line 182 Warning: move_uploaded_file(wp-content/themes/ddpp/upload_csv/test.csv): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/ddpp/wp-content/plugins/ddpp_csv_upload/index.php on line 194 Warning: move_uploaded_file(): Unable to move '/Applications/MAMP/tmp/php/phpvT6vFM' to 'wp-content/themes/ddpp/upload_csv/test.csv' in /Applications/MAMP/htdocs/ddpp/wp-content/plugins/ddpp_csv_upload/index.php on line 194 |
让我们理解为什么会发生这种情况。您得到的错误是
1 |
根据
If filename is not a valid upload file, then no action will occur, and move_uploaded_file() will return FALSE.
If filename is a valid upload file, but cannot be moved for some reason, no action will occur, and move_uploaded_file() will return FALSE. Additionally, a warning will be issued.
确保启用了警告,以帮助您进一步诊断问题(将此添加到脚本顶部):
1 2 |
最常见的问题是文件目录不可写,因为PHP无法在目标目录中创建文件名。确保目录存在,并且PHP进程可以写入该文件夹。
这里的代码有问题
1 2 3 | if (!move_uploaded_file ($_FILES['field_name']['tmp_name'], $updir. $fname)) { echo 'The file '. $fname. ' could not be copied, try again'; } |
这意味着move_uploaded_文件无法完成您需要的操作。检查您的PHP错误日志,可能会出现关于权限的错误。您可能已经使用ftp或其他方法创建了目录,因此Apache无法写入该目录。
为了给Apache提供访问权限,您有两个选项。将Apache添加到允许写入的组中,或者简单地将Apache设置为所有者。我总是发现,如果它是一个网站的目录,那么让ApacheOwnser无害。如果您有ssh访问权限,请运行以下命令。