2011-11-14 60 views
0

我嘗試使用下面的代碼將圖片上傳到服務器中的目錄。然而,當我運行它,我得到這個錯誤:移動上傳文件時出錯

警告: move_uploaded_file(圖片/)[function.move上傳文件]:未能打開流:是在/ home/a2943534 /的public_html目錄/add.php上線24

警告: move_uploaded_file()以[function.move上傳文件]:無法 '/ TMP/php7yEkDe' 移動到在/ home/a2943534 /的public_html/'圖像/'第24行的add.php

我在這裏錯過了什麼?

<?php 
include_once("connect.php"); 
?> 
<?php 

//This is the directory where images will be saved 
$target = "images/"; 
$target = $target . basename($_FILES['photo']['title']); 

//This gets all the other information from the form 
$title=$_POST['title']; 
$name=$_POST['name']; 
$describe=$_POST['describe']; 
$pic=($_FILES['photo']['title']); 
$url=$_POST['url']; 
$country=$_POST['country']; 
$endDate=$_POST['endDate']; 


//Writes the information to the database 
mysql_query("INSERT INTO `authors` VALUES ('$title', '$name', '$describe', '$pic', '$url', '$country', '$endDate')") ; 

//Writes the photo to the server 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 

//Tells you if its all ok 
    $result = "The file ". basename($_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
} 
else { 

//Gives and error if its not 
$result = "Sorry, there was a problem uploading your file."; 
} 
?> 
<?php 
// if the form has been submitted, display result 
if (isset($result)) { 
    echo "<p><strong>$result</strong></p>"; 
    } 
?> 
+0

確保'images'目錄確實存在。 –

+0

還要確保'images'目錄具有寫入權限。 – JRSofty

+0

首先,您錯過了清理,導致了任意文件寫入漏洞。請參閱http://stackoverflow.com/questions/1911382/sanitize-file-path-in-php – Polynomial

回答

0

,而不是寫

$target = $target . basename($_FILES['photo']['title']); 

你應該寫

$target = $target . basename($_FILES['photo']['name']); 

我覺得有沒有像$ _FILES [ '照片'] [ '標題'] ..

+0

謝謝。我會改變它,並得到回覆 – Kelvin

+1

感謝它現在的作品:) – Kelvin

+0

我的榮幸.... :) –

0

我覺得你與

$target = $target . basename($_FILES['photo']['title']); 

錯誤應

$target = $target . basename($_FILES['photo']['name']); 

這是因爲標題不會在$ _FILES [ '照片']中存在

另外這個錯誤陳述它:

Unable to move '/tmp/php7yEkDe' to 'images/' in /home/a2943534/public_html/add.php on line 24 

圖像/不包含您的文件名。

0

錯誤很明顯,不言自明。
第二個參數必須是文件名,而不是目錄。

+0

這是一個文件名是不是? – Kelvin

+0

你的PHP清楚地表明你只提供了一個文件夾:「無法移動到**」圖像/「**」,不是嗎? –

0

除了SQL注入問題似乎出現在每個單獨的PHP問題中,在這裏以MySQL爲特色,您應該開始調試。

你會得到一個非常明確的錯誤,在哪一行以及哪個函數調用導致錯誤。查看Google上的錯誤,閱讀manual瞭解您正在使用的功能。長話短說:你應該創建兩個變量,在你的函數調用之前將它們打印出來,然後弄清楚什麼是錯誤的。

<?php 

$source = $_FILES['photo']['tmp_name']; 
$target = "images/" . basename($_FILES['photo']['title']); 

echo "Moving '$source' to '$target'"; 

move_uploaded_file($source, $target); 

您會立即看到發生錯誤的位置。

+0

請,我有上傳位工作,但我無法讓插入工作。請問有誰知道爲什麼 – Kelvin

+0

關於「開始調試」部分,你沒有得到什麼? :-)在你的查詢中很可能會有錯誤。打印它,分析它,改變它直到它工作。看看文檔。 – CodeCaster