使用 PHP 下载图像时出现 500 内部服务器错误
即使我一次下载并插入到 DB 80 图像,我通过运行以下脚本收到 500 内部服务器错误:
<?php
function download_remote_file($file_url, $save_to)
{
$content = file_get_contents($file_url);
file_put_contents($save_to, $content);
}
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
require_once('sqlconnect.php');
$time_start = microtime_float();
$query="SELECT * FROM GAMES WHERE GAME_IMAGE LIKE '%cache%' LIMIT 0, 80";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
$game_img = $row['GAME_IMAGE'];
$gameDB_id = $row['GAME_DBID'];
chdir($_SERVER['DOCUMENT_ROOT'].'/img/game/');
if(!file_exists($gameDB_id))
{
mkdir($gameDB_id);
}
if(strlen(strstr($game_img,'boxart')) == 0)
{
$game_img = $game_img.'/boxart/original/front/'.$gameDB_id.'-1.jpg';
}
$game_img_path = $_SERVER['DOCUMENT_ROOT'].'/img/game/'.$gameDB_id;
chmod($game_img_path,0777);
$game_img_name = $gameDB_id.'.jpg';
$gamefullpath = 'http://www.someSite.nl/img/game/'.$gameDB_id.'/'.$game_img_name;
if(!file_exists($game_img_path.'/'.$game_img_name ))
{
download_remote_file($game_img, realpath($game_img_path) . '/'.$game_img_name);
$bytes = filesize($game_img_path.'/'.$game_img_name );
$KB = round($bytes / 1024, 2);
if($KB > 50)
{
echo "The ID number ".$gameDB_id. " has an invalid image URL, look it up on gamedb.net! <br />";
}
}
$query2 = "UPDATE GAMES SET GAME_IMAGE = '$gamefullpath' WHERE GAME_DBID = '$gameDB_id' AND GAME_DBID";
$result2 = mysql_query($query2) or die("error!");
//$nr_of_files = count($game_img_path);
//echo "Nr of images downloaded:".$nr_of_files;
}
mysql_close($con);
$time_end = microtime_float();
$time = $time_end - $time_start;
echo "Downloading and adding to the database took ".$time." seconds <br />";
$message = "The specified images have been downloaded!";
echo $message;
?>
我是否做错了什么,导致服务器停止?或者万一效率不高,我该怎么做才能做到这一点。或者问题可能出在某些服务器配置上。
服务器:
Apache/2.2.9 (Debian) DAV/2 SVN/1.5.1 PHP/5.2.6-1+lenny13 with Suhosin-Patch mod_ruby/1.2.6 Ruby/1.8.7(2008-08-11) mod_ssl/2.2.9 OpenSSL/0.9.8g Server
非常感谢您的帮助。
更新(日志):
[Wed Jan 11 08:58:39 2012] [notice] mod_fcgid: call /home/someSiteName/public_html/admin/download_game_img.php with wrapper /home/someSiteName/fcgi-bin/php5.fcgi
更新(时间):
大约100秒。
更新(骰子):
我在这行后面放了一个骰子,错误似乎在这里:
download_remote_file($game_img, realpath($game_img_path) . '/'.$game_img_name);
I'm getting the 500 Internal Server Error by running the following script, even though I'm downloading and inserting to the DB 80 images at a time:
<?php
function download_remote_file($file_url, $save_to)
{
$content = file_get_contents($file_url);
file_put_contents($save_to, $content);
}
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
require_once('sqlconnect.php');
$time_start = microtime_float();
$query="SELECT * FROM GAMES WHERE GAME_IMAGE LIKE '%cache%' LIMIT 0, 80";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
$game_img = $row['GAME_IMAGE'];
$gameDB_id = $row['GAME_DBID'];
chdir($_SERVER['DOCUMENT_ROOT'].'/img/game/');
if(!file_exists($gameDB_id))
{
mkdir($gameDB_id);
}
if(strlen(strstr($game_img,'boxart')) == 0)
{
$game_img = $game_img.'/boxart/original/front/'.$gameDB_id.'-1.jpg';
}
$game_img_path = $_SERVER['DOCUMENT_ROOT'].'/img/game/'.$gameDB_id;
chmod($game_img_path,0777);
$game_img_name = $gameDB_id.'.jpg';
$gamefullpath = 'http://www.someSite.nl/img/game/'.$gameDB_id.'/'.$game_img_name;
if(!file_exists($game_img_path.'/'.$game_img_name ))
{
download_remote_file($game_img, realpath($game_img_path) . '/'.$game_img_name);
$bytes = filesize($game_img_path.'/'.$game_img_name );
$KB = round($bytes / 1024, 2);
if($KB > 50)
{
echo "The ID number ".$gameDB_id. " has an invalid image URL, look it up on gamedb.net! <br />";
}
}
$query2 = "UPDATE GAMES SET GAME_IMAGE = '$gamefullpath' WHERE GAME_DBID = '$gameDB_id' AND GAME_DBID";
$result2 = mysql_query($query2) or die("error!");
//$nr_of_files = count($game_img_path);
//echo "Nr of images downloaded:".$nr_of_files;
}
mysql_close($con);
$time_end = microtime_float();
$time = $time_end - $time_start;
echo "Downloading and adding to the database took ".$time." seconds <br />";
$message = "The specified images have been downloaded!";
echo $message;
?>
Am I doing something wrong which forces the server to stop? Or in case it's not efficient what can I do to make it so. Or maybe the problem is some server configuration.
Server:
Apache/2.2.9 (Debian) DAV/2 SVN/1.5.1 PHP/5.2.6-1+lenny13 with Suhosin-Patch mod_ruby/1.2.6 Ruby/1.8.7(2008-08-11) mod_ssl/2.2.9 OpenSSL/0.9.8g Server
Your help is much appreciated.
UPDATE (log):
[Wed Jan 11 08:58:39 2012] [notice] mod_fcgid: call /home/someSiteName/public_html/admin/download_game_img.php with wrapper /home/someSiteName/fcgi-bin/php5.fcgi
UPDATE (time):
about 100 seconds.
UPDATE (die):
I put a die after this line and the error seems to be here:
download_remote_file($game_img, realpath($game_img_path) . '/'.$game_img_name);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为默认 php 脚本的最大执行时间为 30 秒,而您的页面花费的时间超过 100 秒。
您可以使用 .htaccess 文件增加 php 脚本的最大执行时间,
将以下行添加到您的 .htaccess 文件中,
您还可以在 php.ini 文件中设置限制
this is because the default php script's maximum execution time is 30 seconds and your page is taking more than 100 seconds.
you can increase the maximum execution time for your php scripts using .htaccess file
add the following lines to your .htaccess file
you can also set the limit in php.ini file