2017-10-20 48 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

相關問題