【发布时间】:2011-12-28 15:01:16
【问题描述】:
我尝试使用以下代码将图片上传到服务器中的目录。但是,当我运行它时,我得到了这个错误:
警告: move_uploaded_file(images/) [function.move-uploaded-file]:无法打开流:是第 24 行 /home/a2943534/public_html/add.php 中的目录
警告: move_uploaded_file() [function.move-uploaded-file]:无法在 /home/a2943534/public_html/add.php 中将 '/tmp/php7yEkDe' 移动到 'images/'第 24 行
请问我在这里遗漏了什么?
<?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目录有写权限。 -
首先,您缺少清理,导致任意文件写入漏洞。见stackoverflow.com/questions/1911382/sanitize-file-path-in-php
-
@JRSofty 我很好奇,你有没有想过阅读这个问题?