【发布时间】:2015-11-11 16:51:43
【问题描述】:
我正在用 PHP 创建一个简单的文件上传脚本来上传 XML 文件(它实际上是一个 WordPress 插件),但我怀疑我在将文件从临时文件夹传递到我指定的文件夹时遇到了问题。完整的脚本如下图,脚本实际运行成功,回显语句"The file ". basename( $_FILES["upload"]["name"]). " has been uploaded.";显示。但是,XML 文件永远不会出现在目标目录中。有趣的是,如果我尝试两次上传同一个文件,则返回的 if 语句表明该文件已经存在返回 true。抱歉,如果这是一个明显的问题,我对 PHP 很陌生。
$target_dir = "/user1/caravans/public_html/wordpress/wp-content/uploads/wpallimport/files";
(upload) from the form
$target_file = $target_dir . basename($_FILES["upload"]["name"]);
$success = 1;
$type = pathinfo($target_file,PATHINFO_EXTENSION);
if((!empty($_FILES["upload"])))
{
if (file_exists($target_file)) {
echo "Sorry, file already exists, please archive or remove existing xml files before uploading a new file.";
$success = 0;
}
if ($_FILES["upload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$success = 0;
}
if($type != "xml")
{
echo "Only XML file types are allowed";
$success = 0;
}
if ($success == 0)
{
echo "Error uploading.";
}
else
{
if (move_uploaded_file($_FILES["upload"]["tmp_name"], $target_file))
{
echo "The file ". basename( $_FILES["upload"]["name"]). " has been uploaded.";
}
else
{
echo "Sorry, there was an error uploading your file.";
}
}
}
else
{
echo "Please upload a file!";
}
【问题讨论】:
标签: php file-upload php-5.6