使用 Perl CGI 脚本上传文件位置后,如何将文件位置添加到数据库中?

发布于 2024-08-27 04:02:54 字数 344 浏览 4 评论 0原文

我有一个使用 Perl 编写的 CGI 程序。它的功能之一就是上传图片到服务器。

所有这些都运行良好,包括将各种信息添加到 MySQL 数据库。我的问题是:如何获取添加到数据库的上传图片文件位置和名称?

我宁愿这样做,而不是更改脚本来将图片实际上传到数据库。我听说过将二进制文件上传到数据库的恐怖故事。

由于我对这一切都是新手,我很茫然。已经尝试了三周的一些研究和网络搜索,但没有成功。任何建议或答案将不胜感激。我真的不想手动将所有位置/名称添加到数据库中。

我正在使用:Perl CGI 脚本、MySQL 数据库、Linux 服务器,并且文件正在上传到服务器。我不想将实际文件添加到数据库中。只是他们的位置。

I have a CGI program I have written using Perl. One of its functions is to upload pics to the server.

All of it is working well, including adding all kinds of info to a MySQL db. My question is: How can I get the uploaded pic files location and names added to the db?

I would rather that instead of changing the script to actually upload the pics to the db. I have heard horror stories of uploading binary files to databases.

Since I am new to all of this, I am at a loss. Have tried doing some research and web searches for 3 weeks now with no luck. Any suggestions or answers would be greatly appreciated. I would really hate to have to manually add all the locations/names to the db.

I am using: a Perl CGI script, MySQL db, Linux server and the files are being uploaded to the server. I AM NOT looking to add the actual files to the db. Just their location(s).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぺ禁宫浮华殁 2024-09-03 04:02:54

听起来您的方法已经完成,您可以上传,将其设为字符串并将其扔到 mysql 中,类似于将文件作为字符串读取。然而,由于您给出了文件句柄与 CGI 读取的文件名。您想知道该文件实际上在哪里。

如果您使用CGI.pm,则upload、uploadInfo、上传参数、上传私有文件将帮助您处理上传文件来源。远程客户端和 CGI​​ 完成后,它们存放的位置通常不是永久的,并且至少是不稳定的。

It sounds like you have your method complete where you take the upload, make it a string and toss it unto mysql similar to reading file in as a string. However since your given a filehandle versus a filename to read by CGI. You are wondering where that file actually is.

If your using CGI.pm, the upload, uploadInfo, the param for the upload, and upload private files will help you deal with the upload file sources. Where they are stashed after the remote client and the CGI are done isn't permanent usually and a minimum is volatile.

半透明的墙 2024-09-03 04:02:54

您有一堆上传的文件需要添加到数据库中吗?编写一个一次性脚本来循环所有文件并将详细信息插入数据库应该是微不足道的。如果它们都在一个位置,那么一个简单的 opendir()/readdir() 类型循环将捕获所有它们,否则您可以创建一个文件路径列表来循环并循环。

如果您正在谈论在服务器中记录新上传,那么它会是这样的:

  1. 用户将文件上传到服务器
  2. 脚本从文件中提取任何想要/需要的信息(名称、大小、mime 类型、校验和等)。 ..)
  3. 启动数据库事务
  4. 将文件信息插入数据库
  5. 检索新记录的 ID
  6. 使用 ID 作为文件名
  7. 将上传的文件移动到最终存放位置,如果一切正常,则

,提交事务使用 ID 作为文件名解决文件名冲突的担忧并且新上传的内容会覆盖之前的上传内容。如果您将上传的内容存储在网站根目录之外的某个位置,那么对文件的唯一访问将通过您的脚本进行,从而使您可以完全控制下载。

You've got a bunch of uploaded files that need to be added to the db? Should be trivial to dash off a one-off script to loop through all the files and insert the details into the DB. If they're all in one spot, then a simple opendir()/readdir() type loop would catch them all, otherwise you can make a list of file paths to loop over and loop over that.

If you've talking about recording new uploads in the server, then it would be something along these lines:

  1. user uploads file to server
  2. script extracts any wanted/needed info from the file (name, size, mime-type, checksums, etc...)
  3. start database transaction
  4. insert file info into database
  5. retrieve ID of new record
  6. move uploaded file to final resting place, using the ID as its filename
  7. if everything goes file, commit the transaction

Using the ID as the filename solves the worries of filename collisions and new uploads overwriting previous ones. And if you store the uploads somewhere outside of the site's webroot, then the only access to the files will be via your scripts, providing you with complete control over downloads.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文