简体   繁体   English

无法在 Laravel 5 中上传文件

[英]Can't upload file in laravel 5

I am trying to upload an image in Laravel 5.我正在尝试在 Laravel 5 中上传图像。

My environment is Windows.我的环境是windows。 I am using php artisan serve to run Laravel.我正在使用 php artisan serve 来运行 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.我用 dd(is_writable()) 来检查权限,确实如此。 And I also try chmod 777 public/assets/images/products我也尝试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确保您提到的文件夹,即assets/images/products/存在并且它具有可写权限

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.在 Windows 中,您不能在文件名中使用冒号: 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:如果这里不要求可读性,并且您使用日期和时间来创建唯一的文件名,则可以在文件名前添加 UNIX 时间戳:

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

You might want to read the Naming Files, Paths, and Namespaces documentation on MSDN for more information about the subject.您可能需要阅读 MSDN 上的命名文件、路径和命名空间文档以获取有关该主题的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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