欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

In Laravel 5.1 uploaded file stores with temporary php name instead of original name #31

shiping1 的头像

In Laravel 5.1 uploaded file stores with temporary php name instead of original name #31

 Closed
alexglue opened this issue on 28 Jul 2015 · 8 comments

3 participants

@alexglue@freekmurze@ziming
@alexglue
 
        if($request->hasFile('file')){
            $model->addMedia($request->file('file'));
        }
//vendor/spatie/laravel-medialibrary/src/HasMediaTrait.php:53
$media->name = pathinfo($file, PATHINFO_FILENAME);

So i've got "phpCYQ7ed" (temporary php filename) instead of original uploaded file name.
Or maybe i should to store uploaded file separately before attach it to my model?
Don't u think it'll be better to integrate upload file moving-renaming in your package also?

 
@freekmurze
 
Spatie member

You should get the client original name. Here's an example.

In a next version of the package I'll make this easier.

 
 
@ziming
 

Hi @alexglue

The way I currently handle it is this

// I can't find a rename method on UploadedFile class hence I used the move method to move on the 
// same directory while providing a new filename, then I add the media to the object.
$file = $file->move('.', $file->getClientOriginalName());
$object->addMedia($file);

Hope it helps for the moment!

 
@freekmurze
 
Spatie member

You could also rename the media after adding the file:

$media = $model->addMedia($request->file('file'));
$media->name = $file->getClientOriginalName();
$media->save();
 
@ziming
 

Hi @freekmurze from my own attempt earlier doing that changes the name at the database row, but not the actual file name of the document.

 
@freekmurze
 
Spatie member

@ziming that's true...

I'll add method for renaming the file soon...

 
@ziming
 

That got me stuck for a little long previously hah. Thank you.

 
@freekmurze
 
Spatie member

New in version 2.2.3: when renaming file_name on a media object, the underlying file will be renamed as well.

$mediaObject->file_name = 'newFileName.jpg'
$mediaObject->save(); // This will also rename the file on the filesystem.
 
@freekmurze
 
Spatie member

Fyi, working on allowing uploads to be passed directly to the addMedia-function: c43e577

Still have to test it properly, ...



来自 https://github.com/spatie/laravel-medialibrary/issues/31


普通分类: