1

I have done a code to receive images from iphone to PHP Server and I need to resize these image and move to 4 folders.

Only then the json respose is giving to iphone. But it takes much time.

Requirement:

  1. i want to move a file to the folder "folder1" then want to give the json response.

the resizing process should do from this "folder1" after giving json response.

How to run this resizing process in background.

Here is my code:

http://pastebin.com/qAcT1yi9

merlin
  • 81
  • 1
  • 1
  • 6
  • It can't really fully be 'background' if you want to provide a feedback to the user. Your operations could be carried on by some Ajax code in your page so that it does not 'look' like the page is blocked while the operations are performed. Otherwise you'll need some ajax polling code that keeps asking how far the server has gone. – Stefano Apr 28 '11 at 12:21
  • When a json response is given to iphone . it is out of tht page. Then How can i execute resisizng? – merlin Apr 28 '11 at 12:27
  • the reply to your comments was too long so i wrote an answer, though it lacks real tech details – Stefano Apr 28 '11 at 12:48
  • here is the code. I am not an expert.The user has to wait till it completes all resizing process. I want send respose first and then resizing. pls help. http://pastebin.com/qAcT1yi9 – merlin Apr 28 '11 at 14:41
  • Oh no, your code `$event_code=$_POST['event_code']; $sql = " SELECT events_id,event_title,file,mobile_upload_status,mobile_photo_approval FROM tbl_events WHERE event_code='$event_code'";` contains a **huge** security hole! Always properly escape input or, better, use parametrized SQL queries. Now you're open for SQL injection. BTW, the correct MIME-type of JSON-encoded strings is `application/json`. – Marcel Korpel Apr 28 '11 at 15:31

6 Answers6

1

You could always send your php script to run in the background with a Linux command.

Example:

// using backticks to execute the Linux command but there are 
// other alternatives 
$cmd = `php runScriptInBackground.php &`;
echo $cmd;
Phill Pafford
  • 83,471
  • 91
  • 263
  • 383
  • And how do you call the script containing this code? BTW, if the PHP script returns immediately, you won't have a meaningful return value you can `echo`. – Marcel Korpel Apr 28 '11 at 13:46
  • from my understanding of the question he wanted to send a image from the iPhone to a PHP script. The PHP script would be a wrapper script to take the image and send it to the background. You could return a successful transfer for this part. Now the background script has control of the process and would need to push (if needed) a response (or something) back to the iPhone after the image had been resized or whatever – Phill Pafford Apr 28 '11 at 14:17
  • somebody pls respond. kindly check my code and tell me a good solution – merlin Apr 28 '11 at 15:14
  • And how do you post a response to a web browser from a stand-alonely running PHP script? AFAIK, if you `echo` something from that script, output will go to the (some) console. – Marcel Korpel Apr 28 '11 at 15:34
  • thanks.. the resizing process will work after sending Json response. – merlin Apr 29 '11 at 06:07
0

First send/upload the images and send a response back, without doing the resize operation.

Then, if the upload was successful, let the browser issue another request and do the resizing. When this succeeds, send the message ‘resizing successful’ back.

Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
0

A common solution to this problem is implementing a loading/processing message on hitting a specific event. Then - still being displayed - the action will continue to load on the background and the result page will finally be displayed when done.

Although the user must wait, I prefer this above display a result message when the actual result is not known. Unfortunately I'm not sure how this is done on iphone development.

Wesley van Opdorp
  • 14,888
  • 4
  • 41
  • 59
  • The code should be done in php not in iphone. The image received in PHP server. – merlin Apr 28 '11 at 12:20
  • The processing and the json response are still done on your PHP Server. You can not shorten the runtime of your resizing and still display an accurate message before it's done. This problem is flow-related more then handling the actual work. What I propose here is an more 'user-friendly' flow. An progress-bar for example. – Wesley van Opdorp Apr 28 '11 at 12:25
0

if your building in objective c then you may just resize make a copy and resize it there and send the resized image to your php you could then display a spinner and json result back to the user and also if the is an error the user will still have the resized image to try again with... Also another thought I had was was to use push notification. I don't know what that code would look like but it's something to consider

mcgrailm
  • 17,469
  • 22
  • 83
  • 129
0

you need some async javascript or an iframe in your page posting the image to your server and providing feedback to the user.

This means that the 'main' page would not change, but some visual information can be provided to the user.

You can display an animated gif loader or use JS setInterval to give the user the feeling that things are moving forward why waiting for the server to respond.

If the processing is split in more 1 parts, after each step the server could respond with an HTML page and a redirect: this would even work in an IFRAME without JS. Each 'page' would perform one more step. But if the user closes the browser before all is done you would end with an unfinished task.

A DB, real background processing, and client side JS polling are a more robust alternative.

A full answer would be quite long and require way more details on your settings (apache CGI PHP? or mod_php? are you using an MVC model or framework, or are you writing a page-oriented website?).

If i had to write a full answer I would forget PHP and use Python and celery http://celeryproject.org/ ;-)

PS.

I just found out that a few related questions already existed:

Community
  • 1
  • 1
Stefano
  • 18,083
  • 13
  • 64
  • 79
0

You can do it realy in two times, first send de files and save on first server, after when the user request that you generate the necesary parts.

You will pass the costs from the file sender to the first request from that.

Welington Veiga
  • 151
  • 1
  • 7