Taking the contents of an image I made from my folder

Asked

Viewed 67 times

0

Good i have a form that uploads images , these images are played in the directory: uploads/{idcampaign}/images/

In my PHP controller how do I access this image and grab your content?

    public function get($id) {

      /** @var  UploadedFile $file */

      $image = $this->repository->byId($id)->first();
      $url = "/storage/app/" + $image->path;
      if($image) {
          return response()->file($url , ['Content-Type' => 'image.png | 
      image.jpeg']);
    }
      throw new NotFoundHttpException('image-not-found');
    }

I tried that and it didn’t work. I just need to get to the image and get its contents so I can return to the view.

  • What do you mean "content"? The image content is binary, you won’t be able to represent it directly in json. Why don’t you walk her way to the view?

  • I try to pass a <img src="route"> but this route is not publicly accessible, so I’m trying to get the "content" of the image through a controller

1 answer

1


When such doubts arise, do not hesitate to consult documentation. Basically you can use the function file, accessed from the Response object.

Applying, it gets:

return response()->file($url);

But it will probably be necessary to send some headers as a response, such as the file mime. Example:

 return response()->file($url, ['Content-Type' => 'image/png']);
  • I did this and php actually navigated to the file, but did not send a message saying that the file does not exist , message: "The file "uploads/24/images/2sWc551Kr0WCXLOe9c3hnFTlfKZ4BqTcmu27RsuE.jpeg" does not exist"

  • and this is the exact path of my file

  • The upload folder is in the same directory as your controller?

  • no, I’ve already noticed this and put the right url in the search: 'Storage/app/uploads/etc' but agr the console returned me this error: "file 0 not found"

  • @Gabriel. H Probably when doing this you must have uploaded some directories, or used a base url to concatenate with the file name. Because if you are following the standard of the Standard, your controller must be within app/http/controllers, so if you, at the time you call the file, use relative path, it will search from the controllers folder. Maybe you can use the function app_path to search the path from the app directory. Example: app_path() . '/uploads/24/images/2sWc551Kr0WCXLOe9c3hnFTlfKZ4BqTcmu27RsuE.‌​jpeg'

  • It’s actually in 'Storage/app' , I tried to use 'storage_path' but it didn’t, there’s a way for me to access the 'app' folder inside the 'Storage' folder'?

  • here has all possible path functions. To upload directories just use ../. Type: /home/usuario/storage/app/../arquivo.php. When executed the.php file will be searched inside the Storage folder.

  • It doesn’t have the path function I need , and when I mount my own URL, even if it is correct, it returns an error like this: File 0 does not exist

  • I saw that it was a syntax error , and that using Storage I got to the image path

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.