移动上传文件时出错

时间:2011-11-14 12:28:38

标签: php mysql upload

我尝试使用下面的代码将图片上传到我服务器中的目录。但是,当我运行它时,我收到此错误:

警告: move_uploaded_file(images /)[function.move-uploaded-file]:无法打开流:第24行/home/a2943534/public_html/add.php中的目录< / p>

警告: 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>";
  }
?>

4 个答案:

答案 0 :(得分:0)

我认为你错了

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

哪个应该是

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

这是因为$ _FILES ['photo']

中不存在标题

此错误也表明了这一点:

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

图片/不包含您的文件名。

答案 1 :(得分:0)

错误很明显且不言自明 第二个参数必须是文件名,而不是目录。

答案 2 :(得分:0)

而非写作

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

你应该写

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

我认为没有像$ _FILES ['photo'] ['title'] ..

答案 3 :(得分:0)

除了SQL注入问题,这个问题似乎出现在SO的每个PHP问题上,你应该开始调试。

您在哪一行以及哪个函数调用导致错误时会得到一个非常明确的错误。在Google上查看错误,请阅读manual了解您正在使用的功能。

长话短说:你应该创建两个变量,在函数调用之前打印它们,并找出错误。

<?php

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

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

move_uploaded_file($source, $target);

您将立即看到错误发生的位置。