简体   繁体   中英

Can't upload file in laravel 5

I am trying to upload an image in Laravel 5.

My environment is Windows. I am using php artisan serve to run Laravel.

Error:

NotWritableException in Image.php line 143:
Can't write image data to path (D:\WORK\WebSite\iStore\public\assets/images/products/2015-07-05-08:52:33-6aa3df83gw1dtd5qki2oij.jpg)

Controller:

$image = Input::file('image');
$filename = date('Y-m-d-H:i:s') . "-" . $image->getClientOriginalName();
$path = public_path('assets/images/products/' . $filename);

// echo dd(is_writable(public_path('assets/images/products')));

Image::make($image->getRealPath())-> resize(200, 200)-> save($path);

$product->image = 'assets/images/products/' . $filename;

I used dd(is_writable())to check permission, it is true. And I also try chmod 777 public/assets/images/products

View:

{!! Form::open(array('url' => 'admin/product/update', 'class' => '', 'files' => true)) !!}
<div class="form-group">
{!! Form::Label('image', 'Product Image', array('sr-only')) !!}
<div>
{!! HTML::image($product->image, $product->title, array('class' => 'img-rounded', 'height' => '200')) !!}
</div>
<hr/>
{!! Form::file('image', array('class' => 'form-group')) !!}
</div>
{!! Form::close() !!}

What can I do with this error?

Make sure that the Folder you mentioned ie, assets/images/products/ exists and it has writable permission

Make sure that you have a good file name ie, it should not contain : symbol

So you shall change you code like this

$image = Input::file('image');
$filename = date('Y-m-d-H-i-s')."-". $image->getClientOriginalName();
$path = public_path('assets/images/products/'.$filename);
Image::make($image->getRealPath())-> resize(200, 200)-> save($path);

You problem might lie with the fact that you are using reserved characters within your file name. In Windows you cannot use colons : in file names. So you might want to replace the colons with dashes or something else, maybe like so:

$filename = date('Y-m-d_H-i-s') . "-" . $image->getClientOriginalName();

If readability is not a requirement here, and you're using the date and time to create unique filenames, you could just prepend a UNIX timestamp to the filename:

$filename = time() . "-" . $image->getClientOriginalName();

You might want to read the Naming Files, Paths, and Namespaces documentation on MSDN for more information about the subject.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM