简体   繁体   中英

PHP Handling file uploads

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;
}

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.

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.

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.

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.

Thanks

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

https://github.com/aivis/PHP-file-upload-class/blob/master/upload.php

Looks like something like this will work..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM