开发者

Pass $_FILES into class for processing

开发者 https://www.devze.com 2023-03-30 05:31 出处:网络
I currently have an enquiry submission form that can only be completed by registered users. Part of the form allows the user to upload a file to be attached to their enquiry.

I currently have an enquiry submission form that can only be completed by registered users.

Part of the form allows the user to upload a file to be attached to their enquiry.

What I am attempting to do is pass the $_FILES variable to a class file for processing (using class.upload.php) as I want the file name to be in the following format:

enquiryID_userID_fileName.ext

Is it possible to pass the form $_POST data to a class along with the $_FILES data?

An example of what I am attempting to achieve is the following:

if (isset($_POST['submit_enquiry'])) {
    $enquiry = new Enquiry();
    $enquiry->newEnquiry($_POST, $_FILES);
}

Then in the Enquiry Class:

class Enquiry {

    private function _processFiles($_FILES, $caseID) {
        ...
    }

    private function _processForm($_POST) {
        ...
    }

    public function newEnquiry($_POST, $_FILES) {
        $caseID = $this->_processForm($_POST); // returns caseID
        $this->_processFiles($_FILES, $caseID);
    }
}

A print of $_FILES gives me the following:

Array
(
    [file] => Array
        (
            [name] => Array
                (
                    [0] => Blue hills.jpg
                    [1] => Sunset.jpg
                    [2] => 
                )

            [type] => Array
                (
                    [0] => image/jpeg
                    [1] => image/jpeg
                    [2] => 
                )

            [tmp_name] => Array
                (
                    [0] => /tmp/phpwyLp86
                    [1] => /tmp/phpKJa4iw
                    [2] => 
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 4
                )

            [size] => Array
                (
                    [0] => 28521
                    [1] => 71189
                    [2] => 0
                )

        )

)

When attempting to process the file it's almost as if the tmp file has been lost as it can't process the file to be uploaded. Are there any obvious issues I h开发者_JAVA技巧ave missed attempting to implement this solution?


The upload tmp files will be deleted when your script finishes. As long as it didn't finish, the files are still there.

However, if you move them or if the form goes accross multiple requests (e.g. multi-step form, validation that requires re-submission), you need to build your own tmp files.


You just need to prepend the post values to the file name:

foreach($_FILES as $file){
   $file['name']=$_POST['enquiryID'].'_'.$_POST['userID'].'_'.$file['name'];
}

// do upload

And if you only have a few $_POST inputs, save them as sanitized variables and send them individually through your function as parameters (as Phil said).

0

精彩评论

暂无评论...
验证码 换一张
取 消