PHP - 上传的文件为空

时间:2012-08-02 16:40:41

标签: php file upload null

当我尝试用PHP上传文件时,它不会让我。在这里和那里转储变量之后,我得到了这个:

Invalid file!
!
NULL
24M
32MNULL 

由此产生:

echo "Invalid file!"; //So, we screwed up...
echo "<br />" . $ext . "!<br />";
echo var_dump($_FILES['fileupl']);
echo "<br />" . ini_get('upload_max_filesize');
echo "<br />" . ini_get('post_max_size');
echo "<br />" . var_dump($_FILES['error']);

我上传的任何文件都会返回该文件 我正在尝试上传任何文件:.zip,.jar,.png,用于一个小型的Minecraft文件共享平台。


变量:

$title = $_POST['title'];
    $desc = $_POST['desc'];

    $type = $_GET['type'];

    //File stuff
    $name = $_FILES['fileupl']['name'];
    $rname = $_FILES['fileupl']['tmp_name'];
    $ftype = $_FILES['fileupl']['type'];
    $size = $_FILES['fileupl']['size'];
    $ext = strrchr($rname, '.');
    $allowedExtensions = array(".zip", ".jar", ".png");

代码:

if (!in_array(end(explode(".",
            strtolower($name))),
            $allowedExtensions)) { 

            echo "Invalid file!"; //dafuq, how'd you screw that up man?
            echo "<br />" . $ext . "!<br />";
            echo var_dump($_FILES['fileupl']);
            echo "<br />" . ini_get('upload_max_filesize');
            echo "<br />" . ini_get('post_max_size');
            echo "<br />" . var_dump($_FILES['error']);
        }
        else
        {
            $uploaddir = './content/';

            //Begin file shizzle

            if (is_uploaded_file($_FILES['fileupl']['tmp_name'])) //Is the file uploaded?
            {
                $uploadfile = $uploaddir . basename($name);
                echo $name . " Uploaded successfully.";
                if (move_uploaded_file($rname, $uploadfile))
                {
                    echo $name . " (" . display_filesize($size) . ") Successfully uploaded!";
                    $q = mysql_query("INSERT INTO `content`(`type`, `creator`, `title`, `description`, `filename`) VALUES('" . mysql_real_escape_string($type) . "', '" . $username . "', '" . mysql_real_escape_string($title) . "', '" . mysql_real_escape_string($desc) . "', 'Anon')");
                    if (!$q)
                    {
                        echo "mySQL execution failed!";
                    }
                    else
                    {
                        echo "Uploaded to database!";
                    }
                }
                else
                {
                    echo "File could not be moved!";
                }

            }
            else
            {
                echo "File could not be uploaded!";
                echo var_dump($_FILES['fileupl']);
                echo "<br />" . ini_get('upload_max_filesize');
                echo "<br />" . ini_get('post_max_size');
            }
        }

我已经尝试过所有事情,如果有人知道发生了什么,我会非常感激。

谢谢!

2 个答案:

答案 0 :(得分:1)

将多部分表单数据添加到标记并尝试

enctype="multipart/form-data"

答案 1 :(得分:0)

正如您在修改表单后提到的那样,您现在看到:(我为了清晰起见而进行了编辑)

Invalid file! 
!    
array(5) { ["name"]=> string(17) "EasyMinecraft.zip" ["type"]=> string(15) "application/zip" ["tmp_name"]=> string(14) "/tmp/php87jMDe" ["error"]=> int(0) ["size"]=> int(4325565) } 
24M 
32MNULL 
Array ( [fileupl] => Array ( [name] => EasyMinecraft.zip [type] => application/zip [tmp_name] => /tmp/php87jMDe [error] => 0 [size] => 4325565 ) )

这表明$ ext是空的。变量赋值的相关行是:

$rname = $_FILES['fileupl']['tmp_name'];
$ext = strrchr($rname, '.');

那么,$_FILES['fileupl']['tmp_name']是什么?您的输出显示为 / tmp / php87jMDe ,当您尝试在'。'之后找到所有内容时,这显然不起作用。在其中,因为没有句号。您可能希望更改第一行以使用实际名称,而不是tmp_name,例如:

$rname = $_FILES['fileupl']['name'];

这个条件也会出错:

if (!in_array(end(explode(".",
            strtolower($name))),
            $allowedExtensions)) { 

请注意end(explode(".", strtolower($name)))只会给你'拉链',而不是'.zip'。我不确定您为什么要在此处重新定位扩展程序,而不是使用您已创建的$ext?当你创建它时,我建议使用下套$ext,我觉得你应该好好去那么做?