0

当我尝试在 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');
            }
        }

我几乎尝试了所有方法,如果有人知道发生了什么,我将不胜感激。

谢谢!

4

2 回答 2

1

将 multipart formdata 添加到标签并尝试

enctype="multipart/form-data"
于 2012-08-02T17:10:54.450 回答
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',而不是'.zip'。我不确定您为什么要尝试在此处修改扩展名,而不是使用$ext您已经创建的扩展名?我建议您在创建它时使用小写字母$ext,我认为您应该很好吗?

于 2012-08-02T17:56:02.103 回答