我在 WordPress 中运行了这个自定义上传脚本,因为我不想/不需要使用 WordPress 脚本中的构建。
如果我上传一个 csv 文件错误:The file test.csv could not be copied, try again
无法弄清楚为什么会弹出这个错误!
这是脚本。
<?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';
}
}
}
}
?>
<div style="display: inline-block; width: 25%; vertical-align: top;">
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="field_name" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
更新
以下是上传尝试后创建的一些警告
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
让我们了解为什么会发生这种情况。您收到错误The file test.csv could not be copied, try again
,因此以下行是问题所在:
if (!move_uploaded_file ($_FILES['field_name']['tmp_name'], $updir. $fname))
根据 的文档move_uploaded_file()
,以下是错误的原因:
如果 filename 不是有效的上传文件,则不会发生任何操作,并且 move_uploaded_file() 将返回 FALSE。
如果 filename 是有效的上传文件,但由于某种原因无法移动,则不会发生任何操作,并且 move_uploaded_file() 将返回 FALSE。此外,还会发出警告。
确保已启用警告,以帮助您进一步诊断问题(将其添加到脚本顶部):
ini_set('display_errors', 1);
error_reporting(E_ALL);
最常见的问题是您的文件目录不可写,因为 PHP 无法在目标目录中创建文件名。确保该目录存在,并且 PHP 进程可以写入该文件夹。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句