为什么$ _POST不能识别上传的某些文件?

时间:2013-09-17 14:57:32

标签: php html file-upload upload

你不会相信这个错,我不知道发生了什么。当我尝试上传文件时,其中一些人拒绝上传。

<html>
<body>
<?php
var_dump($_POST);
if ($_POST['fileadd']){
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    print_r($_POST); echo "<br>"; print_r($_FILES);
}
?>

        <form action="" method="POST"
        enctype="multipart/form-data">
        <label for="file">Filename:</label>
        <input type="file" name="file" id="file" class="bigtext" style="width: 80%;"> 
        <br>
        <input type="submit" class="bigbutton" id="showloader" name="fileadd" value="Upload the song" />
        </form>


</body>
</html>

Here it is in action. 问题是当它说 array 0 时它不应该说。

另外,当我选择某些文件时,$_POST数组是nil/zero,它至少应该包含fileadd,作为提交按钮的名称?< / p>

2 个答案:

答案 0 :(得分:2)

您只是假设您的代码是完美的。不是。它假定文件上传总是成功,并且不允许失败的可能性。你需要有更多的东西:

if ($_FILES['file']['error'] !== UPLOAD_ERR_OK) {
   die("File upload failed with error code #" . $_FILES['file']['error']);
}

错误代码在此处定义:http://www.php.net/manual/en/features.file-upload.errors.php

答案 1 :(得分:2)

在看到完整的代码并与OP交谈以找到解决问题的方法之后,将问题归结为以下几点:

此:

if ($_POST['fileadd']){

需要改为:

if(isset($_POST['fileadd']))

以检查是否已设置“提交”按钮。

此外,根据尝试上传的文件大小,上传最大尺寸设置得太低,使用.htaccess中的以下内容来增加它。

php_value upload_max_filesize 48M
php_value post_max_size 48M