【问题标题】:i can not upload image php everything is ok but it failes uploading我无法上传图片 php 一切正常,但上传失败
【发布时间】:2020-04-26 16:52:28
【问题描述】:

一切正常,我可以单独将查询插入到我的数据库中,但它不会移动上传的文件 这是我的代码。如果您能在下面找到任何问题,请查看。

它检查了我的图像文件夹目录是正确的,该目录将我带到了确切的文件夹。 upload_file 也在 php.ini
但它仍然无法正常工作。

<?php

$conn = mysqli_connect("localhost", "root", "HERE_IS_MYCORRECT_PASSWORD", "HERE_IS_MYCORRECT_DTABASE_NAME");
if($conn) {

echo "<p style='color:green;'>connected</p>";
}
else {
    echo " connection failed";
}
if(isset($_POST['uploadfilesub'])){
    $id = $_POST['id'];
    $filename = $_FILES['uploadfile']['name'];
    $filetmpname = $_FILES['uploadfile']['tmp_name'];
    $folder = './Resources/style/images/' . $filename;
    echo "<p>".$filename."</p>";
    echo "<p>".$filetmpname."</p>";
    $sql = "INSERT INTO product_images VALUES ($id,'$filename') ";
    if(move_uploaded_file($filetmpname , $folder)){
         echo "<p color='green'>moved</p>";
         $qry = mysqli_query($conn, $sql);
         if($qry){
            echo "<p color='green'>Inserted into mysql</p>";
        } else {
            echo "<p color='red'>Failed to Insert</p>";
        }
     }
     else {
         echo "<p style='color:red;'>Failed to move</p>";
    }

}
?>

 <!DOCTYPE html>
 <html lang="en">
 <head>
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <meta http-equiv="X-UA-Compatible" content="ie=edge">
     <title>Document</title>
 </head>
 <body>
     <form action="" method="post" enctype="multipart/form-data">
        <input type="text" name="id">
        <input type="file" name="uploadfile" />
        <input type="submit" name="uploadfilesub" value="upload" />
     </form>
 </body>
 </html>

【问题讨论】:

  • 警告!您对 SQL 注入攻击敞开心扉!您应该使用参数化的prepared statements,而不是像这样直接在查询中使用完全未转义的用户数据。
  • 是的,谢谢,但我知道这只是用于图像上传测试。这不起作用

标签: php mysql file-upload image-uploading


【解决方案1】:

据我所知,您的错误(关于特定的、提到的问题)非常简单:

$folder = './Resources/style/images/';

改为:

$folder = './Resources/style/images/' . $filename;

move_uploaded_file 需要完整的目标路径,而不仅仅是目标文件夹。

【讨论】:

  • 我也试过这个,但它不起作用。我编辑了我的问题检查请我的代码与上面相同
  • hmm,我实际上已经运行了您的代码(没有 sql 部分)并且它可以工作,所以剩下的唯一问题必须是路径:尝试使用 var_dump(is_dir('./Resources/style/images/')); 进行检查,如果它是可写的: var_dump(is_writable('./Resources/style/images/'));。如果它返回 FALSE,则问题出在路径上。您可以通过调用var_dump(getcwd()); 来检查脚本的工作路径
  • 感谢您的解决方案有效。问题在于我的目录权限。
猜你喜欢
  • 2014-10-25
  • 2014-07-22
  • 1970-01-01
  • 2015-04-12
  • 2017-02-24
  • 2012-01-13
  • 1970-01-01
  • 1970-01-01
  • 2013-07-28
相关资源
最近更新 更多