PHP - [need Help!]when I Use Header To Init Download
go on with the title, i mean, when i click the download button and it starts downloading, i can't refresh the page or go to other adress of my site, it will really take a long time. the header information is something like this (except some judgement)
header("Content-Disposition: attachment; filename=" . $name); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header("Content-Description: File Transfer"); header("Content-Length: " . filesize($adress)); flush(); // this doesn't really matter. $fp = fopen($adress, "r"); while (!feof($fp)) { echo fread($fp, 65536); flush(); // this is essential for large downloads } fclose($fp); it takes me days but i cant find any way...thanks a lot!!! thanks a lot!!! thanks a lot!!! Similar TutorialsI have a small program that is to be used to export data from some MYSQL tables into an Excel spreadsheet. It does the export successfully, the first pass, but each subsequent download selection causes the browser to display the html code as text to the browser. If I disable the excel output, the page redraws successfully un subsequent requests. The header definition I have is ias follows: Code: [Select] $FileInfo = pathinfo(filename); // fix for IE catching or PHP bug issue header("Pragma: public"); header("Expires: 0"); // set expiration time header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); // browser must download file from server instead of cache // force download dialog header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-type: application/x-msexcel"); header("Content-Type: application/download"); // use the Content-Disposition header to supply a recommended filename and // force the browser to display the save dialog. if ($download_filename == "") $download_filename = "download.xlm"; header("Content-Disposition: attachment; filename=".$download_filename.";"); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize($filename)); @readfile($filename); I did not setup this header, because to be honest, these things confuse the heck out of me. Can anyone see what may be wrong with this? Can you give me some suggestions? I can post more of the code if needed, but i believe that this is the area of interest for this issue. Thanks in advance. I put this in the wrong forum and copied it to this one. I apologize ahead of time for the double post. (quote author=radi8 link=topic=333351.msg1569942#msg1569942 date=1305654626) One more question for you all: We just installed a new Linux Ubuntu V 11.04 server with LAMP (Apache2, PHP 5.3.x, MySQL 5.1.4, etc...) all out of the box stuff. I developed (locally) and app where I am exporting some data from MySQL, putting it into a spreadsheet and then sending the Excel file to the client. This all worked fine in my dev setup. BUT... (you know whats coming next) after deploying the app to the new Web Server, when I attempt to export the data, I am successfully creating the Excel file but rather than opening the download dialogue box, the data is being read and sent to the browser window as text! As I mentioned before, this is a new web server, so there may be something missing on the Apache2 setup, or my code may just be crap. Either way, my head hurts and cannot find out what is happening. Here is my header type configuration: Code: [Select] <?php function save($filename, $download=false, $download_filename="") { if (!$download) { return $this->domXML->save($filename); } elseif ($this->domXML->save($filename)) { $realFileInfo = $_SERVER['DOCUMENT_ROOT'].'/truck/admin/export/'.$download_filename; $FileInfo = pathinfo($filename); ob_end_clean(); // fix for IE catching or PHP bug issue header("Pragma: public"); header("Expires: 0"); // set expiration time header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT'); header('Cache-Control: no-store, no-cache, must-revalidate'); // HTTP/1.1 header('Cache-Control: pre-check=0, post-check=0, max-age=0'); // HTTP/1.1 header ("Pragma: no-cache"); //header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); // browser must download file from server instead of cache // force download dialog header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header('Content-Type: application/vnd.ms-excel;'); // This should work for IE & Opera header("Content-type: application/x-msexcel"); header("Content-Type: application/download"); // use the Content-Disposition header to supply a recommended filename and // force the browser to display the save dialog. if ($download_filename == "")$download_filename = "download.xls"; //header("Content-Disposition: attachment; filename=".$download_filename.";"); header("Content-Disposition: attachment; filename=".$realFileInfo.";"); header("Content-Transfer-Encoding: binary"); //header("Content-Length: ".filesize($filename)); header("Content-Length: ".filesize($realFileInfo)); //@readfile($filename); @readfile($realFileInfo); return true; } return false; } ?> I added a screen cap for you to see the output. Can you see anything wrong? Hello everyone! I'm somewhat of a newb in this so anyways I have this script and I try to force a browser to download a file when someone hits a download button. here is what I'm trying: header('Content-Type: application-download'); header('Content-Disposition: attachment; filename=' .basename($dl_id)); readfile($dl_id); It seems to work in Chrome, Firefox, but it doesn't work in IE Any suggestions? All, I found the following script to do the download: $resultsetpic = mysql_fetch_array($resultpic); $filename = "full_size/$resultsetpic[filename]"; // don't accept other directories $size = @getimagesize($filename); $fp = @fopen($filename, "rb"); if ($size && $fp) { header("Content-type: {$size['mime']}"); header("Content-Length: " . filesize($filename)); header("Content-Disposition: attachment; filename=$filename"); header('Content-Transfer-Encoding: binary'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); fpassthru($fp); exit; } This downloads the file fine. However what I would like to do is also refresh the page that the user was just on. So I tried to add this: $resultsetpic = mysql_fetch_array($resultpic); $filename = "full_size/$resultsetpic[filename]"; // don't accept other directories $size = @getimagesize($filename); $fp = @fopen($filename, "rb"); if ($size && $fp) { header("Content-type: {$size['mime']}"); header("Content-Length: " . filesize($filename)); header("Content-Disposition: attachment; filename=$filename"); header('Content-Transfer-Encoding: binary'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); fpassthru($fp); exit; } $target = "work_queue.php"; header("Location:". $target); However, this doesn't reload the page they were on. Any ideas on how to do this? Thanks in advance. I have this line of code in a function but I can't figure out what its doing. $this->Component->init($this); when I print_r($this->Component); i get Component Object ( [__controllerVars] => Array ( [plugin] => [name] => [base] => ) [_loaded] => Array ( ) [_primary] => Array ( ) [__settings] => Array ( ) [_log] => ) There is no init method or object in the Component Object as you can see, so I am lost. Any ideas? Thanks Code: [Select] <html> <head> <title>Testing</title> </head> <body> <?php $config = parse_ini_file("config.ini",1); $email = $config['Email']; $host = $config['Host']; print_r ($host); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $host); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $contents = curl_exec ($ch); $fp = fopen($host.'.txt','w+'); $fw = fwrite($fp, $contents); curl_close ($ch); fclose($fp); ?> </body> </html> Ok, I am having a problem here. I have an automation script I am trying to make, but I hit a road block. I need to open up multiple URL's from using an array of URL's stored in my config.ini file. It looks like this: Code: [Select] Config.ini Host[] = www.websitea.com Host[] = www.websiteb.com Host[] = www.websitec.com Host[] = www.websited.com I want to use curl_setopt to open up each one of the urls stored in $host. If I use... Code: [Select] print_r ($host); ...I get this output: Code: [Select] Array ( [0] => www.websitea.com [1] => www.websiteb.com [2] => www.websitec.com [3] => www.websited.com ) Now, I need to take those websites in the array and open each one of them using curl_unit. I tried doing this... Code: [Select] curl_setopt($ch, CURLOPT_URL, $host); Then Code: [Select] $fp = fopen($host.'.txt','w+'); But I knew that wouldnt work. Basically, I need to open up all of the pages listed in the .ini file and save the HTML Content of those pages to their own text file, that's named after the URL. I figured I could open an array of URLS from my INI and write each one to their own text file. Example: websitea.com.txt (HTML Content from www.websitea.com) websiteb.com.txt (HTML Content from www.websiteb.com) websitec.com.txt (HTML Content from www.websitec.com) websited.com.txt (HTML Content from www.websited.com)
Basically I would like to place a link on my website and have the user download a file, but rather than just right clicking and choosing save target as, the link must be clicked, on the next page the file is fetched and then the client can download the file. How would I go about setting this up please? I have made a Php program that downloads an Inno setup installation file for installing a program. However, if I for one or another reason want to make a new download of the same Inno setup installation file, the previous file will still be found in the Download folder. Each of the downloads get a number in parenthesis, setup(1), setup(2), setup(3) etc. However, I wondered if it is posible to erase the previous file in the same process as I download a new one, so that however many downloads I do, there will all the time only be one occurence of this file in the Download folder. The download code is as follows: $exe = "Inno script/Test_setup.exe"; header("Content-Type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"Test_setup.exe\""); header("Content-Length: " . filesize($exe)); readfile($exe); Thanks in advance. Sincerely
This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=350027.0 Hello everyone, I'm just starting out with PHP as I need to create an online bookstore for a school project. I'm working by a magazine which should teach you exactly how to do this using PHP, but I've had a bunch of problems with the code they use and I don't really know what's going on. Anyway, this looks really simple and basically what it does is allows you to post a comment on a book, then returns you to the book's page. Problem is, I'm getting the Header may not contain more than a single header, new line detected. error and I can't figure out why. I've tried researching into the matter but all the cases I found had to do with returning to an url, which is not my case. Anyway, here's the snippet of code: The form: Code: [Select] <div style="width:400px; border:1px solid #ffffff; background-color:#F9F1E7; padding:5px"> <b>Adauga opinia ta:</b> <hr size="1"> <form action="adauga_comentariu.php" method="POST"> Nume: <input type="text" name="nume_utilizator"><br><br> Email: <input type="text" name="adresa_email"><br><br> Comentariu: <br> <textarea name="comentariu" cols="45"></textarea><br><br> <input type="hidden" name="id_carte" value="<?=id_carte?>"> <center><input type="submit" value="Adauga"</center> </form> </div> The script adaugare_comentariu.php: Code: [Select] <?php ob_start(); include("conectare.php"); $numeFaraTags=strip_tags($_POST['nume_utilizator']); $emailFaraTags=strip_tags($_POST['adresa_email']); $comentariuFaraTags=strip_tags($_POST['comentariu']); $sql="insert into comentarii (id_carte, nume_utilizator, adresa_email, comentariu) values(".$_POST['id_carte'].", '".$numeFaraTags."','".$emailFaraTags."','".$comentariuFaraTags."')"; mysql_query($sql); $inapoi="carte.php?id_carte=".$_POST['id_carte']; header("location:urldecode($inapoi)"); ob_end_flush(); ?> conectare.php connects to the mysql database. $inapoi is the variable which returns the user to carte.php (the book he posted a comment on), where id_carte is the book's unique id. I'm getting Header may not contain more than a single header, new line detected on line ten, which is the header line. Can anyone help me? I've been stumped on this for a few days now and I've just let it pass and started working on other bits, but it's bugging me too much and I'd like to fix it. Hi,i will like to know how to put a file on my website for people to download..please how should i go about it? thanks merry christmass i need a php script for download the enire directory with the files . i need a script for that .. ex- floder 11 it contains 23 files ....when user click a button i want to download the floder 11 and inside the floder 11 has to be allow files are included in the folder I've written the following code: Code: [Select] <?php echo '<img src="Sheph.png" />'; function Wad(){ if (file_exists("Sheph.png")) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename("Sheph.png")); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize("Sheph.png")); ob_clean(); flush(); readfile("Sheph.png"); exit; } } Wad(); ?> What I want to do is show the image AND show a file download window. The problem is that it shows the download window but the image doesn't appear. and if I comment the function and keep the echo part only,the image appears normally. What's wrong ? I created this php class which recieves an sql query from flash and then produces a csv file for download. The code in its current form produces a csv file on the server. However, I want it to save on the client computer with a 'Save As' dialog box. I am testing on localhost. Here is the class: <?php class CSVExport { public function __construct() { require_once 'Zend/Date.php'; require_once 'DatabaseConnection.php'; } public function exportCSV($query) { $result = mysql_query($query); $fname = 'CSVFile.csv'; $headers = array(); $rowArray = array(); $numFields = mysql_num_fields($result); for ($i = 0; $i < $numFields; $i++) { $headers[] = mysql_field_name($result , $i); } $fp = fopen($fname, 'w'); if ($fp && $result) { header('Content-Type: application/csv'); header('Content-Disposition: inline; filename='.$fname); fputcsv($fp, $headers); while ($thisrow = mysql_fetch_row($result)) { fputcsv($fp, $thisrow); } readfile($fname); fclose($fp); } die; } } Thanks I have music (my own) that I want to put on my website for download, and I was wondering if there is a way to keep track of how many people download each song, by tracking the clicks and displaying that number in a css-styled box. Anyone know how this is done? I don't need help with the css, just the php. On my website, I have a download folder containing several files ranging in size from 6Mb to 700Mb. Users have no problems downloading the smaller files but often have problems downloading the files over 500Mb. We also have an archive of all the files located on a sub-domain on a server in England. Those users that are having problems downloading large files from the main site usually have no problem downloading the large files if we send them the URL for the archive. I've added a button that calls the following script so the users can choose where to download from but have no idea how to code the script. Here's what I've tried: // ukdloader script <?php $php_scripts = '../../php/'; require $php_scripts . 'PDO_Connection_Select.php'; require $php_scripts . 'GetUserIpAddr.php'; function ukdloader($l_filename=NULL) { $ip = GetUserIpAddr(); if (!$pdo = PDOConnect("foxclone_data")) { exit; } if( isset( $l_filename ) ) { echo <a href="http://foxclone.org/".$l_filename"> /* This is the archive site */ $ext = pathinfo($l_filename, PATHINFO_EXTENSION); $stmt = $pdo->prepare("INSERT INTO download (address, filename,ip_address) VALUES (?, ?, inet_aton('$ip'))"); $stmt->execute([$ip, $ext]) ; $test = $pdo->query("SELECT id FROM lookup WHERE INET_ATON('$ip') BETWEEN start AND end ORDER BY start DESC, end DESC"); $ref = $test->fetchColumn(); $ref = intval($ref); $stmt = $pdo->prepare("UPDATE download SET ref = '$ref' WHERE address = '$ip'"); $stmt->execute() ; } else { echo "isset failed"; } } ukdloader($_GET["f"]); exit; Thanks in advance. Hi all, I'm trying to make a PHP script that downloads a file off a remote FTP server and serves the file to the visitor with a limited download speed. Right now I'm using fopen() like this: Code: [Select] $path = "ftp://".$username.":".$password."@".$server."/"; $fullPath = ($path.$fileName); $speed = 400; // 400 kb/s download rate header("Cache-control: private"); header("Content-Type: application/octet-stream"); //header("Content-Length: ".filesize($fullPath)); header("Content-Disposition: filename=\"".$fileName."\""); flush(); $fd = fopen($fullPath, "r"); while(!feof($fd)) { echo fread($fd, round($speed*1024)); flush(); sleep(1); } fclose ($fd); The file does download but with the wrong speed (8kb/s), does anyone know why? When I run the same code with a local file instead of a file on FTP it works fine and downloads at the speed I set it to.. Thanks Hi Guys, I'm putting together a project which requires members of a site to upload files(mostly word files) into a mysql db. I can get the files uploaded successfully but when it comes to downloading them i do have a bit of trouble. When i click on the download link i just get a file called 'download.php' instead of the actual file in the database. I have been working on this link for the past two weeks and think maybe its time for some help, here's my code if you wouldn't mind having a look? upload.php: <?php session_start(); ?> <html> <head> <link href="default.css" rel="stylesheet" type="text/css" media="screen" /> </head> <body> <!-- start header --> <div id="header"> </div> <div id="menu"> <ul> <li><a href="#">Home</a></li> <li><a href="member.php">Member</a></li> <li><a href="logout.php">Log Out</a></li> </ul> </div> <div id = "content"> <form method="post" enctype="multipart/form-data"/> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"/> <input name="userfile" type="file" id="userfile"/> <input type="submit" name="upload1" id="upload1" value=" Upload "/> </form> </div> <?php if(isset($_POST['upload1']) && $_FILES['userfile']['size'] > 0) { $connect = mysql_connect ("localhost","root","") or die ("Couldn't connect!"); mysql_select_db("upload") or die ("Couldn't find db"); $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } $query = "INSERT INTO upload (name, size, type, content ) ". "VALUES ('$fileName', '$fileSize', '$fileType', '$content')"; mysql_query($query) or die('Error, query failed'); echo "<br>File $fileName uploaded<br>"; } ?> <div id = "nav"> </br> <a href = "upload.php"><img src = "images\add-itemGreen.gif"/></a><b>Upload</b></br> </br> <a href = "download.php"><img src = "images\download.gif"/></a><b>Download</b></br> </br> <a href = "discussion.php"><img src = "images\chat-.gif"/></a><b>Discussion Board</b></br> </div> <div id="footer"> </div> </body> </html> View.php: <?php ini_set('display_errors', E_ALL); //include "open_db.inc"; $connect = mysql_connect ("localhost","root","") or die ("Couldn't connect!"); mysql_select_db("upload") or die ("Couldn't find db"); $sql = "SELECT * FROM upload "; $sql .= "ORDER BY name ASC"; $result = mysql_query($sql, $connect); $rows = mysql_num_rows($result); echo "<table>\n"; echo " <tr>\n"; echo " <td>id</td>\n"; echo " <td>name</td>\n"; echo " <td>type</td>\n"; echo " <td>size</td>\n"; echo " <td>Description</td>\n"; echo " <td> </td>\n"; echo " </tr>\n"; while ($rows = mysql_fetch_object($result)) { echo " <tr>\n"; echo " <td>$rows->id</td>\n"; echo " <td>$rows->name</td>\n"; echo " <td>$rows->type</td>\n"; echo " <td>$rows->size</td>\n"; echo " <td>" . stripslashes($rows->description) . "</td>\n"; echo " <td>( <a href='download.php?id=$rows->id'>Download</a> )</td>\n"; echo " </tr>\n"; } mysql_free_result($result); mysql_close($connect); ?> download.php: <?php ini_set('display_errors', E_ALL); if (isset($_GET['id'])) { $id_files = $_GET['id']; // include "open_db.inc"; $connect = mysql_connect ("localhost","root","") or die ("Couldn't connect!"); mysql_select_db("upload") or die ("Couldn't find db"); $sql = "SELECT id, name, type, size, content FROM upload WHERE id=$id"; $result = @mysql_query($sql, $connect); $dataT = @mysql_result($result, 0, "bin_data"); $name = @mysql_result($result, 0, "name"); $size = @mysql_result($result, 0, "type"); $type = @mysql_result($result, 0, "size"); header("Content-type: $type"); header("Content-length: $size"); header("Content-Disposition: attachment; name=$name"); header("Content-Description: PHP Generated Data"); header("Content-transfer-encoding: binary"); echo $dataT; } else { echo 'id_files not set'; } ?> I'm thinking that it could be something to do with the bin_data on the download.php page. Any help would be great. Thanks Hi, I don't want to reveal the real download link to my users for some reason, so I place the file here, for example: http://www.domain.com/download/secure-file.exe Now I want to write a simple php code and link to this address, for example: http://www.domain.com/download/download.php Now this php script read that file and send it to user, without revealing the real file location to user. Of course there is no resume download now, but I don't care. Anyone knows a code snippet for that, or can help me to the right position? I am a newbie in PHP. Thank you. How can i create a download page that allows you to download *.php files on your server. Because if you just click on it it sends you to the page but I just want it to download it. Also i want to be able to do that on the .jpeg files. Thanks Jragon |