2017-03-08 3 views
0

私は厄介なバグで殴られました。 jquery/ajaxからphpスクリプトに画像を送信するフォームシステムがあります。 このスクリプトタスクでは、一時ファイルを取り出し、固定名で移動します。アップロード中の画像:一時ファイル/名前が作成されていません

主な問題は、一時ファイルが作成されていない1つの画像を除き、ほとんどの画像で機能することです。あなたはエラーを解釈する方法を知っていますか:私も、そのファイルのファイル配列。ここ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を示すファイル配列のコード?例外やエラーは発生しません。 enter image description here

次に、このエラーが発生する理由はありますか?

事実:

  • それは似た名前を持つ画像のすべての種類で動作します。

  • それはあなたが、それはファイル名を持って画像を形成見ることができるよう

  • スクリプトは、情報を受け取るサイズの問題ではありません。ここで

は、フロントエンドのコードで、それが成功し、アップロードを報告します。

$.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"); 
      }); 
+2

http://php.net/manual/en/features.file-upload.errors.php – tkausl

+0

この質問で回答してください。私はあなたを選ぶでしょう。私は誓ったかもしれないそれはサイズの問題ではなかったが、それはそうであった。 –

答えて

0

エラーがhere列挙されている、アップロード、1は、次のエラー値:

UPLOAD_ERR_INI_SIZE

Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.

だからあなたのイメージファイルphp.iniで設定された最大値よりも大きいです。

+0

ありがとうございます。とにかくこのバブルを例外/エラーハンドラで捕まえることができますか? –

+1

PHP自体ではなく、スクリプトが呼び出されると、アップロードは既に終了しており、その結果はわかっています。何か問題があった場合は、エラーを自分で確認し、例外を投げる必要があります。 – tkausl

関連する問題