Jump to content

how to DOWNLOAD file from database


Shadow_Walker

Recommended Posts

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.

 

Link to comment
https://forums.phpfreaks.com/topic/288559-how-to-download-file-from-database/
Share on other sites

  • 4 weeks later...

Here is a tutorial I have on downloading a file in php.  You'll have to modify it to accomodate your database query, but the concept of the download is exactly the same either way.

http://amecms.com/article/PHP-Force-File-Download-With-File-Whitelist

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.