好吧,非常簡單的上傳腳本從w3schools,我試圖實現,但我不斷收到錯誤消息。move_uploaded_file()拋出錯誤
對於我使用WordPress和創建自定義上傳方法,所以我不能使用自定義字段等需要參考..
HTML節中有關此:
<form method="post" id="form" enctype="multipart/form-data">
<input type="file" name="frontImage" id="frontImage">
的事情PHP端:
$target_dir = home_url() . "/wp-content/uploads/survey/";
$target_file = $target_dir . basename($_FILES["frontImage"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
$check = getimagesize($_FILES["frontImage"]["tmp_name"]);
if($check !== false) {
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["frontImage"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif") {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["frontImage"]["tmp_name"], $target_file)) {
echo "The file ". basename($_FILES["frontImage"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
好的小號Ø我得到的錯誤是:
"Sorry, there was an error uploading your file."
這意味着,move_uploaded_file()以部分失敗。
如果我的print_r($ _ FILES)我得到:
Array
(
[frontImage] => Array
(
[name] => Everest_kalapatthar_crop.jpg
[type] => image/jpeg
[tmp_name] => /Applications/MAMP/tmp/php/phpHw9BcP
[error] => 0
[size] => 93053
)
)
是否有任何理由,這是不工作?
什麼是* $ TARGET_DIR *?它是你的目錄的正確路徑嗎?如果是,請確保文件夾權限設置爲* 0777 * –
輸出'$ target_dir'確保它是有效的。 –
@ EM-Creations target_dir和target_file都是正確的路徑 –