我嘗試使用下面的代碼將圖片上傳到服務器中的目錄。然而,當我運行它,我得到這個錯誤:移動上傳文件時出錯
警告: 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>";
}
?>
確保'images'目錄確實存在。 –
還要確保'images'目錄具有寫入權限。 – JRSofty
首先,您錯過了清理,導致了任意文件寫入漏洞。請參閱http://stackoverflow.com/questions/1911382/sanitize-file-path-in-php – Polynomial