如何使用PHP从服务器文件夹中检索文件,并使用javascript在网页上显示/下载文件?

时间:2022-04-09 00:30:24

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.

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

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?

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

PS : Am using godaddy.

PS:我在用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')"; 

1 个解决方案

#1


2  

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

    因此,您需要首先从数据库表中检索保存的数据(用户信息和cv的文件夹路径)。为此,在php中使用PDO或mysqli。用户选择query从数据库表中选择匹配的内容。使用PDO语句查看选择表数据。

  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?

    用户HTML和CSS设计用户界面。通过php将获取的数据显示到设计中。包括pdf文件的下载链接。下面我将展示一个php下载文件的示例。查看如何使PDF文件可在HTML链接中下载?

Link to the pdf download could be like this

链接到pdf下载可以是这样。

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

download.php could be like this

下载。php可以是这样的

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

我希望这能帮助你:

#1


2  

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

    因此,您需要首先从数据库表中检索保存的数据(用户信息和cv的文件夹路径)。为此,在php中使用PDO或mysqli。用户选择query从数据库表中选择匹配的内容。使用PDO语句查看选择表数据。

  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?

    用户HTML和CSS设计用户界面。通过php将获取的数据显示到设计中。包括pdf文件的下载链接。下面我将展示一个php下载文件的示例。查看如何使PDF文件可在HTML链接中下载?

Link to the pdf download could be like this

链接到pdf下载可以是这样。

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

download.php could be like this

下载。php可以是这样的

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

我希望这能帮助你: