PHP上传的文件存在于TMP名称中,但实际上它没有上传

时间:2016-03-16 06:23:20

标签: php apache

我用PHP上传了一个文件,它在数组$ _FILES中,tmp_name用他的名字填充,但实际上文件还没有上传,为什么会这样呢?如果tmp_name已填满,这是否意味着它应该被上传?因为我甚至把文件夹设置为0777,仍然无法上传,在phpinfo(); - 一切都在......请帮忙

foreach($_FILES as $file)
{

    foreach($file['tmp_name']['new'] as $name) {

        echo '<br />'.$name.'<br />';
        if(is_uploaded_file($name))
        {
            echo '1';
        }
        else
        {
            echo '0'; // shows this
        }
        //$res = move_uploaded_file($name, "/uploads");
        //echo $res;
    }
    exit;
}
//exit;

is_uploaded_file - 返回false

Array
(
    [name] => Array
        (
            [new] => Array
                (
                    [prikrepit_fajl] => 
                    [prikrepit_fajl1] => 
                    [prikrepit_fajl3] => ava.png
                    [prikrepit_fajl333] => ava.png
                    [prikrepit_fajl55555555] => 
                )

        )

    [type] => Array
        (
            [new] => Array
                (
                    [prikrepit_fajl] => 
                    [prikrepit_fajl1] => 
                    [prikrepit_fajl3] => image/png
                    [prikrepit_fajl333] => image/png
                    [prikrepit_fajl55555555] => 
                )

        )

    [tmp_name] => Array
        (
            [new] => Array
                (
                    [prikrepit_fajl] => 
                    [prikrepit_fajl1] => 
                    [prikrepit_fajl3] => /home/users1/v/vizitka77/tmp/phppOtsIp
                    [prikrepit_fajl333] => /home/users1/v/vizitka77/tmp/phpgY6RQ9
                    [prikrepit_fajl55555555] => 
                )

        )

    [error] => Array
        (
            [new] => Array
                (
                    [prikrepit_fajl] => 4
                    [prikrepit_fajl1] => 4
                    [prikrepit_fajl3] => 0
                    [prikrepit_fajl333] => 0
                    [prikrepit_fajl55555555] => 4
                )

        )

    [size] => Array
        (
            [new] => Array
                (
                    [prikrepit_fajl] => 0
                    [prikrepit_fajl1] => 0
                    [prikrepit_fajl3] => 25352
                    [prikrepit_fajl333] => 25352
                    [prikrepit_fajl55555555] => 0
                )

        )

)

2 个答案:

答案 0 :(得分:0)

您应该阅读:http://php.net/manual/en/function.is-uploaded-file.php

而且:http://php.net/manual/en/features.file-upload.php

您似乎错误地访问了$ _FILES。

foreach($_FILES['tmp_name']['new'] as $inputName => $file)
{
    // $inputName is the name attribute from the HTML form
    echo '<br />'.$file.'<br />';
    if( is_uploaded_file($file) )
    {
        echo '1';
    }
    else
    {
        echo '0';
    }
    //$file = umiImageFile::upload('uploads',$name,'uploads','new');
    //$res = move_uploaded_file($name, "/uploads");
    //echo $res;
}

答案 1 :(得分:0)

需要指出的一件重要事项是:如果您在请求期间未保存上传的文件,那么当请求终止时,PHP会将其删除,因此文件将会消失。

其次,您正在错误地循环数组。您应该尝试使用for循环:

for ($i = 0; $i < sizeof($_FILES['new']['tmp_name']); ++$i) {
    $name = $_FILES['new']['name'][$i];
    $path = $_FILES['new']['tmp_name'][$i];
    $err  = $_FILES['new']['error'][$i];

    if ($err != UPLOAD_ERR_OK) {
        // check error code
        // report error
        continue;
    }

    if (is_uploaded_file($path)) {
        // do something with the file
    }
}

编辑:在您的评论代码中,move_uploaded_file($name, "/uploads");不是移动文件的正确语法。

看起来应该更像:

move_uploaded_file($path, "uploads/{$name}");

注意:如果您没有检查文件内容和扩展名,则上述内容很危险。小心不要允许使用.php或其他可执行扩展名上传文件。