使用 Perl CGI 脚本上传文件位置后,如何将文件位置添加到数据库中?
我有一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您的方法已经完成,您可以上传,将其设为字符串并将其扔到 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.
您有一堆上传的文件需要添加到数据库中吗?编写一个一次性脚本来循环所有文件并将详细信息插入数据库应该是微不足道的。如果它们都在一个位置,那么一个简单的 opendir()/readdir() 类型循环将捕获所有它们,否则您可以创建一个文件路径列表来循环并循环。
如果您正在谈论在服务器中记录新上传,那么它会是这样的:
,提交事务使用 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:
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.