【问题标题】:Laravel - How to upload file to a different folderLaravel - 如何将文件上传到不同的文件夹
【发布时间】:2017-05-27 21:50:59
【问题描述】:

我正在开发一个将文件上传到public/images 的网站,但在我的共享主机中,我的结构不同,因此上传效果不佳。

我的结构是:

- css
- images(I WANT TO UPLOAD TO THIS FOLDER)
- fonts
- js
- laravel-code
-.htaccess

如果我回显base_path(),我会得到/laravel-code,但我需要返回,但我不知道该怎么做。

如果使用public_path,它会在laravel-code 文件夹中创建一个/public/images,我不希望这样。

如何将图像保存在我的图像文件夹中?

谢谢

【问题讨论】:

标签: php laravel laravel-5 shared-hosting


【解决方案1】:

好的。试试看

        public function upload(Request $request)
        {
            $imageName = 'testing';
            $file = $request->file('picture');
            $ext = $file->getClientOriginalExtension();
            $file->move("images/", "{$imageName}.{$ext}");
        }

【讨论】:

    【解决方案2】:

    尝试:

    base_path() . "/../images/"
    

    【讨论】:

      【解决方案3】:

      如果您的路径是 public/images,那么您只需将目标路径用作“images/”,然后将文件移动到目标,如下所示:

      /* check Request has file or not */
      if($request->hasFile('file')){
      
          /* get File from Request */
          $file = $request->file('file');
      
          /* get File Extension */
          $extension = $file->getClientOriginalExtension();
      
          /* Your File Destination */
          $destination = 'images/';
      
          /* unique Name for file */
          $filename = uniqid() . '.' . $_extension;
      
          /* finally move file to your destination */
          $file->move($destination, $filename);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-18
        • 2021-05-02
        • 1970-01-01
        • 2012-03-14
        相关资源
        最近更新 更多