phpflightphp

How to upload files with flightphp


Please give me a example to upload files in flightphp framework. i coded as ...

Flight::route('POST /regSubTask', function () {
 $request = Flight::request();
 $file = $request->files['imagefile']; 
 var_dump($file);
});

But return as null

$file = Flight::request()->data['imagefile'];

this one return name only


Solution

  • From the Flight Docs page, here's how you access it as you've already tried:

    $uploadedFile = Flight::request()->files['myFile'];
    

    In this example, $uploadedFile is basically just like $_FILES['myFile']. So the example in the PHP docs would actually work in this case as well.

    <?php
    $uploads_dir = '/uploads';
    foreach ($uploadedFile["error"] as $key => $error) {
        if ($error == UPLOAD_ERR_OK) {
            $tmp_name = $uploadedFile["tmp_name"][$key];
            // basename() may prevent filesystem traversal attacks;
            // further validation/sanitation of the filename may be appropriate
            $name = basename($uploadedFile["name"][$key]);
            move_uploaded_file($tmp_name, "$uploads_dir/$name");
        }
    }
    ?>
    

    As of right now, Flight doesn't have a built in way to handle file uploads, but I'll make a note of it for a future feature (I currently maintain Flight).