-2

我正在尝试在我的网站上构建图像上传。我正在使用的代码将值插入到我的数据库中,但图像没有移动到我的目录中?

任何人都可以帮助我,或者给我如何调试的建议吗?

输入

 <label for="picture_1">picture 1 : </label>
    <input type="file" name="picture_1" id="picture_1" />
</li>
<li>
    <label for="picture_2">picture 2 : </label>
    <input type="file" name="picture_2" id="picture_2" />
</li>
<li>
    <label for="picture_3">picture 3 : </label>
    <input type="file" name="picture_3" id="picture_3" />

上传文件

if(sizeof($_FILES)){ 
for($i = 1; $i <= 3; $i++) {
 $aFile = $_FILES['picture_'.$i];
 if(empty($aFile['tmp_name'])) continue; # skip for empty elements

    $allowedExts = array("jpg", "jpeg", "gif", "png");
    $extension = end(explode(".", $aFile["name"]));
    if ((($aFile["type"] == "image/gif")
    || ($aFile["type"] == "image/jpeg")
    || ($aFile["type"] == "image/png")
    || ($aFile["type"] == "image/pjpeg"))
    && ($aFile["size"] < 200000000)
    && in_array(strtolower($extension), $allowedExts))
      {
      if ($aFile["error"] > 0)
        {
        echo "Return Code: " .$aFile["error"] . "<br>";
        }
      else
        {       
        if (file_exists("upload/" . $aFile["name"]))
          {
          echo $aFile["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($aFile['tmp_name'], 
            "upload/" .$aFile["name"]); 
          echo "Image Uploaded Successfully";
          }
        }
      }
    else
    {
      echo "Invalid file";  
    }
}

完成页面

include 'assets/connection.class.php';


$firstname = $_POST['First_Name'];


// Connection data (server_address, database, name, poassword)
$hostdb = 'localhost';
$namedb = '';
$userdb = '';
$passdb = '';

try {
  // Connect and create the PDO object
 $conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
$conn->exec("SET CHARACTER SET utf8");      // Sets encoding UTF-8
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

 $sql = "INSERT STMT WAS HERE";

 $statement = $conn->prepare($sql);
 $statement->bindValue(":firstname", $firstname);
 ...
 List of bind values etc is here


 $count = $statement->execute();

  $conn = null;        // Disconnect
}
catch(PDOException $e) {
  echo $e->getMessage();
}


if(sizeof($_FILES)){ 
    for($i = 1; $i <= 3; $i++) {
     $aFile = $_FILES['picture_'.$i];
     if(empty($aFile['tmp_name'])) continue; # skip for empty elements

        $allowedExts = array("jpg", "jpeg", "gif", "png");
        $extension = end(explode(".", $aFile["name"]));
        if ((($aFile["type"] == "image/gif")
        || ($aFile["type"] == "image/jpeg")
        || ($aFile["type"] == "image/png")
        || ($aFile["type"] == "image/pjpeg"))
        && ($aFile["size"] < 200000000)
        && in_array(strtolower($extension), $allowedExts))
          {
          if ($aFile["error"] > 0)
            {
            echo "Return Code: " .$aFile["error"] . "<br>";
            }
          else
            {       
            if (file_exists("upload/" . $aFile["name"]))
              {
              echo $aFile["name"] . " already exists. ";
              }
            else
              {
              move_uploaded_file($aFile['tmp_name'], "'upload/" .$aFile["name"]."'"); 
              echo "Image Uploaded Successfully";
              }         
            }
          }
        else
        {
          echo "Invalid file";  
        }
    }

}


// If data added ($count not false) displays the number of rows added
if($count !== false) echo 'Number of rows added: '. $count; 


?>


<a href="test.php">Go back </a>

错误日志

[Tue Aug 13 19:27:31 2013] [error] [client 89.240.60.62] PHP Warning:  move_uploaded_file('upload/13-001945_1.jpg'): failed to open stream: No such file or directory in /home/liam.co.uk/domains/tiercakes.co.uk/public_html/access/add-cake.php on line 132, referer: http://www.tiercakes.co.uk/access/add-cake.php

[Tue Aug 13 19:27:31 2013] [error] [client 89.240.60.62] PHP Warning:  move_uploaded_file(): Unable to move '/tmp/phpxrwtlM' to ''upload/13-001945_1.jpg'' in /home/liam.co.uk/domains/tiercakes.co.uk/public_html/access/add-cake.php on line 132, referer: http://www.tiercakes.co.uk/access/add-cake.php
4

2 回答 2

0

您缺少一个右花括号,它会在您的 php 文件中创建一个错误,因此它不会执行。这应该是您的代码的结尾,如下所示:

    else
        {
          echo "Invalid file";  
        }
    }
}

只需添加它看起来像的最后一个花括号,你应该会很好。

于 2013-08-12T20:22:35.120 回答
0

修复它!,问题是这条线

move_uploaded_file($aFile['tmp_name'], "'upload/" .$aFile["name"]."'"); 

我在上传开始和文件名末尾有一个额外的 ',我在 apache 错误日志中注意到了这一点,感谢您的所有输入

于 2013-08-13T19:47:00.190 回答