无法将图像上传到laravel中的公共目录

时间:2014-02-18 12:48:59

标签: php laravel laravel-4 image-uploading

我正在尝试将图片上传到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));

我认为这意味着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);

5 个答案:

答案 0 :(得分:8)

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
  }});

会上传到/public/assets/uploads文件夹,也许会帮助您

答案 1 :(得分:4)

您的目的地路径应为:

 $destinationPath = public_path(sprintf("\\uploads\\%s\\", str_random(8)));

答案 2 :(得分:1)

在公共文件夹中创建一个名为uploads的文件夹,然后在上传文件中创建一个名为照片的文件夹,尝试在控制器中使用此文件。

 $data = Input::all();
    $rules = array(           
        'photo' => 'between:1,5000|upload:image/gif,image/jpg,image/png,image/jpeg',            
    );
    $messages = array(
        'upload' => 'The :attribute must be one of the following types .jpg .gif .png .jpeg',
        'between' => 'The :attribute file size must be 1kb - 5mb'
    );

    $validator = Validator::make($data, $rules, $messages);
    if($validator->passes())
    {
        $uploads = public_path() . '/uploads/photos';
        $photo = Input::file('photo');
        $photodestinationPath = $uploads;

        $photoname = null;

        if($photo != null)
        {
            $photoname = $photo->getClientOriginalName();
            Input::file('photo')->move($photodestinationPath, $photoname);
        }

        $thumbnail = new Model_name();          
        $thumbnail ->column_name_in_table = $photoname;

然后将其放入您的路线

 Validator::extend('upload', function($attribute,$value, $parameters){
$count = 0;
for($i = 0; $i < count($parameters); $i++)
{
    if($value->getMimeType() == $parameters[$i])
    {
        $count++;
    }
}

if($count !=0)
{
    return true;
}
return false;});

答案 3 :(得分:0)

在ImageProductController.php(Controller)文件中插入以下代码:

public function store(Request $request, $id)
{
if ($request->hasFile('image')){
  $rules = [
    'image'  => 'required|image|mimes:jpeg,png,jpg,gif,svg'
  ];

  $this->validate($request, $rules);

// Save file in directory
  $file = $request->file('image');
  $path = public_path() . '/images';
  $fileName = md5(rand(0,9999)).'.'.$file->getClientOriginalExtension();
  $file->move($path, $fileName);

  $imageProduct = new ImageProduct();
  $imageProduct->image = $fileName;
}

在create.blade.php(View)文件中插入以下代码:

<form enctype="multipart/form-data" method="post" action=" " >
 {{ csrf_field() }}

      <label for="image" class="btn btn-primary">Inserir Imagem</label> 

      <input type="file" name="image" id="image"  accept="image/*" 
        class="form-control" value="{{ old('image') }}>   
</form>

在ImageProduct.php文件(Model)中插入以下代码:

public function getUrlAttribute()
{
    if(substr($this->image, 0, 4) === "http"){
        return $this->image;
    }

    return '/images/'.$this->image;
}
祝你好运!

答案 4 :(得分:-1)

您需要授予要存储图像的laravel文件夹的权限。 推荐是:

df.groupby('Date').sum()