2011-05-31 137 views
1

我已經在另一個網站中使用了這段代碼,它工作得很好,所以我非常困惑,爲什麼這個工作不正常。我改變的唯一的東西是文件被移動到的目錄,但文件永遠不會上傳。我三重檢查了一切,這一切似乎都很好。代碼全部執行沒有錯誤,並且所有信息都正確地輸入到數據庫中,唯一的問題是文件沒有上傳。我瘋了試圖讓這個工作!請幫忙!PHP文件上傳表單無法正常工作

HTML表單:

<p> 
<h2>Add News</h2><br /> 
REQUIRED *<br /><br /> 
<form enctype="multipart/form-data" action='addnews2.php' method='post' onsubmit="chose.value = uploadForm()" name="newsform"> 
<p>Headline: * 
<br> 
<input name="title" type="text" size="75"> 
<br /> 
<br/> 

    News: *<br> 
<textarea name="text" cols="75" rows="10" width="200"></textarea><br /><br /> 
Image File: 
<br> 
<input type="file" name="filetoupload" id="filetoupload"><br/><br /> 

    <input type="submit" name="choose" value="Submit" /> 
</p> 
</form> 
</p> 

處理頁面:

$date=date("l F d, Y"); 

$title=stripslashes($_POST['title']); 
$text=stripslashes($_POST['text']); 

if (($_FILES["filetoupload"]["type"] == "image/gif") 
|| ($_FILES["filetoupload"]["type"] == "image/jpeg") 
|| ($_FILES["filetoupload"]["type"] == "image/pjpeg") 
|| ($_FILES["filetoupload"]["type"] == "image/png") 
|| ($_FILES["filetoupload"]["type"] == "image/jpg")) 
    { 
    if ($_FILES["filetoupload"]["error"] > 0) 
    { 
    echo "Return Code: " . $_FILES["filetoupload"]["error"] . "<br />"; 
    } 
    else 
     { 
      if (file_exists("images/news/" . $_FILES["filetoupload"]["name"])) 
     { 
     echo $_FILES["filetoupload"]["name"] . " already exists. "; 
     } 
     move_uploaded_file($_FILES["filetoupload"]["tmp_name"], 
     "images/news/" . $_FILES["filetoupload"]["name"]); 
     } 
    } 



$image = "images/news/".$_FILES['filetoupload']['name']; 


if($title==""||$text==""){ 
    die("You need to fill in all details"); 
} 


connect_to_db(); 
$query="insert into news (date, title, text, image) values ('".$date."','".mysql_real_escape_string($title)."','".mysql_real_escape_string($text)."','".$image."')"; 
$result=mysql_query($query); 

if(mysql_affected_rows()==1){ 
header("Location:index.php?page=Admin&news=added"); 
} 
else{ 
    die("there was a problem"); 
} 
mysql_close(); 
+2

您是否檢查了上傳目錄的權限? – 2011-05-31 14:52:41

+0

你的move_uploaded_file代碼總是執行,你需要把它放在else部分 – 2011-05-31 14:57:19

+0

爲了追蹤這個問題,你應該構造一個沒有數據庫信息的新的臨時php文件,然後收集和回顯所有變量的信息以確保你的位置,路徑和構建的數據都是有效的。還啓用錯誤報告(http://php.net/manual/en/function.error-reporting.php)。另外,如前所述,最終副本的目標位置可能沒有適當的文件權限(寫入訪問權限)。 – horatio 2011-05-31 15:05:54

回答

1

看起來你可能缺少一個else語句,代碼格式化關閉,造成混亂:

if (file_exists("images/news/" . $_FILES["filetoupload"]["name"])) 
    { 
     echo $_FILES["filetoupload"]["name"] . " already exists. "; 
    }else { 
     move_uploaded_file($_FILES["filetoupload"]["tmp_name"], 
    "images/news/" . $_FILES["filetoupload"]["name"]); 
    } 

給那一槍。如果沒有,請檢查您的權限等,並確保文件沒有被上傳,只是不在您預期的位置。

只需要一點信息,我會在做圖像檢查之前檢查$title$text。如果兩者都爲空,您可能不想將圖片上傳到網站。你也應該將之前逃避圖像路徑文件名:

$image = "images/news/".mysql_real_escape_string($_FILES['filetoupload']['name']); 

至於那也可能是容易注射。

+0

感謝您的快速響應。不幸的是,這並沒有解決問題:/ – nutman 2011-05-31 14:57:20

+0

我剛剛意識到問題所在。我沒有給文件夾寫入權限。我是這樣的eeeejot!但你的迴應引發了這麼多讚賞:) – nutman 2011-05-31 15:01:46