Qlic's avatar
Level 18

Storage saves files as 23 bytes files

Hi Guys,

I am trying to get the storage to work, but all the files i upload are only 23bytes in size and thus unusable. Is there something i'm doing wrong here?

if (Request::hasFile('attachments'))
{
    foreach (Request::file('attachments') as $file)
    {
        if (substr(File::mimeType($file), 0, 5) == 'image')
        {
            Storage::disk('local')->put('project/'. $id .'/'. time() .'.'. $file->getClientOriginalExtension() , $file);
            // Make thumbnail later on
        }
        else
        {
            Storage::disk('local')->put('project/'. $id .'/'. time() .'.'. $file->getClientOriginalExtension(), $file);
        }
    }
}
0 likes
3 replies
pmall's avatar
pmall
Best Answer
Level 56

I think you are only saving the tmp file path here. Try :

Storage::disk('local')->put('project/'. $id .'/'. time() .'.'. $file->getClientOriginalExtension() , file_get_contents($file));

Also did you see this is the same code in the two parts of the if :D

Qlic's avatar
Level 18

Great that did the trick, btw i changed it so it does not have duplicate code:

if (Request::hasFile('attachments'))
{
    foreach (Request::file('attachments') as $file)
    {
        Storage::disk('local')->put('project/'. $id .'/'. time() .'.'. $file->getClientOriginalExtension() , file_get_contents($file));

        if (substr(File::mimeType($file), 0, 5) == 'image')
        {
            // Make thumbnail later on
        }
    }
}
pmall's avatar

you can also just do

$uploaded_file->move($path);

It move the file on the path of the default filesystem.

Please or to participate in this conversation.