表格不发送图像以外的文件

时间:2012-12-26 23:08:44

标签: php forms file-upload

我正在尝试使用php上传文件,我正在复制和重命名实际工作的其他实例中的文件(上传图片)。但由于某种原因,表单没有传递(POST)任何不是图像的文件: - / 因此,在简历中,我得到了图像文件的这个(Google)“请求有效负载”:

------WebKitFormBoundaryrHOYostaC2KnUDlD
Content-Disposition: form-data; name="uploaded_file[]"; filename="image.jpg"
Content-Type: image/jpeg


------WebKitFormBoundaryrHOYostaC2KnUDlD--

但这适用于txt或pdf文件:

------WebKitFormBoundaryc1RJOtSOpYKAZiBz--

这是表单和脚本(函数是为了避免用户点击'Submit',那些工作正常):

echo '
<script language="JavaScript" type="text/javascript">
function HandleBrowseClick()
{
    var fileinput = document.getElementById("uploaded_file");
    fileinput.click();
}
function Handlechange()
{
var fileinput = document.getElementById("uploaded_file");
var textinput = document.getElementById("filename");
textinput.value = fileinput.value;
}
</script>';

echo '    
<form enctype="multipart/form-data" target="_blank" name="send_file" id="send_file" method="post" action="file_upload.php">

<input type="file" class="hide button" id="uploaded_file" name="uploaded_file" onChange="Handlechange();"/>

<button type="submit" id="btn">Upload!</button>
</form>';

echo '
<div onclick="HandleBrowseClick();" id="fakeBrowse" >Load a file</div>
<input type="text" id="filename" size="50" readonly="true" />
   ';

所以,由于它没有传递任何内容,在我的file_upload.php中,我得到了“ERROR: Please browse for a file before clicking the upload button.”或“Invalid argument supplied for foreach()”(如果我预期数组)错误。
我尝试使用application/x-www-form-urlencoded允许相同的结果。
现在对于那些在没有问号的情况下生气的人:为什么表单适用于图像而不是其他类型的文件?我错了什么?
以下是file_upload.php的前几行(我认为这不是必要的,但你永远不知道):

$target = "../files/temp/"; 

foreach ($_FILES["uploaded_file"]["error"] as $key => $error) {
    if ($error != UPLOAD_ERR_OK) { echo "error"; die;}

$fileName = $target . $_FILES["uploaded_file"]["name"][$key];  // The file name
$fileTmpLoc = $_FILES["uploaded_file"]["tmp_name"][$key];     // File in the PHP tmp folder
$fileType = $_FILES["uploaded_file"]["type"][$key];           // The type of file it is
$fileSize = $_FILES["uploaded_file"]["size"][$key];           // File size in bytes
$fileErrorMsg = $_FILES["uploaded_file"]["error"][$key];      // 0 for false... and 1 for true last $key!!!

$fileName = preg_replace('#[^a-z.0-9]#i', '', $fileName); // filter the $filename
$fileName = strtolower($fileName);
$kaboom = explode(".", $fileName);    // Split file name into an array using the dot
$fileExt = end($kaboom);              // Now target the last array element to get the file extension

if (!$fileTmpLoc) { // if file not chosen
    echo "ERROR: Please browse for a file before clicking the upload button.";
    exit();
    } 
else if ($fileErrorMsg == 1) { // if file upload error key is equal to 1
    echo "ERROR: An error occurred while processing the file. Try again.";
    exit();
}

最后,还有一些js:

    if (window.FormData) {
        formdata = new FormData();
        document.getElementById("btn").style.display = "none";

    }

    input.addEventListener("change", function (evt) {
        document.getElementById("response").innerHTML = "Loading . . ."

        var i = 0, len = this.files.length, img, reader, file;

        for ( ; i < len; i++ ) {    
            file = this.files[i];
            if (!!file.type.match(/image.*/)) {
                if (formdata) {
                    formdata.append("uploaded_file[]", file);
                }
            }   
        }

        if (formdata) {
            $.ajax({
                url: "file_upload.php",
                type: "POST",
                data:  formdata,
                processData: false,
                contentType: false
            }).done(function (res) {
                document.getElementById("response").innerHTML = res;
                if ( window.FileReader ) {
                    reader = new FileReader();
                    reader.onloadend = function (e) { 
                        showUploadedItem(e.target.result, file.fileName);
                    };
                    reader.readAsDataURL(file);
                }
                });
        }
    }, false);

更改contentType没有任何差异     致谢!!!

2 个答案:

答案 0 :(得分:0)

您必须为文件定义MIME类型。例如

.pdf    application/pdf
.doc    application/msword

答案 1 :(得分:0)

好的,我的坏。 js文件有一个图像过滤器。我删除后立即开始工作。