1

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

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


------WebKitFormBoundaryrHOYostaC2KnUDlD--

但这对于 txt 或 pdf 文件:

------WebKitFormBoundaryc1RJOtSOpYKAZiBz--

这是表单和脚本(功能是避免用户单击“提交”,这些都很好用):

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 没有任何区别谢谢!!!

4

2 回答 2

0

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

.pdf    application/pdf
.doc    application/msword
于 2012-12-26T23:14:25.463 回答
0

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

于 2012-12-27T07:02:40.770 回答