Call all Files on my Server in Folder and count them

Hello everybody.

I’m trying to load images and check if they really exists to get a amount of all the images in folder.

The clue is, that I would be able to show the “newest” logical the file with the highest upload_(number).jpg at first.

But it always loads the first image and then it only prints “noo” :D:D

However here is my code:

local image\_status = true local image\_amount = 0 image\_setup() function image\_setup() image = display.loadRemoteImage( "http://m7-studios.de/labor/upload/upload\_"..( image\_amount + 1 )..".jpg", "GET", image\_control, "upload\_"..( image\_amount + 1 )..".jpg", system.TemporaryDirectory, gX / 2, gY / 2 ) if ( image ) then image\_amount = image\_amount + 1 print( image\_amount ) end end function image\_control( event ) if ( image ) then image:removeSelf() else print( "noo" ) end end

display.loadRemoteImage() does not return a handle, hence why the “image” variable in your code is nil.

Use network.dowload instead with display.newImageRect()

Oh forgot:
Is there a way to count the images on the Webserver?

One idea would be to keep a database of the images filenames on the server, that way you could retrieve the count.

Another way would be to have a php script that does the counting and returns the count to your app.

Plenty of options, up to you to choose one that suits you best :wink:

You would need a server side script, such as a PHP script to do this.  Corona SDK can just make HTTP requests to a web server and read the results from it.  Any thing happening on the actual server would require some script to handle.

Rob

What Rob suggests is probably the “best” solution … then you make 1 network.request to the php script and get all the info you need.

2 other possible options:

* If the server allows directory-level requests, then you could make 1 network.request to the directory and get an HTML-formatted listing of the files; you’d need to unpack that listing to get the available files (when I did a quick test, I also noticed that long file names get truncated so keep that in mind); while many webhosts allow this by default, it is possible that directory listing is disabled; and if there is an index.html/php/etc. file in the directory, you’ll get that instead

* If neither of those are possible for some reason, you might want to look at using the HTTP “HEAD” method on the files/urls … this may require that something be enabled on your webserver, but it should return a 200/Ok message (possibly with other useful info) or a 404/Not-Found error; keep in mind that this will still consume a lot of network bandwidth (and hence battery life)

display.loadRemoteImage() does not return a handle, hence why the “image” variable in your code is nil.

Use network.dowload instead with display.newImageRect()

Oh forgot:
Is there a way to count the images on the Webserver?

One idea would be to keep a database of the images filenames on the server, that way you could retrieve the count.

Another way would be to have a php script that does the counting and returns the count to your app.

Plenty of options, up to you to choose one that suits you best :wink:

You would need a server side script, such as a PHP script to do this.  Corona SDK can just make HTTP requests to a web server and read the results from it.  Any thing happening on the actual server would require some script to handle.

Rob

What Rob suggests is probably the “best” solution … then you make 1 network.request to the php script and get all the info you need.

2 other possible options:

* If the server allows directory-level requests, then you could make 1 network.request to the directory and get an HTML-formatted listing of the files; you’d need to unpack that listing to get the available files (when I did a quick test, I also noticed that long file names get truncated so keep that in mind); while many webhosts allow this by default, it is possible that directory listing is disabled; and if there is an index.html/php/etc. file in the directory, you’ll get that instead

* If neither of those are possible for some reason, you might want to look at using the HTTP “HEAD” method on the files/urls … this may require that something be enabled on your webserver, but it should return a 200/Ok message (possibly with other useful info) or a 404/Not-Found error; keep in mind that this will still consume a lot of network bandwidth (and hence battery life)