我被一個討厭的bug擊中。 我有一個從jquery/ajax提交圖像到php腳本的表單系統。 此腳本任務是取出臨時文件並用一個永久名稱來移動它。上傳圖片:臨時文件/名稱未創建
主要的問題是,它適用於大多數圖像,除了沒有創建臨時文件的單個圖像。我還可以看到該文件數組中該文件與1
這裏值的「錯誤」的關鍵是PHP代碼
public function uploadImageInTempDir(): array
{
$imagePath = [];
$slugify = new Slugify();
$currentYear = date('Y');
$currentMonth = date('m');
# Save currently uploaded file as a concrete file, instead of a temporary file.
# We iterate to access the file in question because it is the only way to access files in $_FILES .
# But there always is only 1 file in the array as sent from the frontend.
foreach ($this->files as $file) {
if (! empty($file[ 'name' ])) {
$imageName = $slugify->slugify($file[ 'name' ]);
$randomString = time();
$imageType = Utilities::getFileExtension($file[ 'name' ]);
$finalImageName = $imageName . '-' . $randomString . '.' . $imageType;
$saveTargetTmp = Settings::SAVE_IMAGE_TEMP_PATH . $finalImageName;
move_uploaded_file($file[ 'tmp_name' ], $saveTargetTmp);
# We add year/month to filename to store each file in corresponding year/month directory structure.
$imagePath[ "path" ] = $currentYear . $currentMonth . '/' . $finalImageName;
}
}
# We pass back to the frontend the final path of the image which will be in the form of:
# /year/month/filename-random.ext . Note that currently image is NOT in its final
# destination. It will have to be moved there when user finally posts the full form.
return $imagePath;
}
我的第一個問題是:你知不知道如何解釋錯誤顯示1的文件數組中的代碼?它不會拋出任何異常或錯誤,我可以捕捉全球:
二,你看到爲什麼會發生這種錯誤?
事實:
它適用於所有類型的具有類似名稱的圖像。
它不是一個大小問題
- 腳本接收信息,你可以看到它形成了文件名的圖像。
這裏是前端代碼,它會報告成功的上傳:
$.ajax({
url : '/blog/upload-image',
type : form.attr('method'),
contentType: false, // obligatoire pour de l'upload
processData: false, // obligatoire pour de l'upload
dataType : 'json', // selon le retour attendu
data : data
}).done(function(response) {
console.log(`Upload image success: ${idOfUploadedImage}`)
console.log(response)
// front end tasks
}).fail(function(response) {
console.log("Upload image error: ", response);
}).always(function() {
console.log("Upload image complete");
});
http://php.net/manual/en/features.file-upload.errors.php – tkausl
請回答這個問題,我會選擇你。我本可以發誓這不是一個尺寸問題,但它是。 –