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


Trying to upload image to a folder and save its location in DB error

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

上传表单

<?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的新文件夹。

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

$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

或者可能是@Sean评论的$_FILES['image']['name']

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