我在PHP中創建一個簡單的文件上傳腳本來上傳XML文件(它實際上是一個WordPress插件),我懷疑我有問題將文件從臨時文件夾傳遞給我指定的但是。完整的腳本如下所示,該腳本實際上運行併成功,echo語句"The file ". basename($_FILES["upload"]["name"]). " has been uploaded."
;顯示器。但是XML文件從不出現在目標目錄中。有趣的是,如果我嘗試兩次上傳相同的文件,那麼if語句返回哪個指示文件已經存在返回true。如果這是一個明顯的問題,我很抱歉,我對PHP很陌生。PHP:文件上傳腳本和move_upload_file
$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!";
}