jasonc Posted October 20, 2010 Share Posted October 20, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/216403-unable-to-echo-images-that-are-inside-a-string/ Share on other sites More sharing options...
DavidAM Posted October 20, 2010 Share Posted October 20, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/216403-unable-to-echo-images-that-are-inside-a-string/#findComment-1124551 Share on other sites More sharing options...
jasonc Posted October 20, 2010 Author Share Posted October 20, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/216403-unable-to-echo-images-that-are-inside-a-string/#findComment-1124564 Share on other sites More sharing options...
jasonc Posted October 20, 2010 Author Share Posted October 20, 2010 solved i have a leading / in the first script /getFtpImage.php? and i had not capitalised the $_GET Quote Link to comment https://forums.phpfreaks.com/topic/216403-unable-to-echo-images-that-are-inside-a-string/#findComment-1124566 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.