PHP script for download images

software development



i was running into a curious problem…
I have a php script which downloads a image from a secure folder (…/) to the users folder.
If you run this scrip directly over the browsers addressbar it comes up with the download window where to store.
But if i run it linked over a button from my site nothing is happening. Looking in the access.log file shows my the right string for running the script - copy paste it to the browserbar and … the dialog pop’s up.

Have anyone encountered souch a behavior?
Any ideas?



With out seeing the code, I can only speculate. It sounds like your php code only created a link to the image.
Instead Create another php script that has the header info for the image and use imagegd to send to the browser.

My website


Maybe as an additional information, the script is called from an swf by loadVariablesNum.
If i do it with getURL… and _blank it works also, but i dont want to open a blank window for downloading…

Here is the code:

<?PHP require("../sicher/include/verbinden.php"); /** * Foto downloaden * * Systemvoraussetzung: * Linux * PHP 4 >= 4.2.0, PHP 5 * * Foto downloaden * */ function readfile_chunked($filename,$retbytes=true) { # from: $chunksize=1*(64*1024); // how many bytes per chunk $buffer=''; $cnt=0; $handle=fopen($filename, 'rb'); if ($handle===false) { return false; } while (!feof($handle)) { $buffer=fread($handle,$chunksize); echo $buffer; ob_flush(); flush(); if ($retbytes) { $cnt+=strlen($buffer); } } $status=fclose($handle); if ($retbytes && $status) { return $cnt; // return num. bytes delivered like readfile() does. } return $status; } function makeDownload($file, $full, $type) { if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) { $file = preg_replace('/\./', '.', $file, substr_count($file, '.') - 1); } //Begin writing headers header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: public"); header("Content-Description: File Transfer"); //Use Content-Type header("Content-Type: application/octet-stream"); //Force the download $header="Content-Disposition: attachment; filename=".$file.";"; header($header ); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize($full)); ignore_user_abort(true); $sent = readfile_chunked($full); if(!connection_aborted()){ unlink(); return $sent; } else { //perhaps some logging, which is why I set the ignore_user_abort() to true return 0; } } $type = 'application/zip'; $checkedCat = (int)$_GET["cat"]; $checkedSub = (int)$_GET["sub"]; $invoice=$_GET['Invoice']; $custom=$_GET['pin_code']; $itemname=$_GET['Itemname']; if(!preg_match("/^[0-9a-f]{32,32}$/i", $invoice)){ exit; } if(!preg_match("/^photo_[1-9][\.][0-9]{1,9}$/", $itemname)){ exit; } $dir = '../sicher/download/images/'.$checkedCat.'/'.$checkedSub.'/'; $invoice = AESdecode($invoice); // PIN auf Gültigkeit Prüfen $sql="select idx from customer_pin where invoice = '".$invoice."' and pin = '".$custom."' and access > (NOW() - INTERVAL 24 HOUR)"; $query=mysql_query($sql, GetMyConnection()); $anz = mysql_num_rows($query); CleanUpDB(); if ($anz<1){ exit; } if(($checkedCat!=0) && ($checkedSub!=0)) { if(!empty($_GET['file']) && !preg_match('=/=', $_GET['file'])) { if(file_exists ($dir.$_GET['file'].'.zip')) { $fsize = filesize($dir.$_GET['file'].'.zip'); $bsent = makeDownload(basename($_GET['file']), $dir.$_GET['file'].'.zip', $type); if($bsent==$fsize) { // Download Zähler erhöhen $sql="update paypal_cart_info set downloaded = downloaded + 1 where itemname = '".$itemname."' and invoice = '".$invoice."' and custom = '".$custom."'"; $query=mysql_query($sql, GetMyConnection()); CleanUpDB(); } } } } ?>


Finally i got it worked.

I use now print instead of echo.
Don’t know why this makes the different, but i must not know all secrets…