2017-10-20 12 views
-1

編集記録フォームを送信するときにアップロード用にファイルが選択されているかどうかを確認したいとします。現時点では、何もしない代わりに空のレコードで現在のファイルパスを上書きしているので、動作しません。私はこれに関連する他の質問を検索しようとしましたが、答えのどれも私を助けません。アップロード用にファイルが選択されているかどうかを確認する

if($_FILES['healthandsafetyupload']['name'] != "") { 


//H&S Policy File Upload 
    if (count($_FILES['healthandsafetyupload']['name']) > 0) { 
     $hsInt = count($_FILES['healthandsafetyupload']['name']); 

    //Loop through each file 
    for ($i = 0; $i < count($_FILES['healthandsafetyupload']['name']); $i++) { 
     //Get the temp file path 
     $tmpFilePath = $_FILES['healthandsafetyupload']['tmp_name'][$i]; 

     //Make sure we have a filepath 
     if ($tmpFilePath != "") { 

      //save the filename 
      $shortname = $_FILES['healthandsafetyupload']['name'][$i]; 
      $fileExt = get_extension($shortname); 


      //save the url and the file 
      $hasfilePath = "uploads/has_" . random_string(14) . "." . $fileExt; 

      //Upload the file into the temp dir 
      move_uploaded_file($tmpFilePath, $hasfilePath); 

      } 
     } 
    } 
} 

$sql1 = "UPDATE `subcontractor_qs` SET 
healthandsafetyupload = '" . mysql_real_escape_string($hasfilePath) . "' WHERE subcon_id = $subcon_id"; 
$query1 = mysql_query($sql1) or die(mysql_error()); 
+2

の可能性のある重複した[ユーザーがアップロードするファイルを選択したかどうかをテストする方法?](https://stackoverflow.com/questions/2958167/how-to-test-if-a-user-has -selected-a-file-to-upload) – Thielicious

+0

これらの回答は私の問題を解決するものではありません。 – Ross

+0

あなたのIF句は大丈夫です。それはあなたの更新クエリだけです。ファイルが選択されていなくてもトリガされます。 –

答えて

0

これは私のためにそれを解決しました。

if ($hasfilePath != "") { 
    $sql1 = "UPDATE `subcontractor_qs` SET 
    healthandsafetyupload = '" . mysql_real_escape_string($hasfilePath) . "' WHERE subcon_id = $subcon_id"; 
    $query1 = mysql_query($sql1) or die(mysql_error()); 
} 
0
if($_FILES['image']['size']==0 && $_FILES['image']['error']==0){ 
    //Your code 
} 
+0

ありがとうございますが、動作しません。 – Ross

関連する問題