照片上传getimagesize

照片上传getimagesize

本文介绍了照片上传getimagesize()警告-文件名不能为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个难题,想知道是否有人可以给我一个直接的答案.因此,我使用PHP/MySQL构建了一个照片上传脚本.在脚本中,将调整照片的大小,并在上传时为其指定一个临时名称.我使用几张图片(文件大小220 KB | 960 x 720)对其进行了测试,并且一切正常.然后,我尝试从数码相机上传几张照片(文件大小2.47 MB​​ | 3000 x 4000),突然间我收到此错误:

I've run into a conundrum and was wondering if anyone might be able to give me a straight answer. So I built a photo upload script using PHP/MySQL. Within the script photos are re-sized and given a temporary name while being uploaded. I tested it using several pictures (file size 220 KB | 960 x 720) and everything was working just fine. Then I attempted to upload several pictures from my digital camera (file size 2.47 MB | 3000 x 4000) and all of a sudden I got this error:

Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in /php_parsers/photo_system.php on line 94

Warning: Cannot modify header information - headers already sent by (output started at /php_parsers/photo_system.php:94) in /php_parsers/photo_system.php on line 96

我检查了stackoverflow中是否有类似问题的帖子,并遇到了一个问题,但这似乎不适用于我遇到的情况.

I checked stackoverflow for a post with a similar issue and came upon one however it didn't seem to apply to the scenario I'm experiencing.

这是"photo_system.php"的适用代码.我已经评论了令人讨厌的第94和96行.您能提供的任何帮助/想法将不胜感激!

Here is the applicable code for "photo_system.php". I have commented the offending lines 94 and 96. Any help/ideas you could give would be greatly appreciated!

     <?php
     if (isset($_FILES["photo"]["name"]) && isset($_POST["gallery"])){
         $sql = "SELECT COUNT(id) FROM photos WHERE user='$log_username'";
         $query = mysqli_query($db_conx, $sql);
         $row = mysqli_fetch_row($query);
         if($row[0] > 79){
             header("location: ../message.php?msg=The system allows only 80 pictures total");
             exit();
    }
    $gallery = preg_replace('#[^a-z 0-9,]#i', '', $_POST["gallery"]);
    $fileName = $_FILES["photo"]["name"];
    $fileTmpLoc = $_FILES["photo"]["tmp_name"];
    $fileType = $_FILES["photo"]["type"];
    $fileSize = $_FILES["photo"]["size"];
    $fileErrorMsg = $_FILES["photo"]["error"];
    $kaboom = explode(".", $fileName);
    $fileExt = end($kaboom);
    $db_file_name = date("DMjGisY")."".rand(1000,9999).".".$fileExt; // WedFeb272120452013RAND.jpg
    list($width, $height) = getimagesize($fileTmpLoc); //Offending Line 94
    if($width < 10 || $height < 10){
        header("location: ../message.php?msg=ERROR: That image has no dimensions"); //Offending Line 96
        exit();
    }
    if($fileSize > 4194304) {
        header("location: ../message.php?msg=ERROR: Your image file was larger than 4mb");
        exit();
    } else if (!preg_match("/\.(gif|jpg|png)$/i", $fileName) ) {
        header("location: ../message.php?msg=ERROR: Your image file was not jpg, gif or png type");
        exit();
    } else if ($fileErrorMsg == 1) {
        header("location: ../message.php?msg=ERROR: An unknown error occurred");
        exit();
    }
    $moveResult = move_uploaded_file($fileTmpLoc, "../user/$log_username/$db_file_name");
    if ($moveResult != true) {
        header("location: ../message.php?msg=ERROR: File upload failed");
        exit();
    }
    include_once("../php_includes/image_resize.php");
    $wmax = 800;
    $hmax = 600;
    if($width > $wmax || $height > $hmax){
        $target_file = "../user/$log_username/$db_file_name";
        $resized_file = "../user/$log_username/$db_file_name";
        img_resize($target_file, $resized_file, $wmax, $hmax, $fileExt);
    }
    $sql = "INSERT INTO photos(user, gallery, filename, uploaddate) VALUES ('$log_username','$gallery','$db_file_name',now())";
    $query = mysqli_query($db_conx, $sql);
    mysqli_close($db_conx);
    header("location: ../photos.php?u=$log_username");
    exit();
}
?><?php
if (isset($_POST["delete"]) && $_POST["id"] != ""){
    $id = preg_replace('#[^0-9]#', '', $_POST["id"]);
    $query = mysqli_query($db_conx, "SELECT user, filename FROM photos WHERE id='$id' LIMIT 1");
    $row = mysqli_fetch_row($query);
    $user = $row[0];
    $filename = $row[1];
    if($user == $log_username){
        $picurl = "../user/$log_username/$filename";
        if (file_exists($picurl)) {
            unlink($picurl);
            $sql = "DELETE FROM photos WHERE id='$id' LIMIT 1";
            $query = mysqli_query($db_conx, $sql);
        }
    }
    mysqli_close($db_conx);
    echo "deleted_ok";
    exit();
}
?>

推荐答案

好的,大家好.我弄清楚了问题所在.希望这会在将来对某人有所帮助.因此,我检查了phpinfo()并发现upload_max_filesize仅设置为2M.我将php.ini添加到有问题的文件的目录中,并包括:

OK everyone. I figured out what the issue was. Hopefully this will help someone in the future. So I checked my phpinfo() and found that upload_max_filesize was only set to 2M. I added php.ini to the directory of the offending file and included:

upload_max_filesize = 250M
post_max_size = 250M
max_execution_time = 300

date.timezone = "America/Los_Angeles"

我必须添加date.timezone,因为我的系统不喜欢没有定义它的事实.无论如何,这已经解决了问题.

I had to add the date.timezone because my system didn't like the fact that I didn't have it defined. Anyway this has resolved the issue.

这篇关于照片上传getimagesize()警告-文件名不能为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 01:38