473,385 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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?sourcepic=$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($filePath.$fileName));

$fp = fopen ($filePath.$fileName, "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 2339
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($info))
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='.$basename);
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?sourcepic=$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($filePath.$fileName));

$fp = fopen ($filePath.$fileName, "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('Location: 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.*****@roadrunner.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='.$basename);
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.*****@roadrunner.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='.$basename);
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 #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('Location: 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
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 =...
9
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...
7
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...
5
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';...
3
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 ...
21
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...
3
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 ...
7
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. ...
5
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.