PHP处理文件上传 [英] PHP Handling file uploads

查看:83
本文介绍了PHP处理文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个上传字段的表单.每个字段仅允许上传一个文件,并且它们都应用了适当的javascript验证.然后,我到了我的Ajax函数中收集表单数据并将其发送到PHP的地步.此刻我正在做这样的事情

I have a form which has two upload fields. Each field only allows one file to be uploaded, and they both have the appropriate javascript validation applied. I then come to the point where within my ajax function, I collect the form data, and send it to PHP. At the moment I am doing something like this

submitHandler: function (form) {
    formData = new FormData(),
    fileOne    = $(form).find('[name="fileOne"]')[0].files,
    fileTwo    = $(form).find('[name="fileTwo"]')[0].files;

    if(fileOne.length != 0) {
        $.each(fileOne, function(i, file) {
            formData.append('fileOne-' + i, file);
        });
    }

    if(fileTwo.length != 0) {
        $.each(fileTwo, function(i, file) {
            formData.append('fileTwo-' + i, file);
        });
    }

    $.ajax({
        type: "POST",
        url: "process.php",
        dataType: "json",
        data: formData,
        mimeType: "multipart/form-data",
        contentType: false,
        processData: false
    });
    return false;
}

foreach循环可能不是必需的,因为每个循环只能有一个文件,但是如果没有此循环,我找不到一种方法.

The foreach loops are probably not necessary as each one can only have one file, but I cant find a way to do it without this loop.

无论如何,因此此数据将发送到我的PHP文件.这很重要,不需要两个文件,只需要一个.但是,如果需要,也可以上传2个文件.因此,PHP可能会接收一个或两个文件.

Anyways, so this data is sent to my PHP file. This is important, both files are not required, only one is required. However, 2 files can be uploaded if they want too. So PHP may receive either one or two files.

在我的PHP中,我需要上传文件.目前我正在这样做

Within my PHP, I need to upload the files. Currently I am doing this

 if (!empty($_FILES)) {
    foreach($_FILES as $file) {
        $upload = UPLOAD\Upload::factory('/temp');

        $upload->file($file);

        $upload->set_max_file_size(2);

        $upload->set_allowed_mime_types(
            array(
                'image/bmp',
                'image/jpeg',
                'image/pjpeg',
                'image/png',
                'image/tiff',
                'application/pdf'
            )
        );

        $results = $upload->upload();

        if(!$upload->get_errors()) {
            array_push($fileNames, $results['full_path']);
        }
        else {
            var_dump("Bad File");
        }
    }
} else {
    $errors['file'] = '- Please upload a file';
}

我将文件名收集到一个数组中,因为文件上传后,我需要获取文件并将其放入文档中.

I collect the filenames into an array because after files are uploaded, I need to get the files and put them into a document.

无论如何,我现在的主要问题是这个.说$ _FILES包含两个文件.它执行第一个循环,一切正常,文件已上传.然后执行第二个循环,上传出现问题,并返回错误.如果其中一个文件上传不正确,我不想继续将其上传的文件放入文档中.如果其中一个文件失败,则整个过程应停止,并向用户返回错误.

Anyways, my main question for now is this. Say $_FILES contains both files. It does the first loop, everything is ok, the file is uploaded. It then does the second loop, something goes wrong with the upload, and an error is returned. I do not want to continue putting their uploaded files into a document if one of the files did not upload correctly. If one of the files fails, the whole process should stop, and an error returned to the user.

我将如何检查是否所有上传的文件都已成功上传?我显然有以下内容可以告诉我上传是否有错误

How would I check if all the files that are uploaded were uploaded without a problem? I obviously have the following which tells me if there is an error in the upload

$upload->get_errors()

但是因为这是循环的,所以我不确定如何最好地利用它.

But because this is in the loop I am not sure how to best make use of it.

无论如何,我的主要目标是这个.文件已上传,我收集了文件名.如果一切顺利,则将这些文件名传递给函数,以便可以将其附加到文档中.仅当上传的一个或两个文件都正确上传时,这种情况才会发生.

Anyway, my main aim is this. The files are uploaded and I collect the filenames. If all goes well, I then pass these filenames to a function so they can be appended to a document. This should only happen though if the one or two files uploaded are both uploaded properly.

希望我对自己的解释足够好,真的希望能找到一些有关如何最好地解决此问题的指导.

Hope I have explained myself good enough, really looking for a bit of guidance how to best handle this.

谢谢

推荐答案

您没有说明正在使用的库....看起来像这样,但是我不确定:

You didn't state what library you are using....looks like this one but I"m not sure :

https://github.com/aivis/PHP文件上传类/blob/master/upload.php

看起来像这样的东西会起作用.

Looks like something like this will work..

if ($results["status"] == false){ break; }

这篇关于PHP处理文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆