【问题标题】:500 internal server error while downloading images using PHP使用 PHP 下载图像时出现 500 内部服务器错误
【发布时间】:2015-11-18 22:55:00
【问题描述】:

我通过运行以下脚本得到 500 内部服务器错误,即使我一次下载并插入 DB 80 图像:

<?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);

【问题讨论】:

  • 您的 PHP 错误日志中有一条消息?这个手术需要多长时间?标准的 PHP.ini 配置将允许 30 秒的挂墙时间——你当然可以改变它,但你需要知道发生了什么
  • Yuri 和@Murray McDonald,我已经更新了我的问题 :)
  • 可能是一些配置。超时什么的。阅读手册以获取正确的指令值:httpd.apache.org/mod_fcgid/mod/mod_fcgid.html#fcgidiotimeout 此外,您应该真正尝试通过单独运行每个函数来逐步调试它,至少将die 逐行放置。
  • 这是您的网络服务器的活动日志——也可以有一个“PHP 错误日志”(在生产服务器上总是一个好主意)——它可以通过 php.ini 文件进行配置——我会推荐以下设置: error_reporting = E_ALL display_errors = Off display_startup_errors = On error_log = /path/to/dir/with/proper_permissions/php_errors.log 基本上,http 500 只是意味着服务器“出错”——你真的需要确切地知道什么——我猜你已经超出了一些可配置的限制(在 PHP 设置或网络服务器设置中)——你使用什么网络服务器?
  • 注释中不允许有“换行符”——上面的 php.ini 配置指令在文件中的每一个单独的行上——找到现有条目并更改它们——或注释它们出来并添加新的——确保搜索文件以确保指令不会出现多次——在重复指令的情况下,“最后一个”总是获胜

标签: php mysql internal-server-error


【解决方案1】:

这是因为默认 php 脚本的最长执行时间为 30 秒,而您的页面耗时超过 100 秒。

您可以使用 .htaccess 文件增加 php 脚本的最大执行时间

将以下行添加到您的 .htaccess 文件中

php_value max_execution_time 8000
php_value max_input_time 4000

你也可以在php.ini文件中设置限制

max_execution_time 8000
max_input_time 4000

【讨论】:

  • 我会试试这个,如果它有效,我会接受你的回答。非常感谢。
  • 好吧,htacess 方法只有在服务器配置中启用“AllowOverride”时才有效——最好的办法是让 PHP 错误日志正常工作——然后错误应该显示在该日志中或 Apache 错误日志
  • @Nima -- 对不起,我没有“得到”你所指的任何东西
  • 好的——我现在明白了——所以你已经将它跟踪到“download_remote_file”——当然你无法控制从远程检索文件需要多长时间网站——但请帮自己一个忙,确保你打开了错误日志——现在有 PHP 最大执行时间设置为 chnage,但我记得 Apache http.conf 中可能也有一些东西——它已经自从我做了活跃的 Apache 管理员 4 年了
  • 顺便说一句,我不再记得当您更改 PHP.ini 设置时是否需要停止/重新启动 Apache —— 我认为如果它作为一个模块运行,而如果它正在运行 CGI,那么您不需要t -- 另一种选择是在脚本顶部使用“ini_set” -- 这样您可以更改脚本的设置,但其他脚本的 ini 文件设置保持不变
猜你喜欢
  • 2015-07-05
  • 2016-03-26
  • 2023-03-09
  • 2019-03-21
  • 1970-01-01
  • 2016-06-23
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多