By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,109 Members | 969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,109 IT Pros & Developers. It's quick & easy.

Problem with http File Transfer

P: n/a
Hello all:

I have a web app that creates an image of a graph (as a png), based on
user input of a combination of drop-down box items.

I'm trying to add a function that allows the user to save the graph
image to his hard drive, just like right-clicking on the image and
selecting 'save image as...' There's a link next to the graph that
should open the save image dialog. The link calls the following php script:

<?php
require("myImageSave.php"); //contains getPNG() function

$QUERY_STRING = $_SERVER['QUERY_STRING'];
$data = explode("&", $QUERY_STRING);
$image = ('./temp.png');
$im = getPNG($data);

if(ImagePNG($im, $image)) {
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header('Content-Type: image/png');
header('Content-Length: ' . filesize($image));
header('Content-Disposition: attachment;
filename="Graph.png"');
header("Content-Transfer-Encoding: binary");
readfile($image);

}

exit();
?>

So Here's the problem. This bit of code saves a png file on the server
called temp.png This file contains the correct png, so I know the
getPNG() function, and everything before that is working correctly.
However, the file that gets downloaded to the user's computer is
incorrect and will not display correctly in an image viewer.

I've tried comparing the file saved on the server with the file saved to
the user's computer, and the two files are *almost* exact, with one
exception: The user's downloaded version has 4 extra linefeed ('0A')
characters at the top.

I realize that there's some superfluous code there -- I've tried quite a
few variations of headers, all with the same results. I know I could
just send the file to the user without saving it on the server, but this
also gave me the same results.

Any ideas? Why is the file not transferring correctly?

Thanks in advance.
Jun 4 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Jun 4, 2:11 pm, shadowman <shadow...@noemail.comwrote:
Hello all:

I have a web app that creates an image of a graph (as a png), based on
user input of a combination of drop-down box items.

I'm trying to add a function that allows the user to save the graph
image to his hard drive, just like right-clicking on the image and
selecting 'save image as...' There's a link next to the graph that
should open the save image dialog. The link calls the following php script:

<?php
require("myImageSave.php"); //contains getPNG() function

$QUERY_STRING = $_SERVER['QUERY_STRING'];
$data = explode("&", $QUERY_STRING);

$image = ('./temp.png');
$im = getPNG($data);

if(ImagePNG($im, $image)) {
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header('Content-Type: image/png');
header('Content-Length: ' . filesize($image));
header('Content-Disposition: attachment;
filename="Graph.png"');
header("Content-Transfer-Encoding: binary");
readfile($image);

}

exit();
?>

So Here's the problem. This bit of code saves a png file on the server
called temp.png This file contains the correct png, so I know the
getPNG() function, and everything before that is working correctly.
However, the file that gets downloaded to the user's computer is
incorrect and will not display correctly in an image viewer.

I've tried comparing the file saved on the server with the file saved to
the user's computer, and the two files are *almost* exact, with one
exception: The user's downloaded version has 4 extra linefeed ('0A')
characters at the top.

I realize that there's some superfluous code there -- I've tried quite a
few variations of headers, all with the same results. I know I could
just send the file to the user without saving it on the server, but this
also gave me the same results.

Any ideas? Why is the file not transferring correctly?

Thanks in advance.
What is your error reporting level set to? If you set it to E_ALL at
the top of the script, does it complain about not being able to modify
header information? I'm thinking that you might have some empty lines
in myImageSave.php outside of the PHP block or before the <?php line
in this file.

-Mike PII

Jun 4 '07 #2

P: n/a
Mike P2 wrote:
>
What is your error reporting level set to? If you set it to E_ALL at
the top of the script, does it complain about not being able to modify
header information? I'm thinking that you might have some empty lines
in myImageSave.php outside of the PHP block or before the <?php line
in this file.
No errors coming up in my log, but I checked the myImageSave.php file,
and sure enough, there were 4 linefeeds after the ?>

Removing those fixed the problem. Thanks for your help! It makes sense,
although I never would have thought about that.
Jun 4 '07 #3

P: n/a
shadowman wrote:
Mike P2 wrote:
>>
What is your error reporting level set to? If you set it to E_ALL at
the top of the script, does it complain about not being able to modify
header information? I'm thinking that you might have some empty lines
in myImageSave.php outside of the PHP block or before the <?php line
in this file.

No errors coming up in my log, but I checked the myImageSave.php file,
and sure enough, there were 4 linefeeds after the ?>

Removing those fixed the problem. Thanks for your help! It makes sense,
although I never would have thought about that.
In addition to what Mike said - you're script is very unsafe. What
happens if you get two people who try to generate the files at almost
exactly the same time? Since you're using the same filename for all of
them, one will overwrite the other.

You should generate a unique filename for each one, then delete it at
the end (or after some period of time, anyway).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jun 5 '07 #4

P: n/a
Jerry Stuckle wrote:
shadowman wrote:
>Mike P2 wrote:
>>>
What is your error reporting level set to? If you set it to E_ALL at
the top of the script, does it complain about not being able to modify
header information? I'm thinking that you might have some empty lines
in myImageSave.php outside of the PHP block or before the <?php line
in this file.

No errors coming up in my log, but I checked the myImageSave.php file,
and sure enough, there were 4 linefeeds after the ?>

Removing those fixed the problem. Thanks for your help! It makes
sense, although I never would have thought about that.

In addition to what Mike said - you're script is very unsafe. What
happens if you get two people who try to generate the files at almost
exactly the same time? Since you're using the same filename for all of
them, one will overwrite the other.

You should generate a unique filename for each one, then delete it at
the end (or after some period of time, anyway).
Roger that. I was really only creating the file on the server for
debugging purposes.

I think what I really want to do is skip that altogether. More along the
lines of:

$im = getPNG($data);

header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename=
"Graph.png"');
header("Content-Transfer-Encoding: binary");

ImagePNG($im);

Thanks for your help!
Jun 5 '07 #5

P: n/a
shadowman <sh*******@noemail.comwrote:
>
<?php
require("myImageSave.php"); //contains getPNG() function

$QUERY_STRING = $_SERVER['QUERY_STRING'];
$data = explode("&", $QUERY_STRING);
$image = ('./temp.png');
$im = getPNG($data);

if(ImagePNG($im, $image)) {
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header('Content-Type: image/png');
Did you copy this from someplace? An HTTP response only gets one
Content-Type header. The first one is going to be ignored.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jun 7 '07 #6

P: n/a
Tim Roberts wrote:
shadowman <sh*******@noemail.comwrote:
><?php
require("myImageSave.php"); //contains getPNG() function

$QUERY_STRING = $_SERVER['QUERY_STRING'];
$data = explode("&", $QUERY_STRING);
$image = ('./temp.png');
$im = getPNG($data);

if(ImagePNG($im, $image)) {
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header('Content-Type: image/png');

Did you copy this from someplace? An HTTP response only gets one
Content-Type header. The first one is going to be ignored.
Thanks. I had been playing around with a different combination of
headers when I wasn't sure why I couldn't get this working. Red
herring, so to speak.
Jun 7 '07 #7

P: n/a
On Jun 4, 11:11 pm, shadowman <shadow...@noemail.comwrote:
Hello all:

I have a web app that creates an image of a graph (as a png), based on
user input of a combination of drop-down box items.

I'm trying to add a function that allows the user to save the graph
image to his hard drive, just like right-clicking on the image and
selecting 'save image as...' There's a link next to the graph that
should open the save image dialog. The link calls the following php script:

<?php
require("myImageSave.php"); //contains getPNG() function

$QUERY_STRING = $_SERVER['QUERY_STRING'];
$data = explode("&", $QUERY_STRING);

$image = ('./temp.png');
$im = getPNG($data);

if(ImagePNG($im, $image)) {
header('Content-Description:FileTransfer');
header('Content-Type: application/force-download');
header('Content-Type: image/png');
header('Content-Length: ' . filesize($image));
header('Content-Disposition: attachment;
filename="Graph.png"');
header("Content-Transfer-Encoding: binary");
readfile($image);

}

exit();
?>

So Here's the problem. This bit of code saves a pngfileon the server
called temp.png Thisfilecontains the correct png, so I know the
getPNG() function, and everything before that is working correctly.
However, thefilethat gets downloaded to the user's computer is
incorrect and will not display correctly in an image viewer.

I've tried comparing thefilesaved on the server with thefilesaved to
the user's computer, and the two files are *almost* exact, with one
exception: The user's downloaded version has 4 extra linefeed ('0A')
characters at the top.

I realize that there's some superfluous code there -- I've tried quite a
few variations of headers, all with the same results. I know I could
just send thefileto the user without saving it on the server, but this
also gave me the same results.

Any ideas? Why is thefilenot transferring correctly?

Thanks in advance.
remove this content from your file

header('Content-Type: image/png');

regards
amit

Jun 8 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.