如何创建(可配置)数量的临时下载链接

时间:2021-09-07 16:24:08

I am creating a download link that works only 10 times.So I got 2 options.

我正在创建一个只能工作10次的下载链接。所以我有2个选项。

  • Downloading a file and updating its count to database and delete file when count is 10.
  • 下载文件并将其计数更新为数据库并在count为10时删除文件。

  • Or downloading a file and renaming to filename-1 , filename-2.......filename-10 .. and finally delete it when it reaches filename-10.
  • 或者下载文件并重命名为filename-1,filename-2 ....... filename-10 ..最后在文件名为-10时将其删除。

I guess second option use less resource for not calling database.Now What is the easy way to find the file name.For example:

我猜第二个选项使用较少的资源来不调用database.Now什么是查找文件名的简单方法。例如:

All my filenames are unique like sadkjsah78876djhgsadj.jpg.So when i update count it will be like

我的所有文件名都是独一无二的,比如sadkjsah78876djhgsadj.jpg。所以当我更新计数时它会像

sadkjsah78876djhgsadj-1.jpg
(or)
sadkjsah78876djhgsadj-8.jpg

So how to find the file name based on sadkjsah78876djhgsadj in a directory.I am thinking of looping .but I dont think thats the best method when i set download limit to 100.

那么如何在一个目录中找到基于sadkjsah78876djhgsadj的文件名。我正在考虑循环。但是当我将下载限制设置为100时,我不认为这是最好的方法。

1 个解决方案

#1


1  

Best option is to use an .htaccess with mod_rewite to rewrite calls to a certain directory to a PHP file. e.g. any calls to /downloads/FILEHASH.jpg get rewritten as /download.php?file=FILEHASH

最好的选择是使用带有mod_rewite的.htaccess来将对某个目录的调用重写为PHP文件。例如任何对/downloads/FILEHASH.jpg的调用都会被重写为/download.php?file=FILEHASH

The download.php would then check if the FILEHASH matches a row in the DB and that the dl count was under x, if so, it would increment the dl count, set the response code as 200, content-type to image/jpeg and fire out the image data (either as stored in the filesystem or from a BLOB in the DB).

然后,download.php将检查FILEHASH是否匹配数据库中的行,并且dl计数是否在x下,如果是,则会增加dl计数,将响应代码设置为200,将content-type设置为image / jpeg和发出图像数据(存储在文件系统中或从DB中的BLOB中)。

If not found or if the the dl count was over x, it would set the response code as 404 (or something more appropriate if you prefer) and either return nothing or maybe a helpful message. Depending on volume of files, you should probably also delete the row from the DB and unlink the file from the filesystem at this point.

如果没有找到或者dl计数超过x,它会将响应代码设置为404(或者更合适的话,如果您愿意)并且不返回任何内容或者可能是有用的消息。根据文件的大小,您可能还应该从数据库中删除该行,并在此时取消文件系统中的文件链接。

If you use an MVC framework, this kind of thing is really straightforward.

如果你使用MVC框架,这种事情真的很简单。

If you're serving up more than just images, make sure you get the upload's mime type when it's being added, save that in the DB and serve that as the content-type when downloading.

如果您提供的不仅仅是图像,请确保在添加时获取上传的mime类型,将其保存在数据库中并在下载时将其作为内容类型提供。

#1


1  

Best option is to use an .htaccess with mod_rewite to rewrite calls to a certain directory to a PHP file. e.g. any calls to /downloads/FILEHASH.jpg get rewritten as /download.php?file=FILEHASH

最好的选择是使用带有mod_rewite的.htaccess来将对某个目录的调用重写为PHP文件。例如任何对/downloads/FILEHASH.jpg的调用都会被重写为/download.php?file=FILEHASH

The download.php would then check if the FILEHASH matches a row in the DB and that the dl count was under x, if so, it would increment the dl count, set the response code as 200, content-type to image/jpeg and fire out the image data (either as stored in the filesystem or from a BLOB in the DB).

然后,download.php将检查FILEHASH是否匹配数据库中的行,并且dl计数是否在x下,如果是,则会增加dl计数,将响应代码设置为200,将content-type设置为image / jpeg和发出图像数据(存储在文件系统中或从DB中的BLOB中)。

If not found or if the the dl count was over x, it would set the response code as 404 (or something more appropriate if you prefer) and either return nothing or maybe a helpful message. Depending on volume of files, you should probably also delete the row from the DB and unlink the file from the filesystem at this point.

如果没有找到或者dl计数超过x,它会将响应代码设置为404(或者更合适的话,如果您愿意)并且不返回任何内容或者可能是有用的消息。根据文件的大小,您可能还应该从数据库中删除该行,并在此时取消文件系统中的文件链接。

If you use an MVC framework, this kind of thing is really straightforward.

如果你使用MVC框架,这种事情真的很简单。

If you're serving up more than just images, make sure you get the upload's mime type when it's being added, save that in the DB and serve that as the content-type when downloading.

如果您提供的不仅仅是图像,请确保在添加时获取上传的mime类型,将其保存在数据库中并在下载时将其作为内容类型提供。