How to retrieve files from server folder using PHP and display/download it on a webpage using javascript?

mustafa1993 picture mustafa1993 · Sep 6, 2015 · Viewed 10.2k times · Source

I have build a website where a user can upload user information like name, contact etc. and upload a resume in pdf format which am storing in a "uploads" folder and storing its full path in database column.

I want to build a simple website interface that will retrieve all files along with user info (am having path of all files in database as mentioned above) and display it on interface. How can I achieve it using PHP and javascript?

PS : Am using godaddy.

Here is how I uploaded file :

$path = "/home/easyemployment/public_html/uploaded/";
$tmp  = $_FILES['userfile']['tmp_name'];
$fileName = $_FILES['userfile']['name'];
move_uploaded_file($tmp, $path.$fileName);
$fullpath = $path.$fileName;
$query="INSERT INTO seeker VALUES ('$unm', '$company', '$email', '$city', $contact, '$fullpath')"; 

Answer

aimme picture aimme · Sep 6, 2015

Its very broad so i will try to brief.

Here is the steps you could follow

  1. As you said you have already created uploading and inserting components and it works. So i will leave that part and go directly to the next step. What you want to achieve is show the saved data along with the uploaded file.

  2. So you need to first retrieve the saved data (user info and folder path to the cv) from database table. To do this use PDO or mysqli with php. User Select query to select matching content from database table. See Selecting table data with PDO statements

  3. User HTML and CSS to design the user interface. Show the fetched data to the design through php. including the download link to the pdf file. i will show an example of php download file below. see How to make PDF file downloadable in HTML link?

Link to the pdf download could be like this

 <a href="download.php?file=pdffilename">Download CV</a>

download.php could be like this

header("Content-Type: application/octet-stream");

$file = $_GET["file"] .".pdf";
header("Content-Disposition: attachment; filename=" . urlencode($file));   
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");            
header("Content-Length: " . filesize($file));
flush(); // this doesn't really matter.
$fp = fopen($file, "r");
while (!feof($fp))
{
    echo fread($fp, 65536);
    flush(); // this is essential for large downloads
} 
fclose($fp); 

I hope this help :)