【问题标题】:Upload on server via php error通过php错误上传到服务器
【发布时间】:2015-05-14 23:10:10
【问题描述】:

我正在尝试创建一个上传表单,我可以在其中加载图像,然后图像的路径将保存在数据库中。当然,到目前为止还没有工作!这是我使用的代码:

<?php
include("db_connect.php");

    function GetImageExtension($imagetype)
     {
       if(empty($imagetype)) return false;
       switch($imagetype)
       {
           case 'image/bmp': return '.bmp';
           case 'image/gif': return '.gif';
           case 'image/jpeg': return '.jpg';
           case 'image/png': return '.png';
           default: return false;
       }
     }



if (!empty($_FILES["uploadedimage"]["name"])) {

    $file_name=$_FILES["uploadedimage"]["name"];
    $temp_name=$_FILES["uploadedimage"]["tmp_name"];
    $imgtype=$_FILES["uploadedimage"]["type"];
    $ext= GetImageExtension($imgtype);
    $imagename=date("d-m-Y")."-".time().$ext;
    $target_path = "images/".$imagename;


if(move_uploaded_file($temp_name, $target_path)) {

    $query_upload="INSERT into 'walls' ('images_path','submission_date') VALUES ('".$target_path."','".date("Y-m-d")."')";
    mysql_query($query_upload) or die("error in $query_upload == ----> ".mysql_error());  

}else{

   exit("Error While uploading image on the server");
} 

}

?>
<h1>Image UPLOADER</h1>
<form action="uploader.php" enctype="multipart/form-data" method="post">

<table style="border-collapse: collapse; font: 12px Tahoma;" border="1" cellspacing="5" cellpadding="5">
<tbody><tr>
<td>
<input name="uploadedimage" type="file">
</td>

</tr>

<tr>
<td>
<input name="Upload Now" type="submit" value="Upload Image">
</td>
</tr>


</tbody></table>

</form>

db_connect.php

<?php

    $sql_servername = "localhost";
    $sql_user = "***";
    $sql_pass = "***";
    $sql_db_name = "***";

    // connect to mysql server
    $db = new mysqli($sql_servername, $sql_user, $sql_pass, $sql_db_name);
    if (!$db) {
        die('Could not connect to db: ' . mysql_error());
    }

?>

当然我不能显示凭据,但我确信 100% 是正确的并且不存在问题!当我尝试上传时,我收到消息:

Error While uploading image on the server

但我不明白为什么以及如何解决。任何的想法?谢谢

【问题讨论】:

  • 出于调试原因,您应该print_r($_FILES)。我怀疑它会出错。 PHP 返回一个适当的错误代码以及文件数组,请查看此站点以查看所有可能的错误:php.net/manual/en/features.file-upload.errors.php
  • mmh 我试图打印 print_r($_FILES) 但没有出现...正常吗?
  • 将错误报告添加到您的文件顶部,紧跟在您的打开 PHP 标记之后,例如 &lt;?php error_reporting(E_ALL); ini_set('display_errors', 1); 然后是您的其余代码,以查看它是否产生任何结果。
  • ok wait now print 出现这个:Array ( [uploadedimage] =&gt; Array ( [name] =&gt; icona.png [type] =&gt; image/png [tmp_name] =&gt; /tmp/phpARtAYu [error] =&gt; 0 [size] =&gt; 25916 ) ) Array
  • print_r($_FILES) 至少应该打印一个空数组。即使 error_reporting 被禁用。你在退出前做过吗?

标签: php image mysqli upload


【解决方案1】:

尝试使用:

$query_upload = "INSERT INTO walls (images_path, submission_date) VALUES ('".$target_path."','".date("Y-m-d")."')";

您不应该用引号将您的表格/列名括起来,如果您愿意,您应该使用反引号,但这不是必需的。

然后更改您的查询以使用任一 OOP:

$db->query($query_upload) or die($db->error);

或程序风格:

mysqli_query($db, $query_upload) or die(mysqli_error($db));

代替已弃用的 mysql_query() 函数。

【讨论】:

  • mysqli_query() 至少需要 2 个参数.. 出了点问题
  • 我在 mysqli_query 参数中添加了 $db ......现在我有这个错误:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''images_path','submission_date') VALUES ('/var/www/html/images/14-05-2015-143' at line 1
  • @End.Game 现在您可以看到使解决此问题变得更加容易的错误。我已经编辑了我的答案,但是您应该考虑使用准备好的语句,因为我个人使用 PDO,所以我没有发布如何执行此操作,它可能超出了您的问题范围。
猜你喜欢
  • 1970-01-01
  • 2016-10-22
  • 2014-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-27
  • 1970-01-01
  • 2013-07-14
相关资源
最近更新 更多