PHP - Image Download Help
Hello,
I made that script for previewing all the images in the directory: Code: [Select] $dir = "../images"; $images = glob($dir . "*.jpg"); foreach($images as $image) { echo $image; } Now what i would like to do is to put checkbox to every image. And when i press download button, all the selected images are downloaded. (as .zip, .rar) Thank you very much for your help Similar TutorialsHow do I make it so when a user clicks download, it will download the image from the path to their computer? I have a PHP script that locates a photo in a MySQL database and based on the path /filename stored in the database, retrieves the file and downloads it via the browser, the problem is that it saves as a filename that is the url with no extension. so we pass : dlfile.php?id-10 the script is supposed to get the filename from the stored file and download it with that filename but it downloads the file as 'dlfile?id=10' instead of PIC76534.jpg. where am i going wrong? code below for dlfile.php. or is there a way to ask the user to select a filename when downloading? I know the script is crude but have had it for ages. <?php require( '../js/db.php' ); $id = $_GET[ 'id' ]; $sql = "SELECT * FROM image_archive WHERE id = " . $id; $result = mysqli_query( $conn, $sql ); if ( !$result ) { echo "ERROR: " . mysqli_error( $conn ); } else { $row=mysqli_fetch_assoc($result); //path to the picture you're wanting the viewer to download $path = "/archimages/".basename($row['pic_OriginalPath']); if ( file_exists( $path ) ) { //split the extension and name from eachother $e = explode( '.', $path ); //get the name of the file $file_name = $e[ 0 ]; //image extension $extension = $e[ 1 ]; // Send a header saying we'll be displaying a picture header( 'Content-type: '.$row['pic_Type'] ); // Name the file header( 'Content-Disposition: attachment; filename="' . $file_name . $extension . '"' ); // Path to the picture you're wanting the user/viewer to download readfile( $file_name . '.' . $extension ); } else { echo 'The requested image does not exist.'.$path.'<hr>'; } } ?>
Hi Guys, I have an issue with the following script which is throwing this error Quote( ! ) Parse error: syntax error, unexpected end of file in C:\wamp64\www\script\images.php on line 41 I just can not see the error, can anyone help (not even sure the script is going to work) <?php /* settings */ //folder for images saving $saveDir = "c:\wamp64\www\script\images\"; //database connection // Turn off all error reporting error_reporting(0); $conn = mysqli_connect('localhost', 'root', '', 'tbl_temp_products'); //start and end table row to prevent timeout or hard server work $start = '1'; $end = '200'; /* end of settings */ //query for fetching the image urls $sql = 'SELECT image_url FROM tbl_temp_products ORDER BY id DESC LIMIT ' . $start . ',' . $end . ''; $res = $conn->query($sql); $count = 0; //this is for count the total row fetched $notify = ''; //this is for seeing result or errors while ($row = $res->fetch_assoc()) { $url = $row(['image_url']); $dest = $saveDir . clean($row(['image_url'])) . '.jpg'; if (!file_exists($dest)){ //prevent file overwriting if (copy($url, $dest)){ $notify.= 'image saved: '. $row(['image_url']); $count++; }else{ $notify.= 'ERROR saving image: '. $row(['image_url']); }else{ $notify.= 'image already exists: '. $row(['image_url']); } } //output result echo 'TOTAL IMAGE SAVED: ' .$count .'\n'; echo $notify; ?>
I'm trying to create a series of images on a page but the code won't run. (left table code out): <body> <?PHP /// fyi, the image_link field contains, e.g., artistfoldername/imagefilename.jpg $path = "artWorkImages/"; $art_id ='2'; $QUERY="SELECT art_title, about_art, image_link FROM artWork WHERE art_id = '$art_id'"; $res = mysql_query($QUERY); $num = mysql_num_rows($res); if($num>0){ while($row = mysql_fetch_array($res)){ $artist_name = $row["art_title"]; $about_art = $row["about_art"]; $image_link = $row["image_link"]; print "<img src=$path.$image_link/>"; } } ?> </body> Thanks Allen
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
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 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 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 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. Hello! I'm making a interface in a website do manage files in a ftp hosted on other server. I'm using the function ftp_get() to download a file but I've tried different aways but I dosent seem do be able to download the file directly from the FTP server do the user the only way it works is if I download the file to the website server first then to the client. Is there any way that I can download the file directly to the user without have to hosting it permanently on the website server? Thank you 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 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 This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=316412.0 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 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 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 Little help please? 1. I tried downloading a .txt file on mywebsite but it turned out to be viewing the text file, works on .doc files though 2. Can i have a string variable saved to a file to be downloaded in textfile (.csv) algorith would be like this csv_value = "1,2,3,4,5,6,7,8,9,0"; downloadlink(csv_value,file_name.csv); PS. I used output_buffering = true on my php.ini so i have a little problem if your to suggest to use the header() function. unless there is another loophole. thanks in advance yours truly Art =) 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? |