cfk*_*ane 5 php image-uploading laravel laravel-4
我正在尝试将图像上传到Laravel应用程序,然后需要公开显示.但是,我似乎只能上传到应用程序根目录下的文件夹.任何上传到公共目录的尝试都会引发以下错误:
protected function getTargetFile($directory, $name = null)
{
if (!is_dir($directory)) {
if (false === @mkdir($directory, 0777, true)) {
throw new FileException(sprintf('Unable to create the "%s" directory', $directory));
Run Code Online (Sandbox Code Playgroud)
我假设这意味着Laravel将阻止我上传到任何可公开访问的文件.我实际上更喜欢这样,但是,我如何链接到公共目录之外的图像我试图... /我的方式从公共文件夹,但适当地,这不起作用.
或者,任何让我直接上传到公共文件夹的内容也会很棒.
如果它有帮助,这是我的控制器方法将文件放在公共文件夹中:
$thumbnail_file = Input::file('thumbnail');
$destinationPath = '/uploads/'. str_random(8);
$thumbnail_filename = $thumbnail_file->getClientOriginalName();
$uploadSuccess = Input::file('thumbnail_url')->move($destinationPath, $thumbnail_filename);
Run Code Online (Sandbox Code Playgroud)
Route::post('upload', function(){
$avarta = Input::file('avatar');
if(strpos($avarta->getClientMimeType(),'image') !== FALSE){
$upload_folder = '/assets/uploads/';
$file_name = str_random(). '.' . $avarta->getClientOriginalExtension();
$avarta->move(public_path() . $upload_folder, $file_name);
echo URL::asset($upload_folder . $file_name); // get upload file url
return Response::make('Success', 200);
}else{
return Response::make('Avatar must be image', 400); // bad request
}});
Run Code Online (Sandbox Code Playgroud)
将上传到 /public/assets/uploads
文件夹,也许会帮助你
您的目标路径应该是:
$destinationPath = public_path(sprintf("\\uploads\\%s\\", str_random(8)));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14728 次 |
最近记录: |