2009-06-09 99 views
0

我想用PHP將文件上傳到服務器。我無法讓它工作。下面是代碼:在php中上傳文件的問題

if(isset($_POST['Upload'])) 
{ 

    //size condition 
    if ($_FILES['uploaded']['size'] > 350000) 
    { 
     $mesg = "Your file is too large.<br>"; 
     exit; 
    } 

    if(move_uploaded_file($_FILES['uploaded']['tmp_name'], "upload/" . $_FILES['uploaded']['name'])) 
    { 
     $mesg = "The file ". basename($_FILES['uploaded']['name']). " has been uploaded";  
    } 
    else 
    {  
     $mesg = "Sorry, there was a problem uploading your file."; 
    }  
} 
else 
{ 
    $mesg = "Select a File to upload."; 
} 

這裏是我使用的文件提交表單的代碼:將上傳

<? 
echo $mesg; 
?> 
<br /><br /> 
<form enctype="multipart/form-data" action="" method="POST"> 
Please choose a file: <input name="uploaded" type="file" /><br /> 
<input type="submit" value="Upload" /> 
</form> 

回答

3

您的提交按鈕沒有一個名字:

<input type="submit" value="Upload" /> 

你檢查$_POST['Upload'],所以你可能想:

<input type="submit" value="Upload" name="Upload" /> 
6

您需要enctype="multipart/form-data"<form>標籤或裏面什麼都沒有。

欲瞭解更多信息,請查看PHP manual

另外,我不確定您是否只是在測試功能,但是您應該謹慎將上載的文件放到網絡可訪問的文件夾中,尤其是使用其原始名稱。這爲有人上傳惡意腳本並可能接管您的服務器留下了一扇敞開的大門。

+2

+1優秀的安全保護點 – 2009-06-09 00:09:55

+0

這是一個密碼保護區,只有2或3人可以訪問所以我不擔心惡意腳本。 – 2009-06-09 00:11:55

+0

我添加了enctype =「multipart/form-data」,但我仍然遇到同樣的問題 – 2009-06-09 00:13:43

1

您的形式更改爲

<form action="" method="POST" enctype="multipart/form-data">