【问题标题】:Uploading Image not Working上传图片不工作
【发布时间】:2009-07-14 04:47:43
【问题描述】:

我正在尝试编写一个可以上传多个文件的 PHP 脚本。

for($i=0;$i<count($_FILES['uploadimg']['name']);$i++){
    $name = $_FILES['uploadimg']['name'][$i];
    $type = $_FILES['uploadimg']['type'][$i];
    $filepath = $_FILES['uploadimg']['tmp_name'][$i];
    $size = getimagesize($filepath);
    $img = file_get_contents($filepath);

    //insert into database
}

问题在于变量没有按应有的方式填充。

当我上传 jpeg "image.jpg" 时,我发现变量具有以下值:(通过将数据库代码替换为 echos 和 var_exports)

$name = 'image.jpg'; // good
$type = ''; // not good
$filepath = ''; // not good
$size = false; // not good
$img = false; // not good

我应该注意,我意识到 $size$img 依赖于 $filepath 是一个有效的文件路径。

谁能提供一些关于出了什么问题或我遗漏了什么的见解?我已经玩了一天的代码,但无法提出解决方案。

更新:

print_r($_FILES) 3 张图片返回:

Array (
    [uploadimg] => Array (
        [name] => Array (
            [0] => test1.jpg
            [1] => test2.jpg
            [2] => test3.jpg
        )
        [type] => Array (
            [0] => image/jpeg
            [1] =>
            [2] => image/jpeg
        )
        [tmp_name] => Array (
            [0] => /tmp/phpkC6f2F
            [1] =>
            [2] => /tmp/phpgFrPl8
        )
        [error] => Array (
            [0] => 0
            [1] => 1
            [2] => 0
        )
        [size] => Array (
            [0] => 238906
            [1] => 0
            [2] => 237308
        )
    )
)

我可以假设这意味着图像太大了吗?

【问题讨论】:

    标签: php file-upload


    【解决方案1】:

    This question / answer 可能会有所帮助。

    编辑 - 看起来像是文件大小问题。两个工作文件的大小都约为 1.8 Mb,默认情况下 PHP 的文件上传限制为 2 Mb。我只能从 Windows 的角度给你建议,但我会在 PHP.ini 中寻找增加 upload_max_filesize或许还有post_max_size settings

    Edit 2 - PHP.net 上有一个部分lists the error codes for uploading。你得到一个 1 是:

    UPLOAD_ERR_INI_SIZE
    
    Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.
    

    这与我在第一次编辑中所说的相符。

    【讨论】:

    • 谢谢,这解释了很多,尤其是结合罗斯提出的转储建议以及我现在发布的结果。
    【解决方案2】:

    所有这些文件都是同一类型吗?上传几个文件时,你能给我们一个var_dump 的变量吗?所有上传的变量是空的还是只有一些?

    例如这段代码:

    <?php
    print_r($_FILES);
    ?>
    
    <html><body>
    <form action="test.php" method="post" enctype="multipart/form-data">
        <input name="upload[]" type="file" /><br />
        <input name="upload[]" type="file" /><br />
        <input name="upload[]" type="file" /><br />
        <input type="submit" value="Upload" />    
    </form>
    </body></html>
    

    返回这个输出:

    Array (
        [upload] => Array (
            [name] => Array (
                [0] => IMG_0005.jpg
                [1] => IMG_0249.jpg
                [2] => IMG_0007.JPG
            )
            (...snip...)
            [size] => Array ( 
                [0] => 1776529 
                [1] => 1902522 
                [2] => 798008 
            ) 
        ) 
    )
    

    记得检查每个文件的$_FILES['name']['error']

    【讨论】:

    • 我已经发布了结果,我相信这表明问题出在文件大小上。
    猜你喜欢
    • 2016-07-14
    • 2017-01-01
    • 2010-11-19
    • 2016-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-25
    相关资源
    最近更新 更多