2012-05-25 84 views
0

將PDF文件保存到服務器上的文件夾時出現問題。代碼一次工作,現在不行。我想要做的是在提交表單時檢查是否有人嘗試上載PDF,如果文件字段中有PDF,則上傳它,然後將路徑保存到mysql數據庫。代碼如下:使用PHP保存PDF文件

if (!empty($_FILES['pdf'])){ 
      $idir = "../files/PDF/"; //my directory file is supposed to be saved in 
      $randomd=rand(0000000,9999999); //creates a random number as filename 
      $domain = "http://".$_SERVER['HTTP_HOST']; 
      $file_ext = strrchr($_FILES['pdf']['name'], '.'); grabs file extension. my code checked if the file was a pdf a different way and neither seems to work. 
      $destination=$randomd.$file_ext; //new filename 
      if ($file_ext=='pdf') { 
       move_uploaded_file($_FILES['pdf']['tmp_name'], "$idir" . $destination); 
       $pdf= $domain."/files/PDF/".$destination;            } else { echo("File type not supported."); 
mysql_query("UPDATE tbl_listings SET pdf='$pdf' WHERE listing_id='$lid'"); 
                      } 

的如果不爲空不工作,它總是試圖上傳一個文件,但是當我檢查的文件夾沒有在那裏,它不更新了MySQL。

+1

'move_uploaded_file()'返回什麼? – yuvin

+0

@yuvin沒什麼。 – user1385895

+0

該函數返回TRUE或FALSE(在某些情況下,它會發出警告)。請參考[documentation](http://php.net/manual/en/function.move-uploaded-file.php)。我認爲原因可能是編寫權限或文件大小。也嘗試使用絕對目標路徑。 – yuvin

回答

1

$_FILES['pdf']永遠不會是空的(當表單被提交時),無論文件是否被選中,它都會返回一個數組。

檢查$_FILES['pdf']['error'],當沒有文件上傳時,將會是4

+0

它返回數字0。 – user1385895