Question

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?

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.

Was it helpful?

Solution

@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.

OTHER TIPS

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.

wget -O filename URL && unrar filename

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top