-1

I've got some function that takes very long time to execute (downloading some external images in my case) and I want to avoid execution time exceeded error.

Is there any way to avoid this (for example by dividing downloading of single images into single php 'threads' or something like that) ?

I cannot change execution time limit or any of ini settings.

I'm not able to use cron works as it'd be used in WordPress theme and I can't control platform of end user.

Adam Pietrasiak
  • 12,773
  • 9
  • 78
  • 91
  • Can you make cronjobs? –  Feb 07 '15 at 14:19
  • Nope, my range of actions is quite limited as it is commercial WordPress theme and I cannot control who and how will use it. – Adam Pietrasiak Feb 07 '15 at 14:20
  • Sorry for misspelling, I cannot change ANY ini setting. – Adam Pietrasiak Feb 07 '15 at 14:21
  • Update your question and provide all the information in there. We don't like guessing what you can / can't do and why. – PeeHaa Feb 07 '15 at 14:21
  • @AdamPietrasiak WordPress has a WordPress cron, you could use that, no? –  Feb 07 '15 at 14:21
  • I'm not sure if cron is good solution as I need to show user progress of uploading and finish process as quick as possible after he clicks some synchronize button. – Adam Pietrasiak Feb 07 '15 at 14:22
  • Wow even more hidden requirements. Update your question man and provide *all* needed information. – PeeHaa Feb 07 '15 at 14:23
  • @PeeHaa well if there'd be any kind of solution I'd adopt it to my needs and it could still be useful for some people who will read this question in the future. I'm quite sure I've listed all vital requirements. – Adam Pietrasiak Feb 07 '15 at 14:26

2 Answers2

1

One of the possibilities is to make a PHP script that downloads one external image, and let that script be called using Ajax. Then you can build a user interface with JavaScript which calls this PHP script for each image, one by one. It could show some progress bar depending on how many images have been downloaded already.

  • Well if no way to do it in php I guess sending small request with js would be the only solution. – Adam Pietrasiak Feb 07 '15 at 14:31
  • @AdamPietrasiak no, I'm not aware of any other solution that would just work anywhere. You're quite limited because many wordpress users are on shared hosting. If I remember correctly, there are some plugins that use this idea. –  Feb 07 '15 at 14:33
0

Yes you can. But you will have to host or proxy the images you want to download by chunk if the remote server does not understand partial transfer downloads.

Then you will have to make your PHP script request the image by chunks to the server

Request

GET /proxy/?url=http://example2.com/myimage.jpg HTTP/1.1
Host: www.example.com
Range: bytes=200-1000

Answer

HTTP/1.1 206 Partial Content
Date: Tue, 17 Feb 2015 10:50:59 GMT
Accept-ranges: bytes
Content-range: bytes 200-1000/6401
Content-type: image/jpeg
Content-length: 800

You will have many choice to call your php script enough times to get all the chunks : automatically refresh the page, ajax request, ...

Adam
  • 17,838
  • 32
  • 54