简体   繁体   English

在PHP中从wget获取文件名

[英]Get filename from wget in php

I'm setting up a script so that I can input a URL to a web page and the script will wget the file. 我设置了一个脚本,这样我可以输入一个URL的网页,脚本将wget文件。 Most of the files, however, will be in the *.rar format. 但是,大多数文件将采用*.rar格式。 Is there anyway I can pass the filename to the unrar command to unarchive the files downloaded via wget ? 无论如何,我可以将文件名传递给unrar命令来取消存档通过wget下载的文件吗?

Many, many thanks in advance! 非常感谢!

EDIT I thought about using PHP's explode() function to break up the URL by the slashes ( / ) but that seems a bit hack-y. 编辑我曾考虑过使用PHP的explode()函数以斜杠( / )分隔URL,但这似乎有点hack-y。

Rather than forking out to external programs to download and extract the file, you should consider using PHP's own cURL and RAR extensions. 您应该考虑使用PHP自己的cURLRAR扩展,而不是使用外部程序来下载和提取文件。 You can use the tmpfile() function to create a temporary file, use it as the value of the CURLOPT_FILE option to make cURL save the downloaded file there, and then open that file with the RAR functions to extract the contents. 您可以使用tmpfile()函数创建一个临时文件,将其用作CURLOPT_FILE选项的值,以使cURL将下载的文件保存在那里,然后使用RAR函数打开该文件以提取内容。

使用basename()获取文件名。

@Wyzard gives the best answer. @Wyzard给出了最佳答案。 If there's a library that solves your problem, use it instead of forking an external process. 如果有一个库可以解决您的问题,请使用它来代替外部过程。 It's safer and it's the clean solution. 更安全,这是干净的解决方案。 PHP's cURL and RAR are good, use them. PHP的cURLRAR很好,请使用它们。

However, if you must use wget and unrar , then @rik gives a good answer. 但是,如果必须使用wgetunrar ,则@rik提供了一个很好的答案。 wget 's -O filename option saves the file as filename , so you don't have to work it out. wget-O filename选项将文件另存为filename ,因此您不必进行处理。 I would rather pipe wget 's output directly to unrar though, using wget -q -O - http://www.example.com | unrar 我宁愿使用wget -q -O - http://www.example.com | unrarwget的输出直接通过管道传递到unrar wget -q -O - http://www.example.com | unrar wget -q -O - http://www.example.com | unrar . wget -q -O - http://www.example.com | unrar

@Byron's answer is helpful, but you really should not need to use it here. @Byron的答案很有帮助,但是您实际上不需要在这里使用它。 It is, however, better than using explode() as your edit mentions. 但是,这比使用您的编辑提到的explode()更好。

wget -O filename URL && unrar filename

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM