ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> php-Dev
php-Dev
#26060 [Fbk->Opn]: Not closing download handles
by datacompboy at mail dot ru other posts by this author
Nov 17 2003 1:45PM messages near this date
#26060 [Opn->Fbk]: Not closing download handles | #26060 [Fbk]: Not closing download handles
ID:               26060
 User updated by:  datacompboy at mail dot ru
 Reported By:      datacompboy at mail dot ru
-Status:           Feedback
+Status:           Open
 Bug Type:         cURL related
 Operating System: Win2003 adv server
 PHP Version:      4.3.3
 New Comment:

Sorry for delay with report - was a bit busy (I have made work-around
via moveing all download/transform features to spawned program)

Removeing ob_* does not matter, only I can't send headers if spawned
process shows info - the Header function set after echo `filter`...

In end of this comment I added script, that simplified (HTML parser
removed)

If you run an lot of requests at one time (I have tested in local by
pressing F5 key for a while) - then in directory of scrpit will created
several *.TMP files, and not removed.

if comment out line
  echo `echo  trans.exe $lang $infile $outfile`;
then no this TMP files left.

As you see, I have changed external filter to just echo, so this not
filter problem. This looks like problem of external app spawning...

<?
//  dl("php_curl.dll");
  error_reporting (E_ALL ^ E_NOTICE);
  set_time_limit( 60 );
  ignore_user_abort ( 1 );
  ob_start();

  $transself = "http://";
  if (isset($_SERVER["HTTP_HOST"])) { $transself .=
$_SERVER["HTTP_HOST"]; }
                               else { $transself .=
$_SERVER["SERVER_NAME"]; }

  if (isset($_SERVER["URL"])) { $transself .= $_SERVER["URL"]; }
                         else { $transself .= $_SERVER["SCRIPT_NAME"];
}

  if ($_SERVER["REQUEST_METHOD"]=="GET")
  {
    if (empty($_GET["_translator_lang"]))
    {
      $lang  = $_GET['lang'];
      $url   = $_GET['url'];
      $ref   = '';
      $post  = 0;
    } else
    {
      $lang  = $_GET['_translator_lang'];
      $ref   = $_GET['_translator_referrer'];
      $url   = $_GET['_translator_form'];
      $qs = ''; $req=array();
      foreach ($_GET as $na =>  $va)

      {
        if (strcmp($na,'_translator_lang')&&
            strcmp($na,'_translator_referrer')&&
            strcmp($na,'_translator_form'))
        {
          $req[]="$na=".urlencode($va);
        }
      }
      $qs = join("&", $req);
      $url  .= "?".$qs;
      $post  = 0;
    }
  } else
  if ($_SERVER["REQUEST_METHOD"]=="POST")
  {
      $lang  = $_POST['_translator_lang'];
      $ref   = $_POST['_translator_referrer'];
      $url   = $_POST['_translator_form'];
      $post  = 1;
      $qs = ''; $req=array();
      foreach ($_POST as $na =>  $va)

      {
        if (strcmp($na,'_translator_lang')&&
            strcmp($na,'_translator_referrer')&&
            strcmp($na,'_translator_form'))
        {
          $req[]="$na=".urlencode($va);
        }
      }
      $qs = join("&", $req);
  } else
  die("Unsupported method");

  if (!$lang ) { die("No language"); }
  if (!preg_match('#^http(?:s?)://#', $url)) { die("Incorrect URL"); }
  if (preg_match('#^http(?:s?):[/]+[^/]*$#', $url)) { $url.='/'; }

  $oinfile = tempnam ("./", "URLIN");
  $infile = preg_replace("#.*[/\\\\](.*?)#","\$1",$oinfile);
  $ooutfile = tempnam ("./", "URLOU");
  $outfile = preg_replace("#.*[/\\\\](.*?)#","\$1",$ooutfile);
  $inf = fopen($infile, "w");

  fwrite($inf, "<meta http-equiv=\"Content-Type\"
content=\"text/html\"> \n");


  $ch = curl_init();
  curl_setopt ($ch, CURLOPT_URL, $url);
  curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt ($ch, CURLOPT_HEADER, 0);
  curl_setopt ($ch, CURLOPT_FILE, $inf);
  if (strlen($ref)> 0)

  {
    curl_setopt($ch, CURLOPT_REFERER, $ref);
  }
  if ($post)
  {
//    if (preg_match("#^multipart",$_SERVER['CONTENT_TYPE']))
//    { // post as multipart/formdata
//    } else
    { // post as application/x-www-form-urlencoded
      curl_setopt ($ch, CURLOPT_POST, 1);
      curl_setopt ($ch, CURLOPT_POSTFIELDS, $qs);
      curl_setopt ($ch, CURLOPT_POSTFIELDSIZE, strlen($qs));
    }
  }
  if (curl_exec ($ch))
  {
    fclose($inf);
    echo `echo  trans.exe $lang $infile $outfile`;

    // Show output to browser with transform
    $inf = @fopen($outfile, "r");
    if (!is_resource($inf)) $inf = @fopen($infile, "r");
    if (is_resource($inf))
    {
      $codepage=chop(fgets($inf)); // get codepage of translated
document
      preg_match("#text/html[^;]*;[ ]*CHARSET=([^'\"]*)#i", $codepage,
$m);
      header("Content-type: text/html; charset=".$m[1]);

      fpassthru($inf);
    }
  }
  @fclose($inf);
  curl_close ($ch);
  @unlink ($infile);
  @unlink ($outfile);
  @unlink ($oinfile);
  @unlink ($ooutfile);
  ob_end_flush();
?> 


Previous Comments:
------------------------------------------------------------------------

[2003-11-06 15:29:41] sniper@[...].net

And provide an actually working script..(one that COULD actually work)


------------------------------------------------------------------------

[2003-11-06 15:28:03] sniper@[...].net

Simplify your script..drop the ob_* stuff totally and 
don't use any external apps on the files.

Does it work then? (add the new script here too)



------------------------------------------------------------------------

[2003-10-31 12:42:18] datacompboy at mail dot ru

Description:
------------
I tried to download file with curl, parse it with external app, show
output and kill temp file. But:
1. if php as isapi module - when several requests at one time some
inputs are locked! Filemon says that it frees by some cmd.exe after all
runned sub-processes already finished. Very strange!
2. if php as fastcgi - it often crashes with error in php_curl (all
latest dlls in system32 upgraded; php4ts only in c:\php, php4isapi also
in c:\php and no in c:\windows\system)
3. if php as cgi - all OK, except of when user cancelled download the
script terminated, but shotdown_procedure not called, so temp files
left.

Reproduce code:
---------------
<?ob_start(); $url="http://some.site.com";
  $oinfile = tempnam ("./", "URLIN");
  $infile = preg_replace("#.*[/\\\\](.*?)#","\$1",$oinfile);
  $ooutfile = tempnam ("./", "URLOU");
  $outfile = preg_replace("#.*[/\\\\](.*?)#","\$1",$ooutfile);
  register_shutdown_function(create_function('',
    "@unlink(\"$infile\"); @unlink(\"$outfile\");
@unlink(\"$oinfile\"); @unlink(\"$ooutfile\"); return 1;"));
  $inf = fopen($infile, "w");
  fwrite($inf, "<meta http-equiv=\"Content-Type\"
content=\"text/html\"> \n");

  $ch = curl_init();
  curl_setopt ($ch, CURLOPT_URL, $url);
  curl_setopt ($ch, CURLOPT_FILE, $inf);
  $st=curl_exec ($ch);
  curl_close ($ch);
  fclose($inf);
  echo `filter.exe $lang $infile $outfile 2> &1`;

  ob_end_flush();
?> 

Expected result:
----------------
No .tmp files not in windows\temp\ folder, not in script folder.

Actual result:
--------------
Some .tmp files left and in windows\temp, and in script folder.


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=26060&edit=1
Thread:
datacompboy at mail dot ru


datacompboy at mail dot ru



Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved