【问题标题】:Error when moving uploaded files移动上传的文件时出错
【发布时间】: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>";
  }
?>

【问题讨论】:

标签: php mysql upload


【解决方案1】:

而不是写

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

你应该写

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

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

【讨论】:

    【解决方案2】:

    我认为你犯了一个错误

    $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
    

    to images/ 不包含您的文件名。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 你的PHP明确说你只提供了一个文件夹:“无法移动到'images/'”,不是吗?
      【解决方案4】:

      除了 SQL 注入问题似乎出现在每个 PHP 问题中,这里以 SO 为特色,你应该开始调试。

      您会在哪一行和哪个函数调用导致错误上得到一个非常清楚的错误。在 Google 上查找错误,阅读manual 了解您正在使用的功能。

      长话短说:您应该创建两个变量,在函数调用之前将它们打印出来,然后找出问题所在。

      <?php
      
      $source = $_FILES['photo']['tmp_name'];
      $target = "images/" . basename( $_FILES['photo']['title']); 
      
      echo "Moving '$source' to '$target'";
      
      move_uploaded_file($source, $target);
      

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

      【讨论】:

      • 拜托,我已经完成了上传工作,但我无法让插入工作。请问有谁知道原因
      • 您对“开始调试”部分没有得到什么? :-) 您的查询很可能有错误。打印它,分析它,改变它直到它工作。查看文档。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-21
      • 1970-01-01
      • 1970-01-01
      • 2010-12-16
      相关资源
      最近更新 更多