note 105274 rejected from function.fopen by thiago
| From: | thiago@php.net | Date: | Sun, 07 Aug 2011 00:36:23 +0000 |
| Subject: | note 105274 rejected from function.fopen by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-182288@lists.php.net to get a copy of this message | ||
Note Submitter: thealthomson at gmail dot com
----
Hello, I'm trying to make a "download" button work with images, and I'm running
into some issues. I'm trying to make it as dummy-proof as possible as the users of this site
will be just about everyone, not necessarily computer-literate.
Also, sorry in advance if I'm missing something, I just started learning PHP about two weeks
ago :P
First of all, in the main page, I have this, where the user can click a download button:
<?php
echo '<FORM METHOD="GET" ACTION="download.php"><Input
name="img" value='.$client_val.' type=hidden><Input
type="submit" value="Download/Save"></FORM>';
?>
where the "client_val" variable is the name of the image file (the above code is actually
a snippet from a looping-table-making thing).
THEN, I go over to the called "download.php" file here:
<?php
//$id=$_SESSION["id"];
//$username=$_SESSION['username'];
$username='Alvin'; //CHANGE THIS!!!!!! (Normally this info would come from the session but
since we're in test mode we bypass it to make things easier)
$id='1'; //AND THIS!!!!
$ImageName = stripslashes($_GET['img']);
$ImageLoc =
'../data/client_private/'.$id.'_'.$username.'/client/'.$ImageName;
header("Cache-Control: public, must-revalidate");
header("Content-Type: application/octet-stream");
header("Content-Length: ".filesize($ImageLoc) );
header("Content-Disposition: attachment; filename=".$ImageName);
header("Content-Transfer-Encoding: binary\n");
$fp = fopen($ImageLoc,'r');
fread($fp,filesize($ImageLoc));
fclose($fp);
print $buffer;
header("location:photo_view.php");
?>
SO the actual problem is when all is said and done, the downloaded file is un-readable, and I cannot
for the life of me figure out why.
For testing purposes I used the following inserts to check if it is actually finding the correct
file, and it is and it is displaying it fine.:
<?php
//$id=$_SESSION["id"];
//$username=$_SESSION['username'];
$username='Alvin'; //CHANGE THIS!!!!!!
$id='1'; //AND THIS!!!!
$ImageName = stripslashes($_GET['img']);
$ImageLoc =
'../data/client_private/'.$id.'_'.$username.'/client/'.$ImageName;
echo $ImageLoc.'<BR>';
echo '<IMG SRC="'.$ImageLoc.'" Height=100>';
/*
header("Cache-Control: public, must-revalidate");
header("Content-Type: application/octet-stream");
header("Content-Length: ".filesize($ImageLoc) );
header("Content-Disposition: attachment; filename=".$ImageName);
header("Content-Transfer-Encoding: binary\n");
*/
$fp = fopen($ImageLoc,'r');
fread($fp,filesize($ImageLoc));
fclose($fp);
echo 'FP: '.$fp.'<BR>ImageLoc: '.$ImageLoc;
print $buffer;
//header("location:photo_view.php");
?>
I'm not claiming to have written the code above, I found it on another page and modified it,
and with my limited (thus far) knowledge I actually have no idea what the header() stuff does (like
I know its essential, but I don't know how it works and would be happy if anyone would be able
to point out what each part of it does).
Another note, I don't normally use the GET method, I use POST, but the original creator of this
script used GET, so I just followed suit (at least until it works, then I may change it to POST if
there are no issues).
Thanks in advance!! :D