geek123's avatar

How to get and display images from FTP disk storage?

How to get and display image from ftp disk storage?

I use Storage::put() to upload them and it works perfectlly, but to get them I tried Storage::get(), Storage::url() and Storage::download() but all of them do not work!

0 likes
7 replies
jlrdw's avatar

See my answer here

https://laracasts.com/discuss/channels/laravel/how-to-storage-link-laravel-project-in-live-server

And please study the documentation Taylor explains all of this.

Edit: Not sure what you are trying to do, but another way to display images, I don't use it any longer is:

<?php
$basedir = 'your base directory to images';
// but replac with your base dir
$imagedir = $_GET['dir'];
$image = $_GET['img'];

$file = $basedir.'/'.$imagedir.'/'.$image;
$fallback = $basedir.'/fallback.gif';
$size = filesize($file); // File size
$length = $size;
//DETERMINE TYPE
$ext = array_pop(explode ('.', $file));
$allowed['gif'] = 'image/gif';
$allowed['png'] = 'image/png';
$allowed['jpg'] = 'image/jpeg';
$allowed['jpeg'] = 'image/jpeg';

if(file_exists($file) && $ext != '' && isset($allowed[strToLower($ext)])) {
    $type = $allowed[strToLower($ext)];
} else {
    $file = $fallback;
    $type = 'image/gif';
}

header("Accept-Ranges: bytes");
header("Cache-Control: public");
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $length);
//header('Content-Disposition: inline; filename="' . $image . '"');
header("Content-Disposition: inline; filename=\"".$image."\";");
header("Last-Modified: " . date('r', filemtime($file)));

ob_clean();
readfile($file);
exit(0);

?> 

files called with the img src tag

Your title:

How to get and display images

But then you say:

Storage::download()

geek123's avatar

Thank you @jlrdw but my problem is with the FTP disk (in filesystems.php)

jlrdw's avatar

If all you are doing is trying to display an image then use the asset helper.

See helpers in the documentation.

Otherwise can you explain better.

Cronix's avatar

ftp is meant to transfer files to/from a server. It's not meant to display images.

Where are you uploading the images via ftp? I mean, what's the dir they're getting stored in? If you store them in the /public folder of laravel, then you can directly access them via the url without having to go through a very slow ftp process to do it requiring a login (ftp process) for each image, etc.

geek123's avatar

@cronix yes FTP is slow to store and retrieve images but I use it just for development purpose.

@jlrdw My config looks like:

/* app/config/filesystems.php */

'default' => env('FILESYSTEM_DRIVER', 'ftp'),

'ftp' => [
            'driver'   => 'ftp',
            'host'     => 'ftp.drivehq.com',
            'username' => env('FTP_USERNAME'),
            'password' => env('FTP_PASSWORD'),
        ],

to store files I used:

$myFileName = 'image.png';
Storage::put($myFileName, $myFileContent);  // the file uploded to the FTP server successfully

to retrieve files I used:

Storage::get($myFileName )   // throws FileNotFoundException
Storage::url($myFileName )    // throws "This driver does not support retrieving URLs."

any idea?

jlrdw's avatar

yes FTP is slow to store and retrieve images but I use it just for development purpose.

Why ftp, just store the images and display images.

geek123's avatar

@JLRDW - I deployed my app on Heroku and have issues to store images on its server so I switched from 'local' to 'ftp' disk.

Please or to participate in this conversation.