4

I am trying to upload files (they can be any type), and I have a problem uploading file with certain way.

I am able to upload a file correctly using $request->file('file_name') and Storage::disk($disk)->put($path, $file);. However, $file parameter can only be through $request->file('file_name').

However, because of the way I want to upload multiple orders with multiple files like below:

Controller

foreach ( $filesArray as $key => $files ) {

    $path = 'order/'.$order->id;

    if ( isset($files[$i]) && !empty($files[$i]) ) {
        for ( $j = 0; $j < count($files[$i]); $j++ ) {
            $uploadedFile = FileHelper::upload($files[$i][$j], $path);

            $orderFile = [];
            $orderFile['order_id'] = $order->id;
            $orderFile['file_id'] = $uploadedFile->id;
            OrderFileModel::create($orderFile);
        }
   }
}

FileHelper

static public function upload($file, $path, $disk = 's3')
{
    $fileOrm = new FileModel;
    $fileOrm->size = $file->getSize();
    $fileOrm->extension = $file->getExtension();
    $fileOrm->bucket_name = self::$BUCKET_NAME;
    $fileOrm->type = self::getFileType($file->getExtension());
    $fileOrm->key = Storage::disk($disk)->put($path, $file);
    $fileOrm->created_time = now();
    $fileOrm->save();

    return $fileOrm;
}

I've also attached images where I see the difference. One with $request->file('file_name') and the other with just $request->file_name which is blob type.

able to upload

not able to upload: with error fstat() expects parameter 1 to be resource, object given

The image below would return error saying fstat() expects parameter 1 to be resource, object given

How could I solve this problem?

Any advice or suggestion would be appreciated. Thank you.

3
  • are you able to refactor the fileHelper::upload method to include the request object? Or, can you call the request() helper method in the upload method? If so, you can use laravel's built in file handler $path = $request->file('avatar')->store( 'avatars/'.$request->user()->id, 's3' ); ? Otherwise you'll need to use something like file_get_contents($files[$i][$j]) to get the contents of the temp file and handle it manually. Commented Mar 5, 2018 at 16:39
  • @j.steelman thanks for the comment. How would I use file_get_contents to get the contents? According to the doc, file_get_contents required the name of the file in the first parameter, however $files[$i][$j] is an object. Commented Mar 6, 2018 at 0:46
  • @smchae User this package and upload any number of files with any type easily in one line of code. Commented Mar 9, 2018 at 21:05

4 Answers 4

3
+50

Do you get your file list to use $request->files? If you do, change to $request->allFiles(). This method will convert File object to UploadedFile object.

Sign up to request clarification or add additional context in comments.

Comments

3

Actually just put an array in your input file like this

<input type="file" name="files[]">
<input type="file" name="files[]">
<input type="file" name="files[]">
<input type="file" name="files[]">

NOTE: you need to enctype="multipart/form-data" enabled in your form.

Then in your controller, you can loop the input file by doing such

foreach($request->file('files') as $index => $file){
    // do uploading like what you are doing in your single file.
}

1 Comment

I am trying to several sequences of uploading multiple files. So, I have to get those files in array form, then loop through that array and access each file for each action(order creation)
0

Try:

$this->validate($request, [
    'files.*' => 'file|mimes:...'
]);

foreach ($request->files as $file) {
    $fileName = (string)Str::uuid() . '.' . $file->getClientOriginalExtension();
    try {
        if (\Storage::disk('s3')->put($fileName, file_get_contents($file))) {
            FileModel::create([
                'size'         => '...',
                'extension'    => '...',
                'bucket_name'  => '...',
                'type'         => '...',
                'key'          => '...',
                'created_time' => '...'
            ]);
        }
    } catch (\Exception $e) {
        // ...
    }
}

Comments

0

You might have been lucky before that some type casting on your $image object made a string out of it, I guess a simple chnage of your last line to

$disk->put($path, $file->__toString());

will fix the problem and is safer anyway as the "put" method officially only accepts strings (and looking at the implmentation also php resources). That should keep you compatible to changes in the long run.

1 Comment

can u able to print it ? $file->__toString() ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.