如何使用PHP从服务器文件夹中检索文件并使用javascript在网页上显示/下载? [英] How to retrieve files from server folder using PHP and display/download it on a webpage using javascript?

查看:117
本文介绍了如何使用PHP从服务器文件夹中检索文件并使用javascript在网页上显示/下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个网站,用户可以上传姓名,联系方式等用户信息,并上传pdf格式的简历,该简历存储在uploads文件夹中,并将其完整路径存储在数据库列中。

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.

我想构建一个简单的网站界面,它将检索所有文件以及用户信息(如上所述,我拥有数据库中所有文件的路径)并在界面上显示。如何使用PHP和javascript实现它?

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:我使用的是godaddy。

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')"; 


推荐答案

它非常宽泛,所以我会尝试简要介绍。

Its very broad so i will try to brief.

以下是您可以遵循的步骤

Here is the steps you could follow


  1. 正如您所说已经创建了上传和插入组件,它的工作原理。所以我将离开那一部分直接进入下一步。
    您想要实现的是显示已保存的数据以及上传的文件。

  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.

因此您需要先检索已保存的数据(用户信息)和数据库表中的cv的文件夹路径。要使用php,请使用 PDO mysqli 。用户选择查询以从数据库表中选择匹配的内容。请参阅使用PDO语句选择表格数据

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

用户HTML和CSS来设计用户界面。通过php向设计显示获取的数据。包括pdf文件的下载链接。我将在下面展示一个php下载文件的例子。请参阅如何在HTML链接中下载PDF文件?

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?

pdf下载的链接可能是这样的

Link to the pdf download could be like this

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

download.php可能是这样的

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); 

我希望这有帮助:)

这篇关于如何使用PHP从服务器文件夹中检索文件并使用javascript在网页上显示/下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆