【问题标题】:Can't upload images to a public directory in laravel无法将图像上传到 laravel 中的公共目录
【发布时间】:2014-03-18 05:49:01
【问题描述】:

我正在尝试将图像上传到需要公开显示的 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);

【问题讨论】:

  • 只是为了敲响一些钟声。您可以使用 0755 作为权限模式,如果您想创建随机文件或文件夹名称,您可以使用 time() 函数。

标签: php laravel laravel-4 image-uploading


【解决方案1】:
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文件夹,也许对你有帮助

【讨论】:

    【解决方案2】:

    您的目标路径应该是:

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

    【讨论】:

    • 你需要转义所有的反斜杠 --> $destinationPath = public_path().sprintf("\\uploads\\%s\\", str_random(8));
    • 我收到错误:Impossible to create the root directory
    【解决方案3】:

    在您的 public 中创建一个名为 uploads 的文件夹并在 uploads 中创建另一个名为 photos 的文件夹后,尝试在您的控制器中使用这个。

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

    【讨论】:

      【解决方案4】:

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

      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(查看)中插入以下代码:

      <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 文件(模型)中插入以下代码:

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

      祝你好运!

      【讨论】:

        【解决方案5】:

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

        sudo chown www-data:www-data /var/www/html/project-name/path/to/folder

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-01-26
          • 2022-01-18
          • 2015-09-09
          • 2021-12-22
          • 2021-10-05
          • 1970-01-01
          相关资源
          最近更新 更多