【问题标题】:White empty images after PHP upload and MySQL insertionPHP上传和MySQL插入后的白色空图像
【发布时间】:2013-11-29 17:31:43
【问题描述】:

我创建了一个 PHP 和 MySQL 脚本,它通过 PHP 成功将提交的图像上传到我服务器上的文件夹,然后将带有扩展名的文件名添加到我的 MySQL 数据库中。

使用 FTP 程序,我可以在服务器上正确的文件夹中看到提交的图像,并且文件大小正确。但是,当我在浏览器中输入新上传图片的文件路径 (http://xxxxxx.com/images/image.jpg) 时,我得到一个空白页面。此外,当我尝试将图像导入网站时,什么也没有出现。

但是,当我通过 FTP 程序将图像重新下载到我的计算机上时,我可以看到图像完全正常。我错过了什么?

我的代码摘录如下:

    <?php
 // getting current post id and slug
 $pid = $_POST['pid'];
 $slug = $_POST['slug'];

 //This is the directory where images will be saved 
 $target = '../company/'.$slug.'/images/'; 
 $target = $target . basename( $_FILES['image']['name']); 

 //This gets all the other information from the form 
 $pic = ($_FILES['image']['name']); 
 $fileTmpLoc = ($_FILES["image"]["tmp_name"]);

 $extract = explode(".", $pic);
 $fileExt = end($extract);
 list($width, $height) = getimagesize($fileTmpLoc);
    if($width < 10 || $height < 10){
        header("location: ../message.php?msg=ERROR: That image has no dimensions");
        exit(); 
    }
 $rename = rand(100000000000,999999999999).".".$fileExt;

// check for correct filetype
if (!preg_match("/\.(gif|jpg|png)$/i", $pic) ) {
        header("location: ../message.php?msg=ERROR: incorrect filetype");
        exit();
    }



 include_once "../database-connect.php";

 //Writes the information to the database 
 mysqli_query($dbconnection,"UPDATE companies SET picture='$rename' WHERE ID='$pid'") ; 

 //Writes the photo to the server 
 if(move_uploaded_file($fileTmpLoc, "../company/'.$slug.'/images/$rename")) 
 { 

 .... etc

我错过了什么,它没有显示在浏览器中?

【问题讨论】:

  • 我唯一能想到的可能是您可能没有在 PHP 中加载 GD 库。或者,也许,您使用 CMS,它有自己的 htaccess 重写规则,这些规则没有正确使用 -f 标志,因此不会忽略指令

标签: php mysql image upload ftp


【解决方案1】:

当您尝试链接到图像或尝试打开它时,可能路径不是您认为的那样。

请注意,这看起来非常错误:

if(move_uploaded_file($fileTmpLoc, "../company/'.$slug.'/images/$rename")) 

这将为您的路径添加两个引号和两个点,因此如果$slugsome_company,则路径将为:

/company/'.some_company.'/images/123456789.jpg

也许您在 ftp 程序中没有看到或没有注意到这一点。

还要注意你有一个sql注入问题,你应该切换到绑定变量的prepared statements。

【讨论】:

  • 谢谢,杰罗恩,会调查的。但是,我在正确的位置使用我的 FTP 客户端查看图像,如果我从我的 FTP 客户端复制路径 URL 并将其粘贴到浏览器中(URL 正确),那么它只是空白 ...
  • @rainerbrunotte 奇怪,但也许 ftp 客户端隐藏了特殊字符。
  • 谢谢,jeroen,我已经编辑了 URL,现在可以正常工作了 :)
【解决方案2】:

问题确实是 URL 输出结构。已经改变了,就像 jeroen 建议的那样:

if(move_uploaded_file($fileTmpLoc, "../images/$rename"))

现在可以正常使用

【讨论】:

    猜你喜欢
    • 2012-05-09
    • 1970-01-01
    • 2013-02-01
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2016-07-16
    相关资源
    最近更新 更多