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

ekqde3dh  于 2023-03-28  发布在  PHP
关注(0)|答案(5)|浏览(232)

我试图将图像上传到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);
7ajki6be

7ajki6be1#

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文件夹,也许对你有帮助

ghhkc1vu

ghhkc1vu2#

您的目标路径应为:

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

laik7k3q3#

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

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

nr9pn0ug4#

在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(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;
}

祝你好运!

jmo0nnb3

jmo0nnb35#

你需要给予laravel文件夹的权限来存储图片。命令是:
sudo chown www-data:www-data /var/www/html/project-name/path/to/folder

相关问题