开发者

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. Most of the files, however, will be in the *.rar format. Is there anyway I can pass the filename to the unrar command to unarchive the files downloaded via wget?

开发者_JAVA技巧

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.


Rather than forking out to external programs to download and extract the file, you should consider using PHP's own cURL and RAR extensions. 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.


Use basename()to get the filename.


@Wyzard gives the best answer. 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.

However, if you must use wget and unrar, then @rik gives a good answer. wget's -O filename option saves the file as filename, so you don't have to work it out. I would rather pipe wget's output directly to unrar though, using wget -q -O - http://www.example.com | unrar.

@Byron's answer is helpful, but you really should not need to use it here. It is, however, better than using explode() as your edit mentions.


wget -O filename URL && unrar filename

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜