开发者

Bad PHP script for downloading

I have a easy script:

  $url=file_get_contents($_GET['url']);
  $name=$_GET['name'];
  file_put_contents('temp.'.$_GET['ext'],$url);

It is scr开发者_如何学Cipt for downloading file by http. But file is so big (~10 Mb), and script stops with message:

"Fatal error: Maximum execution time of 30 seconds exceeded"

On my local server I can edit php.ini and set 300 seconds, but I can't do it on server on Internet. How can I do my task without editing php.ini? Give me example of code please, thank you.


Maybe you can download file in parts:

$parts = 4;
$file_size = 500000;
$size_per_part = $file_size/$parts;

for($i=1;$i<$parts;$i++){ 
$url{$i} = file_get_contents('file.text', NULL, NULL, 0, $size_per_part);
}


If you can't edit your php.ini file try following code to increase your;

// check if your web server accepts ini_set, before attempt to change anything
if(!ini_get('safe_mode')) {
    set_time_limit(240);
    ini_set('max_input_time', 300);
    ini_set("max_execution_time", "240");
}

More information about PHP ini_set.


There's no way to get around the script timeout. You'll need to start the download in the background and use another script to check on its progress.

exec( 'wget -b -O temp.' . $_GET['ext'] . ' "' . $_GET['url'] . '" > /dev/null &' );

Will execute wget (assuming your hosting service has wget) in the background and you can check the wget-log file that gets generated to see when it's done.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜