How to check if a file exists on an internet server before attempting to download it?

Hi, 

I have a simple quest. To check if a file exists before trying to download it.

I know how to setup for a download and if the file I’m interested does not exist yet I know how to trigger a php script to create the file I need. I just need to figure out how to check if the file exists on the server first. I am guessing I can do this using the error codes returned by network.download but not sure. Any thoughts?

Your help is most appreciated. Thank you very much. 

Regards,

Kerem

You should be able to use network.request() using the “HEAD” method (basically a GET that doesn’t return anything).  It will return you the headers and you can see if the event.status is a 200 or not.

Hi Rob,

Thanks much for responding to our queries even during the long holiday weekend. Most appreciated.

Is there a code sample you can point me towards for what you suggest above? If not that’s ok. I’ll try to figure it out later this afternoon.

In case you wonder… I’m trying to get my mask files created on the web server to go around the display.save issue we were talking about In another thread. I’ll post the solution here and in code exchange.

Happy 4th of July weekend. Enjoy the break.

Regards,
Kerem

Look at the network.request() documentation.  Use “HEAD” instead of “GET” in the example.  Dump the contents of the event table in your network listener to see what information is there.  I use the “print_r” function from community code to dump tables since I"m an old PHP guy, it’s comfortable to me.

Super. This should get me going. Thank you very much. Take care.

Ok. All good. Seems like the following is all that is needed. Thanks much for your help.

local function networkListener( event ) if ( event.isError ) then print( "Network error!") elseif ( event.phase == "ended" ) then print(event.status) if event.status == 200 then -- file exists. call the function to download it. else -- file does not exist. call the function to get it created and then download it. end end end network.request( "http://myserver.com/mask-320x240.png", "HEAD", networkListener )

Keep in mind that any 2XX return value is a success. 3XX is some form of re-direction, but you should end up with a 2XX in that case.  4XX and 5XX are problems.  1XX only happen on put options, so you shouldn’t encounter those

Got it. Then I need to modify the listener to pass any 2xx. Easy to do.

I just posted on the code exchange what this was all about. With your php background and Corona wits if you could take a look and let me know if I missed any gotchas I would appreciate it. It seems to work but can be improved I’m sure. 

http://developer.coronalabs.com/code/runtime-bitmap-maskfile-creation

Thanks much for all your help.

Just a couple of thoughts.  First, I would use GET instead of POST, for a couple of reasons.  First semantics:  You are "GET"ting something more than you’re sending something.  Secondly, there are a bunch of PHP servers that won’t load up $_POST unless the right headers are set.  GET doesn’t have that problem.

Next I would maybe list the server requirements.  I’m assuming you’re using ImageMagik but I’m not sure.  There may be a better way using the GD library, but at a minimum you should let the users know what all they need to do on the server side.

Hi Rob, 

Thanks much for the feedback. I am a total php noob so I was simply stitching things I picked from here & there. GET works for me when I type the parameters on the browser URL box and hit enter. POST seems to work when I send the parameters from Corona SDK app. I will try to make it work with GET and update the post. 

Good idea to update the exchange with server requirements. Will do. You are right. This is using ImageMagick. 

Regards,

Kerem

You should be able to use network.request() using the “HEAD” method (basically a GET that doesn’t return anything).  It will return you the headers and you can see if the event.status is a 200 or not.

Hi Rob,

Thanks much for responding to our queries even during the long holiday weekend. Most appreciated.

Is there a code sample you can point me towards for what you suggest above? If not that’s ok. I’ll try to figure it out later this afternoon.

In case you wonder… I’m trying to get my mask files created on the web server to go around the display.save issue we were talking about In another thread. I’ll post the solution here and in code exchange.

Happy 4th of July weekend. Enjoy the break.

Regards,
Kerem

Look at the network.request() documentation.  Use “HEAD” instead of “GET” in the example.  Dump the contents of the event table in your network listener to see what information is there.  I use the “print_r” function from community code to dump tables since I"m an old PHP guy, it’s comfortable to me.

Super. This should get me going. Thank you very much. Take care.

Ok. All good. Seems like the following is all that is needed. Thanks much for your help.

local function networkListener( event ) if ( event.isError ) then print( "Network error!") elseif ( event.phase == "ended" ) then print(event.status) if event.status == 200 then -- file exists. call the function to download it. else -- file does not exist. call the function to get it created and then download it. end end end network.request( "http://myserver.com/mask-320x240.png", "HEAD", networkListener )

Keep in mind that any 2XX return value is a success. 3XX is some form of re-direction, but you should end up with a 2XX in that case.  4XX and 5XX are problems.  1XX only happen on put options, so you shouldn’t encounter those

Got it. Then I need to modify the listener to pass any 2xx. Easy to do.

I just posted on the code exchange what this was all about. With your php background and Corona wits if you could take a look and let me know if I missed any gotchas I would appreciate it. It seems to work but can be improved I’m sure. 

http://developer.coronalabs.com/code/runtime-bitmap-maskfile-creation

Thanks much for all your help.

Just a couple of thoughts.  First, I would use GET instead of POST, for a couple of reasons.  First semantics:  You are "GET"ting something more than you’re sending something.  Secondly, there are a bunch of PHP servers that won’t load up $_POST unless the right headers are set.  GET doesn’t have that problem.

Next I would maybe list the server requirements.  I’m assuming you’re using ImageMagik but I’m not sure.  There may be a better way using the GD library, but at a minimum you should let the users know what all they need to do on the server side.

Hi Rob, 

Thanks much for the feedback. I am a total php noob so I was simply stitching things I picked from here & there. GET works for me when I type the parameters on the browser URL box and hit enter. POST seems to work when I send the parameters from Corona SDK app. I will try to make it work with GET and update the post. 

Good idea to update the exchange with server requirements. Will do. You are right. This is using ImageMagick. 

Regards,

Kerem