PHP - Download File Link
I'm wanting to have users able to download a file, but I'm wanting it to be a file that only users who are logged in can download. Basically, I don't want a user to be able to enter the URL and download it while not being logged in. Does anyone know what I would use to do this? Thanks.
Similar TutorialsHi, Just looking how to create a link to download a file located on the server, it will be files like .doc etc. Cant seem to find anything on the net so i think i need php but may be html. Can anyone point me in the right direction? Thanks p.s. it will be used for more than one file so im looking for a small ish amount of code. Hello I already know this is possible with php <?php $file = 'monkey.gif'; if (file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($file)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); exit; } ?> But I am wondering is it possible to just send over a string of data stored in like $data and create a file clientside (nothing server side). So i'm guessing readfile() has to be replaced with something Dear PHPFreak members, I have been searching a solution for serving a file download via my website. The file to be downloaded is actually on a remote server. What i need is a code that serves as a download medium without actually downloading the file to my web server. Something like masking file url Can anyone help me with it ??? Have been trying this since days. But no solution till date.
Hi guys,
echo " Download <a href=".$file_path." download>Here</a>"; ?>
Didnt work, just took me to the path of the image. What i want is a link and when clicked the download window pops out.
Thanks Hi, I have This Url After Click Link For Access and download file . Code: [Select] index.php?module=Files&action=show&id=default&download=yes&id=$_REQUEST[id] I set my mod_rewrite to : Code: [Select] RewriteRule ^files/(.*)/?$ index.php?module=Files&action=show&id=default&download=yes&id=$1in download page i have this for accept & download file : Code: [Select] if ($_REQUEST[download] == 'yes') { //check and create MYSQL Result echo " <meta http-equiv=\"refresh\" content=\"0; url=$cms[url]\">"; // $cms[url] = test.jpg } Now generate url (domain.com/files/test.jpg) but not refresh and not show file for download ( new Page)! what's problem ! thaks for any help Can php encrypt a link so that it's hidden or expires after a download or x number of days? An example of this use would be on music download sites. Hi, I have one webhost (A) that supports no PHP and has unlimited bandwidth. I have a second webhost (B) that does support PHP, but has limited bandwidth. The website is on host A and contains a 5mb file that people can download and I'd like to count the downloads. But now I need to use host B to count the downloads. I'm using this small script on host B to count the downloads. <?php $hits=file("count.txt"); $hits[0]++;$fp=fopen("count.txt","w"); fputs($fp,$hits[0]); fclose($fp); header("Location: http://webhost-A.com/files/download.zip"); ?> The download link people see on my website is: http://webhost-B.com/download.php Generally this works fine, but I found out that people can still find the direct link with download managers and then put that direct link on their website, which prevents me from counting those downloads. A solution I found is to use ReadFile. But the problem is that when using ReadFile, host B first downloads the file from host A and then sends it to the user and that's exactly what I don't want. Is it possible to let people download the file directly from host A without showing the direct download link in any way and also count the downloads? So I am trying to download an image created with my php script. I use the following for the download: Code: [Select] <?php session_start(); $filename = $_SESSION['imgOut']; header('Content-type: application/octet-stream'); header('Content-Disposition: attachment; filename='.$filename ); ?> The file downloads however instead of downloading "example.jpg" from the /temp folder , it downloads "temp_example.jpg" which of course is an empty file. I am sure I am approaching this the wrong way, any suggestions? Hey guys, I'm going to start working on a simple php/mysql project, and wondering if you guys could point me in the right direction. Here is what I want to do: 1. User is presented with a simple form asking a series of questions (including email address) 2. Based on the answers, a specific download link is emailed to the user 3. store all inputted information in the database. I'm still not sure if I want the email process to be automated, or "held" until the information is reviewed, then download link sent. Does anyone know if a script already exist for this? I don't need to create user accounts, I just want to obtain user information every time they want to download something. Thanks for any help. 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 ? 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 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 Hiya guys After getting everything else working how I expected, I'm sort of struggling on the last step. Downloading I have an upload.php file that allows me to upload a file to Mysql, the fields available a upid - Primary id - Need to link this to the logged on user id name type size content The upload works perfectly Can anyone help with implmenting it to the profile.php (1st page after login) On profile page I have: Welcome "username" from Session Dynamic Table display his user id, username and password at the moment, this will be changed as not needed tho. I am using the sessions MM_Username to pass from the login The table for Login looks like: id - primary username password I assume that if I can copy the ID from login and put it in ID in Upload and add colums to dynamic table to show the upload file, will this make that file only available to logged in user? Cheers Hi PHP freaks!!
I have manage to make a script to upload file in database and its working. I will share to you the codes so for other viewers and readers to use also.
if($result){ if($_FILES['LRCard']['name'] != ""){ $filename = $_FILES['LRCard']['name']; $ext = strrchr($filename,"."); $LRCardname = $student_id; $LRCardname .="_". $filename; if($ext ==".jpg" || $ext ==".jpeg" || $ext ==".JPG" || $ext ==".JPEG" || $ext ==".gif" || $ext ==".GIF"){ $size = $_FILES['LRCard']['size']; if($size > 0 && $size < 5000000){ $archive_dir = "LRCards"; $userfile_tmp_name = $_FILES['LRCard']['tmp_name']; if(move_uploaded_file($userfile_tmp_name, "$archive_dir/$LRCardname")){ /* if LRC is successfully uploaded then LRC is stored in database. */ mysql_query("update student_information set LRCard='$LRCardname' where student_id='$student_id'", $link_id); $flag = "success"; if(mysql_error()!=null){ die(mysql_error()); } } else{ if(file_exists('LRCard/' . $LRCardname)) { unlink('LRCards/' . $LRCardname); } rollbackData(); } } else{ if(file_exists('LRCards/' . $LRCardname)) { unlink('LRCard/' . $LRCardname); } rollbackData(); die("You can upload LRCard of 5 MB size only. Please, try again."); } } else{ if(file_exists('LRCards/' . $LRCardname)) { unlink('LRCards/' . $LRCardname); } rollbackData(); die("You can upload LRCard of .jpg, .jpeg, .gif extensions only. Please, try again. "); } } } else{ $flag="error"; } if($flag == "success"){ mysql_query(" COMMIT "); $flag="success"; if(mysql_error() != null){ die(mysql_error()); } }Now, my problem is how to make a Php script to DOWNLOAD this uploaded file considering the 1. file path 2. file name 3. file extension. 4. student_id (to determine the specific file from a specific student) My plan is to make a download button which hyperlink to download.php and after clicking that button the specified file to a specific student id will automatically be downloaded by the browser. I was able to upload the files to the DB but now am trying to retrieve it from the table but it's not working.
my code below
<?php while($row = mysql_fetch_array($query1)) { (list($id, $name) = mysql_fetch_array($query1)); echo"<tr>"; echo"<td> $row[category]</td>"; echo"<td> $row[description]</td>"; echo"<td> $row[amount]</td>"; ?> <td> <a href="approved_req.php?id=<?php echo urlencode($id);?>" ><?php echo urlencode($name);?></a> </td> <?php echo"<td> $row[status]</td>"; ?> <td><a class="btn btn-success" <?php echo" href='invoice.php?id=$row[id]'> Pay </a></td>";?></a> <?php echo"</tr>"; } // } ?> </table> <?php if(isset($_GET['id'])) { // if id is set then get the file with the id from database //$con = mysql_connect('localhost', 'root', '') or die(mysql_error()); //$db = mysql_select_db('test', $con); $id = $_GET['id']; $query = "SELECT name, type, size, content " . "FROM requisition WHERE id = '$id'"; $result = mysql_query($query) or die('Error, query failed'); list($name, $type, $size, $content) = mysql_fetch_array($result); header("Content-length: $size"); header("Content-type: $type"); header("Content-Disposition: attachment; filename=$name"); ob_clean(); flush(); echo $content; mysql_close(); exit; } ?>Thanks in advance The following code works fine when using chrome or firefox, but fails with IE. Error: Requested site is either unavailable or cannot be found. Question: Does anyone see anything here that is a known bug with IE that I don't know about? Code: [Select] public function download(){ if($this->validateFile()){ if(!headers_sent()){ header('Content-type: '.$this->getType()); header('Content-Disposition: attachment; filename="'.$this->name.'"'); readfile($this->file); } else{ echo 'Headers have already been sent and are preventing the file download from taking place.'; } return true; } return false; } //I felt this function might be relevant to the issue. I know, it's kind of a ghetto function, I'll improve on it some other time though. private function getType(){ $types=array( 'txt' => 'text/plain', 'mp3' => 'audio/x-mp3', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png', 'doc' => 'application/msword', 'swf' => 'application/x-shockwave-flash', 'flv' => 'video/flv', 'mpg' => 'video/mpeg', 'mpeg' => 'video/mpeg', 'avi' => 'video/x-msvideo', 'rtf' => 'application/rtf', 'pdf' => 'application/pdf', 'zip' => 'application/zip', 'exe' => 'application/octet-stream', 'xls' => 'application/msexcel', 'xlsxl' => 'application/msexcel'); if(!$types[$this->getExt()]==''){ return $types[$this->getExt()]; } return 'application/octet-stream'; } Hi there, I have a cronjob for PHP which generates a table with dynamic data and this table is exported to excel when run. When i have one table it downloads one excel file. I want to download multiple excel files using a for loop. Please Help... Any suggestions will be helpful.. Thank you, Regards, Rohit On PHP 5.2.14. I want to make files downloadable from my website. This seems easy enough: <?php $file = "downloads/someFile.txt"; // Set headers header( "Content-Description: File Transfer" ); header( "Content-Type: application/force-download"); header( "Content-Length: " . filesize( $filename ) ); header( "Content-Disposition: attachment; filename=$file"); // header( "Content-Transfer-Encoding: binary"); readfile( $file ); unlink( $file ); ?> You'll notice, however, that I have "unlink" at the end of this file. That's exactly what I want: to remove the file from the server once it has been downloaded, to prevent anyone else from downloading it. Here's the conundrum: When the user navigates to this URL, browsers offer the choice of Open/Save/Cancel. I don't want users to "open", and if the user presses "cancel", well then I probably don't really want the file deleted. In short: I want the file SAVED, only. And I want to delete the file immediately after it has been downloaded. Thoughts? The other day I noticed someone had post something like this, well this was actually the solution to the problem. But it got me thinking about a project of mine that I am working on and the need to pump out a downloadable file. We are storing most of the files in a database using base64 encoding the 2 key types of files we are storing are images and PDF's mostly pdf's anyway where I am wanting to go with this is, is there anyway to take the base64 encoded file and get it to download through this, or am I tackling the idea in the wrong way? <?php $filename = 'somename.txt'; //this would obviously be changed according to the file type we would output $data =<<<DATA I know my data would go here for the given file once decoded. DATA; header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . $filename); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . strlen($data)); echo $data; ?> 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? |