2011-11-07 22 views
0

我想在本地服務器上使用php上傳文件,並在運行腳本(這是一個非常簡單的腳本)後,應該回顯信息的頁面只是空白。有人能幫我弄清楚問題是什麼嗎?我嘗試閱讀許多不同的帖子,但我似乎無法找到解決方案。 感謝php文件上傳不起作用 - 空白頁

HTML

<form enctype="multipart/form-data" action="upload.php" method="POST"> 
<input type="hidden" name="MAX_FILE_SIZE" value="100000" /> 
Choose a file to upload: <input name="uploadedfile" type="file" /><br /> 
<input type="submit" value="Upload File" /> 
</form> 

PHP

<?php 
// Where the file is going to be placed 
$target_path = "tmp/"; 
$target_path = $target_path . basename($_FILES['uploadedfile']['name']); 
$target_path = "tmp/"; 
$target_path = $target_path . basename($_FILES['uploadedfile']['name']); 
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
    echo "The file ". basename($_FILES['uploadedfile']['name']). 
    " has been uploaded"; 
} else{ 
    echo "There was an error uploading the file, please try again!"; 
} 
?> 

我沒有收到來自這樣的東西。只是一個空白頁面......當我檢查tmp DIR時,沒有任何內容......有任何建議?我正在使用XAMP

編輯:

原來是兩部分。 #1 DIR權限和#2我必須通過服務器的DNS瀏覽到我的xampp目錄。這很奇怪,因爲我通常可以通過本地主機,它會正常工作。我想這個問題與XAMPP本身有關。

感謝大家的幫忙!

+0

該代碼是否遇到錯誤?日誌顯示有用嗎?在調試時,代碼與預期行爲有什麼不同? – David

+0

激活錯誤報告。 –

+0

你在檢查大小/擴展名/文件類型?你確定錯誤是在move_uploaded_file而不是其他地方?如果你沒有做任何檢查......嗯,你的服務器會很快恨你:) –

回答

0

我實際上只是運行你的代碼,它工作得很好。您可能遇到的唯一問題是權限,請確保/ tmp目錄具有允許移動該文件的0777權限。這裏是我跑的代碼:

<?php 
// Where the file is going to be placed 
if(isset($_FILES['uploadedfile'])){ 

$target_path = "img/"; 
$target_path = $target_path . basename($_FILES['uploadedfile']['name']); 
$target_path = "img/"; 
$target_path = $target_path . basename($_FILES['uploadedfile']['name']); 
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
    echo "The file ". basename($_FILES['uploadedfile']['name']). 
    " has been uploaded"; 
} else{ 
    echo "There was an error uploading the file, please try again!"; 
} 
} 
?> 


<form enctype="multipart/form-data" action="" method="POST"> 
<input type="hidden" name="MAX_FILE_SIZE" value="100000" /> 
Choose a file to upload: <input name="uploadedfile" type="file" /><br /> 
<input type="submit" value="Upload File" /> 
</form> 
+0

isset()不足以檢查有效的上傳。失敗的上傳也有一個$ _FILES條目,但大多數數據丟失/無效,並且爲非零錯誤代碼。 –

+0

我沒有聲稱它檢查上傳,當然他必須自己做這部分,這只是一個簡單的黑客我做了HTML和PHP在一個頁面,這就是所有...他應該檢查$ _FILES錯誤數組等... –

2

除了在上傳過程中,臨時目錄將爲空。 PHP自動刪除上傳的文件,除非你自己處理它們。我建議檢查你的服務器和PHP日誌來查找錯誤消息,並且肯定會爲腳本添加一些錯誤處理。你假設上傳成功,這是不好的:

if ($_FILES['uploadedfile']['error'] === UPLOAD_ERR_OK) { 
    ... your code here ... 
} else { 
    die("Upload failed with error code " . $_FILES['uploadedfile']['error']); 
} 

的代碼定義here

+0

這是一個偉大的觀點,我從來沒有意識到它,謝謝! – Fahad