2012-08-17 32 views
1

我真的不知道太多的PHP。但無論如何,我無法使用以下php上傳.csv文件。我已經解決了與upload_max大小相關的屬性的問題。在我的本地工作正常,但不在沙箱上。它是文件類型問題嗎?在Apache上的錯誤

我只能得到它打印這樣的東西......這根本沒有幫助。 「 」 - 要加載的CSV文件:> > > NOT a file valid:1「

我應該在哪裏修復?如何打印完整的PHP錯誤消息,而不是隻顯示值「1」,我不確定它對應的是什麼?即...輸出更有用的打印像這樣「UPLOAD_ERR_INI_SIZE:1」?

<?php 
// using upload at click from http://code.google.com/p/upload-at-click/ 
// FileData is the name for the input file 

$file_result = ""; 
$file = $_FILES['Filedata']; 

$allowedExtensions = array("csv", "txt"); 
$arrayVar = explode(".", $file["name"]); 
$extension = end($arrayVar); 

//commented out for 「Only variables should be passed by reference」 error 
//$extension = end(explode(".", $file["name"])); 


function isAllowedExtension($fileName) { 
    global $allowedExtensions; 
    return in_array(end(explode(".", $fileName)), $allowedExtensions); 
} 

if($file["error"] > 0){ 
    echo "failure to upload the file >>> ". "Error code: ".$file["error"]."<br>"; 
}else{ 
    //echo " >>> CURRENT DIR: ".getcwd() . "\n"; 
    $workDir = getcwd(); 

    $dir = substr($workDir, 0, -10); 
    $path = $file["name"]; 
    $newFileLoc = $dir.$path; 

    $file_result.= 
    "<br>  Upload: " . $file["name"] . "<br>" . 
    "  Type: " . $file["type"] . "<br>" . 
    "  Size: " . $file["size"] . "<br>" . 
    "  file uploaded to: ".$newFileLoc."<br>"; 

    // txt - text/plain 
    // rtf - application/msword 
    // dat/obj - application/octet-stream 
    // csv - application/vnd.ms-excel 
    // maximum 200 MB file - 200,000,000 k 

    if ( ($file["type"] == "application/vnd.ms-excel" || $file["type"] == "text/plain") 
      && isAllowedExtension($file["name"]) 
      && ($file["size"] < 200000000) 
     ) 
     { 
      move_uploaded_file($file["tmp_name"], $newFileLoc); 
      //echo $file_result.=" >>> File uploaded successfull!!"; 
      echo "|".$path;//"filePath : " . $newFileLoc; 

     }else{ 
      echo " >>> NOT a file valid: ". isAllowedExtension($file["name"]); 
     }  
} 
?> 

這是按照其他用戶建議的方式添加的線路,以正確捕捉錯誤。請讓我知道如果這是正確的對不起,我一點都不瞭解PHP。不管怎麼說,印刷錯誤只是 「 - CSV文件加載:> > >了無效的文件:1」

<?php 
// using upload at click from http://code.google.com/p/upload-at-click/ 
// FileData is the name for the input file 

ini_set('display_errors', 1); error_reporting(E_ALL); 

$file_result = ""; 
$file = $_FILES['Filedata']; 

$allowedExtensions = array("csv", "txt"); 
$arrayVar = explode(".", $file["name"]); 
$extension = end($arrayVar); 

回答

1

所考慮的模塊如下:

if(($file["type"] == "application/vnd.ms-excel" || $file["type"] == "text/plain") && 
    isAllowedExtension($file["name"]) && 
    ($file["size"] < 200000000)) 
{ 
    move_uploaded_file($file["tmp_name"], $newFileLoc); 

    //echo $file_result.=" >>> File uploaded successfull!!"; 

    echo "|".$path;//"filePath : " . $newFileLoc; 
} 
else 
{ 
    echo " >>> NOT a file valid: ". isAllowedExtension($file["name"]); 
} 

請注意,您'再次擊中你的其他人echo,'1'是因爲isAllowedExtension()返回一個布爾值,當你打印它時,它是true,表示爲'1'(而不是'0')。

您在if的條件之一就是失敗。我會把它們分開一點,看看具體是哪一個。

編輯例如:

if(($file["type"] == "application/vnd.ms-excel" || $file["type"] == "text/plain")) 
{ 
    if(isAllowedExtension($file["name"])) 
    { 
     if($file["size"] < 200000000) 
     { 
      move_uploaded_file($file["tmp_name"], $newFileLoc); 

      echo "|".$path;//"filePath : " . $newFileLoc; 
     } 
     else 
     { 
      echo "Invalid file size: " . $file["size"] . "\n"; 
     } 
    } 
    else 
    { 
     echo "Invalid extension: " . $file["name"] . "\n"; 
    }  
} 
else 
{ 
    echo "Invalid type: " . $file["type"] . "\n"; 
} 

或者,你可以print_r()$file,看看它的值。

+0

很好,非常感謝您的更新。我根據你的建議更新了php。這種方式更加清晰。但現在得到一個新的錯誤「 - 加載的CSV文件:無效的類型:application/octet-stream」 – user1518600 2012-08-17 17:52:58