2012-05-19 95 views
2

我已經查看了這裏的其他五個類似的問題,他們都沒有能夠解決我的問題。我正在上傳一個1657字節的CSV文件。PHP文件上傳/ move_uploaded_file不工作,不像其他問題

下面是我的php.ini文件

upload_max_filesize = 2M

post_max_size = 8M

$file = $_FILES['csv']; 

if (!isset($file)) { 
    halt(HTTP_NOT_FOUND, 'This page could not be found on the web server'); 
} 

if (substr(strrchr($file['name'], '.'), 1) != 'csv') { 
    halt(500, 'Sorry but please upload a CSV file next time!'); 
} 

if ($file['error'] != '0') { 
    die('An error occurred during upload.<br />Error: ' . $file['error']); 
} 

$file_loc = '/home/x/tmp/up/' . basename($file['tmp_name']) . '.csv'; 

if (file_exists('/home/x/tmp/up/')) { 
    echo 'the directory exists....<br/>'; 
} 

if (file_exists($file['tmp_name'])) { 
    echo 'tmp file exists<br/>'; 
    echo filesize($file['tmp_name']) . 'b<br/>'; 
} 

if (move_uploaded_file($file['tmp_name'], $file_loc)) { 
    echo 'Uploaded to: ' . $file_loc; 
} else { 
    echo 'something went wrong!' . print_r($_FILES['csv'], true); 
    echo '<br />Upload Max Size: ' . ini_get('upload_max_filesize'); 
    echo '<br />POST Max Size: ' . ini_get('post_max_size'); 

} 

這裏有些價值觀是當我上傳的文件生成的輸出:

the directory exists.... 
tmp file exists 
1657b 
something went wrong!Array ([name] => hm.csv [type] => application/vnd.ms-excel [tmp_name] => /tmp/php72p1VP [error] => 0 [size] => 1657) 
Upload Max Size: 2M 
POST Max Size: 8M 

我已經在「com」下看過PHP.net星期一陷阱「文章沒有導致任何東西被固定。任何人都可以看到有什麼不對或給我一些提示?

+2

檢查目錄的權限。具體而言,請檢查Web服務器是否具有目錄的寫入權限。 – Corbin

+0

您的PHP用戶是否有正確的文件權限來訪問您要將文件寫入的目錄? – Gareth

+2

PHP爲$ _FILES數組中的'tmp_name'參數創建一個隨機值。您應該**不要**將其作爲永久值用於最終存儲此文件的任何位置。並且不要使用字符串操作來解析文件名,也不要相信用戶文件名是準確的。一個.CSV文件可以被稱爲.txt,並且仍然是完全有效的。同樣,敵對用戶可以將'nastygarbage.exe'重命名爲'gooddata.csv',並通過您的「安全」系統。 –

回答

0

與託管服務提供商高科技多重交流後,他們設法給文件夾正確的權限(我試過0777之前但它沒有工作)。我相信一些巫術在幕後進行,但這是No errors in the error_log; I have set 777 permissions on /home/x/up,劇本開始工作。

1

以下提示可能會有幫助。

1. The folder exists, check for spellings 
2. Check the properties of the folder and make sure the permissions have read+write 0666 
3. Make sure the file is within your public html root, otherwise double check the owner of the file, and make sure PHP Has read/write access to it. 
4. Look for the logs i.e. Unable to move '/tmp/php6wlOg1' to 'upload/110216104651_00134_smooth_1440x900.jpg' 
5. If the size of post data is greater than post_max_size, the $_POST and $_FILES superglobals are empty. So set it according to your need. 

還要設置這個,看看是否有任何錯誤

error_reporting(E_ALL); // or E_STRICT 
ini_set("display_errors",1); 
ini_set("memory_limit","1024M"); 
+0

感謝您的提示,不幸的是,其中大部分已在評論中提到過...... – kstev