Jump to content

unable to echo images that are inside a string


jasonc

Recommended Posts

how do i get the image to show as the code i have is echoing what i have been told is binary, not confirmed.  and not the images.

 

all images are .jpg

 

$allowed_types = array('png','jpg','jpeg','gif');
$imgdir = '/home/mysite/ftpfolder';
$imgFilesArray = scandir($imgdir);
foreach ($imgFilesArray as $imgkey => $imgvalue)
{
$imgInfo =  pathinfo($imgdir . '/' . $imgvalue);
$imgExtension = $imgInfo['extension'];
if(in_array($imgExtension, $allowed_types))
{
readfile($imgdir . '/' . $imgvalue);
//echo '<img src = "'. $imgdir  .'/'. $imgInfo['basename'].'" alt="" />';
}
}

 

images are FTP'd to the ftpfolder and can not be placed anywhere else but this folder due to security reasons.

Link to comment
Share on other sites

To send an image from PHP to a browser, you have to first send a content-type header to tell the browser that you are sending an image (and the type of image).  This cannot be done in the same script that is sending an HTML page. You can NOT mix HTML and binary data.  You also cannot send more than one image at a time.

 

If the FTP folder is in the public area of your website, then the <IMG ...> tag (you have commented out) will work IF you specify the SRC attribute relative to your website's root directory.

 

If the FTP folder is NOT in the public area then you have to specify another PHP script as the IMG SRC attribute with a parameter. For instance:

<IMG src="/getFtpImage.php?file=filename.ext">

Then write the getFtpImage.php script to send the content-type header, read the file and send it (the binary data) to the browser.

 

 

Link to comment
Share on other sites

very confused now...

 

i now have this...

 

								$allowed_types = array('png','jpg','jpeg','gif');
								$imgdir = '/home/mysite/ftpfolder';
								$imgFilesArray = scandir($imgdir);
								foreach ($imgFilesArray as $imgkey => $imgvalue)
								{
									$imgInfo =  pathinfo($imgdir . '/' . $imgvalue);
									$imgExtension = $imgInfo['extension'];
									if(in_array($imgExtension, $allowed_types))
									{
										 // readfile($imgdir . '/' . $imgvalue);
										 // echo '<img src = "'. $imgdir  .'/'. $imgInfo['basename'].'" alt="" />';
										 ?><IMG src="/getFtpImage.php?file=<? echo ($imgvalue); ?>"><?
									}
								}

 

getFtpImage.php

<?
header('Content-Type: image/jpg');
$imgdir = '/home/mysite/ftpfolder';
readfile($imgdir . '/' . $_get['file']);
?>

 

but i still get the same results and no images showning.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.