drix47193 2019-07-14 15:48
浏览 414

如何在MYSQL中存储PDF文件的文件引用以在php中检索

I am trying to store a reference to a PDF file in MySQL, I then want to load a few files on to a webpage. When I click on the file name I would like the file to load. How do I store the reference in MySQL and access it via PHP?

I have read many posts advising not to add the file directly to MySQL and to just store a reference, but I can't find a tutorial or instructions to guide how to store only a reference/path of the file in MySQL and access via PHP.

<?php 
     $file = 'BinarySearch.pdf';
     $filename = 'BinarySearch.pdf';
     header('Content-type: application/pdf');
     header('Content-Disposition: inline; filename = "' .$filename . '"');
     header('Content-Transfer-Encoding: binary');
     header('Accept-Ranges: bytes');
     @readfile($file);
?>

The above code only opens the file directly stored on the server directly. I am not sure of how to store the path in MySQL. Then on my webpage I want to click a button to retrieve the file. (I have multiple files I want to store.) Thank you so much in advance but I can't find how to do this at all and its integral for my project.

  • 写回答

1条回答 默认 最新

  • duanhuan3705 2019-07-14 16:09
    关注

    Although the question isn't very clear, I'll take a stab at it with some assumptions.

    I strongly discourage storing file as string in database as you've learned.

    You have couple of options -

    Use a cloud provider like Amazon S3 to push the pdf file to a bucket which returns a URL referencing that file. You then store that URL into your table as a reference as the API would return the link of the uploaded file. When you need to render the file when requested, simply locate the file via reference in your database and render it like you are currently doing. This will require more work as you need to setup your cloud provider account, provision a bucket (storage) and add SDK to your project to communicate with the storage engine.

    Another approach would be that you designate a folder in your Web host that is accessible via your site url. For example - https://yourdomain.com/files/9d6fdb34-f92f-4078-9ab3-e5ada4522f2e.pdf

    File (9d6fdb34-f92f-4078-9ab3-e5ada4522f2e.pdf) is physically stored in your cloud host local folder which can be any where like /var/files but your webserver points to this directory when user enter https://yourdomain.com/files/ and a reference to this file can be stored in a MySQL along with other metadata, example for your case -

    file_guid :  9d6fdb34-f92f-4078-9ab3-e5ada4522f2e
    file_name : BinarySearch
    file_extension: pdf
    user_id : 123 (if applicable)
    

    In the user interface, your users see (BinarySearch.pdf) text which link to https://yourdomain.com/files/9d6fdb34-f92f-4078-9ab3-e5ada4522f2e.pdf

    When you store the file to the /var/files/ if your users are uploading files, always rewrite the file name as a unique uuid but store the file name they uploaded in your database as I specified above.

    I hope this helps.

    Good luck.

    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗