tags:

views:

428

answers:

3

Hello,

I'm running wget through php's shell_exec() How could I understand that wget got 404 error getting file?

Thank you.

+1  A: 

According to this, when faced with a simple "download file XYZ" operation, wget will return a return code of 1 when the download fails for some reason. It won't work for complex operations like mirroring a site, but in your case, it should be fine.

You can get hold of the return code using exec()`s "return_var" variable.

Wget should return 1 (or some other status code that is not 0) if it is presented with a proper 404 page. Try it out.

To actually distinguish the exact error status code that wget receives, I think you'll have to start parsing its output (or use a PHP-based web client class.)

Pekka
+1  A: 

You can use a command line like the following with exec:

exec("wget http://www.google.com/not_present.html 2> some_temp_file ",&output,&retvalue);

Check the retvalue to see if an error has occurred. A non-zero value means error occurred. Since we've redirected the standard error to some_temp_file, you can grep this file for the pattern 404 Not Found. If present it indicates that a 404 has occurred.

codaddict
+1  A: 

I think you are solving the wrong problem. Please try CURL before using anything that can't fail portably with a meaningful, parse-able status as David first suggested in comments.

Tim Post
@Downvoters: Care to explain your vote? Is it my fault that the OP added more info and I wasn't clicking refresh once a second so I could see it and adjust my answer? Can programmers not read timestamps?
Tim Post