【问题标题】:getimagesize() returns no data?getimagesize() 不返回数据?
【发布时间】:2012-01-17 05:31:44
【问题描述】:

尝试上传 .JPG 文件时出现以下错误。

Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in H:\Programs\webserver\root\media\images\upload.php on line 43

以下是upload.php文件的一部分:

if(isset($_FILES['files']))
{
    $cat = $_POST['cat'];
    $title = $_POST['title'];
    $album = $_POST['album'];

    foreach($_FILES['files']['tmp_name'] as $key => $tmp_name)
    {

        // get dimensions of uploaded images
        echo $temp = $_FILES['files']['tmp_name'][$key]; //location of file
        echo $type = $_FILES['files']['type'][$key]; //image type
        echo $size = $_FILES['files']['size'][$key]; // image size
        >> line 43 >>**echo $size2 = getimagesize($temp); //function to get info of file and put into array**
        echo $width = $size2[0]; // first part of the array is the image width
        echo $height = $size2[1]; // second part fo the array is the image width

        if($type == 'image/jpeg')
        {
        $filetype = '.jpeg';
        }else{
        $filetype = str_replace('image/','',$type);
        }
        $random_name = rand(1000,9999).rand(1000,9999).rand(1000,9999).rand(1000,9999);
        $path = 'img/'.$random_name . $_FILES['files']['name'][$key];
        $thumb_path = 'img/thumb_/'.$random_name .$_FILES['files']['name'][$key];

我有另一个 .jpg 图像文件,我一直在上传以测试哪个可以正常上传,.png 和 .gif 文件也可以正常上传。

只是补充一点,我尝试了更多类似大小的图像文件并收到相同的错误。 第一个图像文件是 4.2MB,第二个是 5MB。为什么文件大小会阻止它上传?

当使用 $_FILES['files']['error'][$key]; - 它返回 1。

这是我在使用 print_r 时得到的:

Array ( [files] => Array ( [name] => Array ( [0] => DSCN0354.JPG ) [type] => Array ( [0] => ) [tmp_name] => Array ( [0] => ) [error] => Array ( [0] => 1 ) [size] => Array ( [0] => 0 ) ) )

【问题讨论】:

  • 你上传的图片有多大,失败了?
  • 您是否考虑过使用print_r($_FILES) 来查看阵列中的实际内容?如果没有,请执行此操作并将输出添加到您的问题中。
  • 我猜你没有在这里粘贴完整的代码。
  • Why would the file size stop it uploading? 因为php设置限制了它。查找upload_max_filesize 和post_max_size。而且 4.2 MB 与 5 MB 不同

标签: php image-processing


【解决方案1】:

让我们看看已知的:

  • 可以上传.jpg、.gif、.png 没问题
  • 您在上传特定 .jpg 图片时遇到问题

这些已知信息使我相信问题图像对于您的 php.ini 设置中当前允许的upload_max_filesize 来说太大了:

$ -> php -i | fgrep -i size

upload_max_filesize => 2M => 2M

尝试将大小增加到 6M 或 8M,您的更大图像应该可以工作。

【讨论】:

    【解决方案2】:

    如果你添加一些错误处理,你可以使用error code轻松查看问题所在:

    foreach($_FILES['files']['tmp_name'] as $key => $tmp_name)
    {
      if ($_FILES['files']['error'][$key] == UPLOAD_ERR_OK)
      {
         // good
      }
      else
      {
         // not good, see messages on http://www.php.net/manual/en/features.file-upload.errors.php
      }
    }
    

    【讨论】:

    • 您可能希望在比较中使用===。如果由于某种原因根本没有设置该值,您会认为一切正常。 (UPLOAD_ERR_OK = 0 == NULL)
    • @Michael Mior 你说得对,我只是盲目地从 php 手册中复制了这个,但我自己可能会使用===。我想知道为什么他们没有在 php 手册中,但请参阅 php.net/manual/en/function.move-uploaded-file.php ...
    • 嗯,奇怪。我以为我以前在手册的其他地方看到过===
    【解决方案3】:

    您必须在访问 tmp_name 之前检查 $_FILES['error'][$key] - 文件未上传,可能是因为文件太大或其他原因(您将通过 'error' 字段的值找到)

    【讨论】:

      【解决方案4】:

      Also when using $_FILES['files']['error'][$key]; - it returns 1.

      错误 1:上传的文件超过 php.ini 中的upload_max_filesize 指令。

      (见http://www.php.net/manual/en/features.file-upload.errors.php

      【讨论】:

        【解决方案5】:

        错误消息指出提供的文件名是空的。

        您正在使用:

        $size2 = getimagesize($temp);
        

        你从哪里得到 $temp?是 foreach() 吗,如果是,你应该使用 $tmp_name 来代替。

        【讨论】:

        • 它在那里:$temp = $_FILES['files']['tmp_name'][$key]; //location of file。此外,该代码适用于该特定图像
        猜你喜欢
        • 2012-05-14
        • 1970-01-01
        • 2016-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-26
        • 1970-01-01
        相关资源
        最近更新 更多