PHP上传和MySQL插入后的白色空图像


White empty images after PHP upload and MySQL insertion

我创建了一个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

它没有显示在浏览器中,我缺少什么?

当你试图链接到图像或打开图像时,路径可能不是你想象的那样。

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

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

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

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

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

还要注意,如果存在sql注入问题,则应切换到带有绑定变量的已准备语句。

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

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

现在工作正常