473,811 Members | 2,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trouble Using a PHP Script to Send an Image

I have a script, which I've called test-loadpic.php and some pages
reference it by means of

<img src="test-loadpic.php?sou rcepic=$picNum" >

where $picNum stores a number. This part itself works fine.
test-loadpic.php uses the "sourcepic" GET variable to reference a file
on disk, get it's file/MIME type, transmit the headers and then the raw
image. The important code is below:

header ("Content-type: $fileMIME");
header ("Content-Length: ".filesize($fil ePath.$fileName ));

$fp = fopen ($filePath.$fil eName, "rb");
$er = fpassthru ($fp);
fclose($fp);

$fileMIME is the MIME type of the image, of course ("image/gif" or
"image/jpeg" in my case). $filePath and $fileName point to the image
(ex: "/var/www/phptest/images/" and "filter.jpg ").

Is this all I need to properly transmit an image?

This seems to work fine in IE but sometimes Netscape 7.1 tries to
download the image instead of showing it. Are my headers incorrect? I
suspect there's a glitch in my scripting that IE can tolerate but
Netscape can't. Is there a proper "end of image" code I need to send to
the browser that isn't included at the end of the images themselves?

Thank you in advance to anyone with some insight.

--
- Michael J. Astrauskas

Jul 17 '05 #1
7 2376
yes should work fine...

you could try this function to display the image....
Usage: showImage('/path/to/image.jpg');
<?php

function showimage($path )
{
if (!file_exists($ path))
return false;

$filesize = filesize($path) ;
$basename = basename($path) ;

$info = @getimagesize($ path);
if (!is_array($inf o))
return false; // invalid image

switch ($info[2])
{
case 1:
$contentType = 'image/gif';
break;
case 2:
$contentType = 'image/jpeg';
break;
case 3:
$contentType = 'image/png';
break;
default:
return false; // unsupported
break;
}

$fd = @fopen($path, 'rb');
if (!is_resource($ fd))
return false; // could not read file.

$contents = '';
while ($data = fread($fd, 4096))
{
if (strlen($data) == 0)
break; // EOF
$contents .= $data;
}
fclose($fd);

Header('Content-type: ' . $contentType);
Header('Content-length: ' . $filesize);
Header('Content-Disposition: inline; filename='.$bas ename);
echo $contents;
return true;
}

?>
On Tue, 25 May 2004 10:42:31 -0700, "Michael J. Astrauskas"
<tr****@nospam. cox.net> wrote:
I have a script, which I've called test-loadpic.php and some pages
reference it by means of

<img src="test-loadpic.php?sou rcepic=$picNum" >

where $picNum stores a number. This part itself works fine.
test-loadpic.php uses the "sourcepic" GET variable to reference a file
on disk, get it's file/MIME type, transmit the headers and then the raw
image. The important code is below:

header ("Content-type: $fileMIME");
header ("Content-Length: ".filesize($fil ePath.$fileName ));

$fp = fopen ($filePath.$fil eName, "rb");
$er = fpassthru ($fp);
fclose($fp);

$fileMIME is the MIME type of the image, of course ("image/gif" or
"image/jpeg" in my case). $filePath and $fileName point to the image
(ex: "/var/www/phptest/images/" and "filter.jpg ").

Is this all I need to properly transmit an image?

This seems to work fine in IE but sometimes Netscape 7.1 tries to
download the image instead of showing it. Are my headers incorrect? I
suspect there's a glitch in my scripting that IE can tolerate but
Netscape can't. Is there a proper "end of image" code I need to send to
the browser that isn't included at the end of the images themselves?

Thank you in advance to anyone with some insight.


Jul 17 '05 #2
In article <rl************ *************** *****@4ax.com>, Shane Lahey wrote:
<?php

function showimage($path )
{
if (!file_exists($ path))
return false;


Assuming the $path is in public space

header('Locatio n: http://url/to/image');

Done ;)

--
Tim Van Wassenhove <http://home.mysth.be/~timvw/contact.php>
Jul 17 '05 #3
Shane Lahey wrote:
$contents = '';
while ($data = fread($fd, 4096))
{
if (strlen($data) == 0)
break; // EOF
$contents .= $data;
}
fclose($fd);


What is the purpose of this loop?

--
- Michael J. Astrauskas

Jul 17 '05 #4
On Tue, 25 May 2004 22:48:42 -0700, "Michael J. Astrauskas"
<tr****@nospam. cox.net> wrote:
Shane Lahey wrote:
$contents = '';
while ($data = fread($fd, 4096))
{
if (strlen($data) == 0)
break; // EOF
$contents .= $data;
}
fclose($fd);


What is the purpose of this loop?


binary safe way to read the contents of a file into a variable.......
Jul 17 '05 #5

"Shane Lahey" <s.*****@roadru nner.nf.net>
news:rl******** *************** *********@4ax.c om...
yes should work fine...
[skip] $fd = @fopen($path, 'rb');
if (!is_resource($ fd))
return false; // could not read file.

$contents = '';
while ($data = fread($fd, 4096))
{
if (strlen($data) == 0)
break; // EOF
$contents .= $data;
}
fclose($fd);

Header('Content-type: ' . $contentType);
Header('Content-length: ' . $filesize);
Header('Content-Disposition: inline; filename='.$bas ename);
echo $contents;
return true;
}

?>


Header('Content-type: ' . $contentType);
readfile($path) ;
I'm using a little bit shorter script. Would you like to compare with yours?
I'm interesting in your opinion and others opinion too.
Jul 17 '05 #6
heh, sorry I don't actually have one, I came up with this off the top
of my head :/

On Wed, 26 May 2004 10:19:46 +0300, "Dennis Biletsky" <uf***@ua.fm>
wrote:

"Shane Lahey" <s.*****@roadru nner.nf.net>
news:rl******* *************** **********@4ax. com...
yes should work fine...

[skip]
$fd = @fopen($path, 'rb');
if (!is_resource($ fd))
return false; // could not read file.

$contents = '';
while ($data = fread($fd, 4096))
{
if (strlen($data) == 0)
break; // EOF
$contents .= $data;
}
fclose($fd);

Header('Content-type: ' . $contentType);
Header('Content-length: ' . $filesize);
Header('Content-Disposition: inline; filename='.$bas ename);
echo $contents;
return true;
}

?>


Header('Conten t-type: ' . $contentType);
readfile($path );
I'm using a little bit shorter script. Would you like to compare with yours?
I'm interesting in your opinion and others opinion too.


Jul 17 '05 #7
Tim Van Wassenhove wrote:
Shane Lahey wrote:
<?php

function showimage($path )
{
if (!file_exists($ path))
return false;


Assuming the $path is in public space

header('Locatio n: http://url/to/image');

Done ;)


This is a good idea, and I'll keep it in mind, but I'm trying to hide
file names from users.

--
- Michael J. Astrauskas

Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
19108
by: Uncle_Alias | last post by:
I would like use some of the GD image functions, so I ran a couple of short scripts to see if it worked, such as: <?php /* Create a red square */ $image = imagecreate(200, 200); $colorRed = imagecolorallocate($image, 255, 0, 0); imagefill($image, 0, 0, $colorRed);
9
4968
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
7
3346
by: Jake Barnes | last post by:
I've a little webcam program that snaps a picture of me and uploads it every 20 seconds. It automatically uploads the image to my server. It always give the image the same name, and thus it overwrites the image that has been there for the last 20 seconds. People can, if they wish, hit the refresh button every 20 seconds, but I thought it would be fun to have a Javascript function that actually refreshed the image. However, this following...
5
1881
by: yawnmoth | last post by:
I'm having some difficulty with adding elements to a webpage via the DOM. The following works: main.htm: <script> js = document.createElement('script'); js.src='test.js'; document.getElementsByTagName('head').appendChild(js); </script>
3
1714
tolkienarda
by: tolkienarda | last post by:
hi all i have a problem i have a script that i got working exactly the way i wanted it. and then i tried to intrigate it into the site and it totaly stopped working. below is the original code <? $host="localhost"; // Host name. $db_user="****"; // MySQL username. $db_password="*******"; // MySQL password. $database="cms"; // Database name.
21
34454
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most obvious of which is the sharing of files. For example, you upload images to a server to share them with other people over the Internet. Perl comes ready equipped for uploading files via the CGI.pm module, which has long been a core module and allows users...
3
4517
by: kksandeep | last post by:
i am using this three files to uplod file. i got this file from net but i think these have some error. i am new to this field plz help the script i found is some helpful but not too that i need my objective is this that when i uplod a file it should be desply on same page with ajax uplod after when i refresh page this should be not remains longer and on clicking other image its replase previous image plz help how i can do this the...
7
4051
by: mike57 | last post by:
The minimal AJAX script below works in Firefox, but not in IE, Opera, or Chrome. I could use some suggestions or referrals to resources that will help me get the script working in other browsers. Before there are six characters entered in the CAPTCHA code field, the 'Send' button is supposed to be disabled. When there are at least six characters in the CAPTCHA code field, the script attempts to verify the CAPTCHA w/AJAX. If it verifies, it...
5
13389
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL http://mghospedagem.com/images/controlpanel.jpg instead of http://mghospedagem.comhttp://mghospedagem.com/images/controlpanel.jpg As u see, there's the website URL before the image URL.
0
9728
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10389
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10402
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7670
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6890
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3867
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.