2015-11-11 97 views
2

我在PHP中創建一個簡單的文件上傳腳本來上傳XML文件(它實際上是一個WordPress插件),我懷疑我有問題將文件從臨時文件夾傳遞給我指定的但是。完整的腳本如下所示,該腳本實際上運行併成功,echo語句"The file ". basename($_FILES["upload"]["name"]). " has been uploaded.";顯示器。但是XML文件從不出現在目標目錄中。有趣的是,如果我嘗試兩次上傳相同的文件,那麼if語句返回哪個指示文件已經存在返回true。如果這是一個明顯的問題,我很抱歉,我對PHP很陌生。PHP:文件上傳腳本和move_upload_file

$target_dir = "/user1/caravans/public_html/wordpress/wp-content/uploads/wpallimport/files"; 
(upload) from the form 
$target_file = $target_dir . basename($_FILES["upload"]["name"]); 
$success = 1; 
$type = pathinfo($target_file,PATHINFO_EXTENSION); 

if((!empty($_FILES["upload"]))) 
{ 
    if (file_exists($target_file)) { 
     echo "Sorry, file already exists, please archive or remove existing xml files before uploading a new file."; 
     $success = 0; 
    } 
     if ($_FILES["upload"]["size"] > 500000) { 
      echo "Sorry, your file is too large."; 
      $success = 0; 
     } 
      if($type != "xml") 
      { 
       echo "Only XML file types are allowed"; 
       $success = 0; 
      } 
       if ($success == 0) 
       { 
        echo "Error uploading."; 
       } 
        else 
        { 
         if (move_uploaded_file($_FILES["upload"]["tmp_name"], $target_file)) 
         { 
          echo "The file ". basename($_FILES["upload"]["name"]). " has been uploaded."; 
         } 
          else 
          { 
           echo "Sorry, there was an error uploading your file."; 
          }  
        } 
    } 
    else 
    { 
    echo "Please upload a file!"; 
    } 

回答

0

問題出現在$ target_dir路徑中。根目錄路徑錯誤。使用$ _SERVER ['DOCUMENT_ROOT']。

改變這一行

$target_dir = "/user1/caravans/public_html/wordpress/wp-content/uploads/wpallimport/files"; 

$target_dir = $_SERVER['DOCUMENT_ROOT']."/wordpress/wp-content/uploads/wpallimport/files"; 

這樣,所有文件將被上傳到 「文件」 目錄。

現在文件被上傳到錯誤的目錄中。所以爲什麼你不能在文件目錄中看到它們。

在Linux服務器(Ubuntu,CentOS)中,用戶目錄不是根目錄。它存在於「主目錄」下。但是,根據LINUX的風格和版本,它可能會有所不同。

echo $_SERVER['DOCUMENT_ROOT']; 

這將幫助您找到您的root directoy。