尝试将图像上传到文件夹并将其位置保存在数据库错误中

时间:2016-05-01 02:28:11

标签: php mysql

我一直收到这个错误,我不知道为什么。

上传表单

<?php
require_once 'processor/dbconfig.php';

if(isset($_POST['submit']))
{
    if($_FILES['image']['name'])
    {
      $save_path="newimages"; // Folder where you wanna move the file.
      $myname = strtolower($_FILES['image']['tmp_name']); //You are renaming the file here
      move_uploaded_file($_FILES['image']['tmp_name'], $save_path.$myname); // Move the uploaded file to the desired folder
    }

    $hl = $_POST['headline'];
    $fd = $_POST['feed'];
    $st = $_POST['story'];
    $tp = $_POST['type'];

    if($user->send($h1,$fd,$st,$tp,$save_path,$myname))
    {
        echo 'Success';
        $user->redirect("input.php");
    }
}
?>

<form enctype="multipart/form-data" method="post">
   <input type="text" name="headline">
   <input type="text" name="feed">
   <textarea cols="15" id="comment" name="story" placeholder="Message" rows="10"></textarea>
   <input type="text" name="type">
   <input type="file" name="image">
   <input type="submit" name="submit">
</form>

这是我的SEND功能

public function send($hl,$fd,$st,$tp,$save_path,$myname)
{
  try
  {
    $stmt = $this->db->prepare("
        INSERT INTO news(headline,feed,story,type,folder,file)
        VALUES(:headline, :feed, :story, :type, :foler, :file);
    ");

    $stmt->bindparam(":headline", $hl);
    $stmt->bindparam(":feed", $fd);
    $stmt->bindparam(":story", $st);
    $stmt->bindparam(":type", $tp);
    $stmt->bindparam(":folder", $save_path);
    $stmt->bindparam(":file", $myname);

    $stmt->execute();

    return $stmt;
  }
  catch(PDOException $e)
  {
      echo $e->getMessage();
  }
}

最后,这是我的错误。

Warning: move_uploaded_file(newimages//var/tmp/phpyctf0k) [function.move-uploaded-file]: failed to open stream: No such file or directory in /nfs/c11/h05/mnt//domains/concept/html/input.php on line 12

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/var/tmp/phpyctf0K' to 'newimages//var/tmp/phpyctf0k' in /nfs/c11/h05/mnt//domains/concept/html/input.php on line 12
SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

是的,我确实创建了一个名为newimages的新文件夹。

1 个答案:

答案 0 :(得分:0)

所以你说你已经在当前运行的php脚本newimages的同一 relative 目录中创建了一个文件夹,你要在其中上传这个文件。为了绝对清楚,这不是绝对路径,那没关系。

$save_path="newimages";

我认为它需要一个尾随斜杠,因为它出现在输出中,但不在你的示例代码中。

$save_path="newimages/";

在这一行上,您将绝对路径中的临时文件的整个路径和文件名设为小写/var/tmp/phpyctf0k

$myname = strtolower($_FILES['image']['tmp_name']); //You are renaming the file here

所以在这一行上,您追加本地相对路径newimages以及临时文件的整个绝对路径/var/tmp/phpyctf0k,所以这现在向PHP解释器指示您打算将文件移动到不存在的目录newimages/var/tmp/phpyctf0k

move_uploaded_file($_FILES['image']['tmp_name'], $save_path.$myname); // Move the uploaded file to the desired folder

为了解决这个问题,您可以使用类似basename

的内容
move_uploaded_file($_FILES['image']['tmp_name'], $save_path.basename($myname)); // Move the uploaded file to the desired folder

或@ $_FILES['image']['name'] @Sean评论。

move_uploaded_file($_FILES['image']['tmp_name'], $save_path.strtolower($_FILES['image']['name'])); // Move the uploaded file to the desired folder