Display image from FTP server in HTML IMG tag

I have full access to the image file on the server from the client in FileZilla. Trying to access this image from HTML IMG generated from PHP on the client. Trying to play by the book and failed. Any ideas are welcome.

eg. img src=‘ftp://server name/directory path/file name’

Well, you can not load it directly thru FTP that way. It is because you are not logged into the FTP account. You can use the PHP FTP functions, but, they are tricky. Is there a reason you can not just use HTTP version? Is your image file protected?

eg: < img src=“http://server-name/directory-path/image-file-name.jpg” > ???

I tried HTTP:// with the same result.
Also tried variety
ftp://username:[email protected]/directory-path/image-file-name.jpg
ftp://[email protected]/directory-path/image-file-name.jpg
http://username:[email protected]/directory-path/image-file-name.jpg
http://[email protected]/directory-path/image-file-name.jpg

It is obvious that something simple is missing.

This one should work. Unless it is a secured server. In that case HTTPS:// …
Now, the other issue that might cause this problem is the folder’s permission settings. If you use your FTP, Filezilla or anything else, you can view the the permissions set on the folder that the image is in.
Let us know that. I will explain why FTP will not work…

Your browser handles the BROWSER-SIDE code displaying data and images. The server handles SERVER-SIDE programs. You NEVER see the server side items in the browser. Therefore PHP is never in the code when you view-source of a page. This is for security so that hackers can not see your code and hack your site. Now, that is why you can not use FTP the way you have in your HTML. It just is not possible.

Two workarounds are available. Simply change the permissions for the image folder to allow reading of the files OR simply use PHP to insert the image. The first way is the easiest. But, of course it allows hackers to see your images if they know the name of the files. The second way is trickier. You need to create an image object, load it with the base64 version of the image and then send the image object to the browser using headers. Not too many lines to do that, but, more work.

So, the next question is, are you worried about users or hackers seeing the images? If you need them to be 100% secure, do it the second way. If it is okay for them to be seen, the first way is simpler!

1 Like

Oh, by the way, you can do it this way for version #2. ( On the server of course… )

$image = "directory-path/image-file-name.jpg";
$image_info = getimagesize($image);
header("Content-type: {$image_info['mime']}");
This only works of course for PHP files, so the HTML must be inside a PHP file on the server.
Works for most image types. (PNG, png, GIF, gif, JPG, jpg, JPEG, jpeg, etc... )  Hope this helps...
Sponsor our Newsletter | Privacy Policy | Terms of Service