note 105274 added to function.fopen

From: Date: Sat, 06 Aug 2011 13:39:39 +0000
Subject: note 105274 added to function.fopen
Groups: php.notes 
Request: Send a blank email to php-notes+get-182280@lists.php.net to get a copy of this message
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 ---- Server IP: 69.147.83.197 Probable Submitter: 74.210.60.107 ---- Manual Page -- http://www.php.net/manual/en/function.fopen.php Edit -- https://master.php.net/note/edit/105274 Del: integrated -- https://master.php.net/note/delete/105274/integrated Del: useless -- https://master.php.net/note/delete/105274/useless Del: bad code -- https://master.php.net/note/delete/105274/bad+code Del: spam -- https://master.php.net/note/delete/105274/spam Del: non-english -- https://master.php.net/note/delete/105274/non-english Del: in docs -- https://master.php.net/note/delete/105274/in+docs Del: other reasons-- https://master.php.net/note/delete/105274 Reject -- https://master.php.net/note/reject/105274 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#182280) next »