The online racing simulator
Nooby file access problem in PHP
(14 posts, started )
Nooby file access problem in PHP
Hi, I have a slight problem with opening a (dedi config) file for read access. The code does work, but when OpenFile () fails it takes aaaaages (60 sec execution timeout almost) for the script to react to the header () redirection. Also, in task manager it shows PHP taking 99% of CPU cycles. Any idea why this, and why OpenFile () fails with a existing URL type path (allow_url_fopen is On)?

Main code:

<?php 
$file 
OpenFile ($_POST['path']);
if (!
$fileheader ("location: $self?errcode=10");
// parse file
?>


<?php 
function OpenFile ($path) {
 if (
file_exists ($path)) {
  if (
$file fopen ($path"rb")) return ($file);
  return 
FALSE;
 }
 return 
FALSE;
}
?>

PHP 5.1.4 running as CGI (php-cgi.exe) under Apache 2.0.59 on WinXP Pro w/SP2
Quote from NotAnIllusion :Hi, I have a slight problem with opening a (dedi config) file for read access. The code does work, but when OpenFile () fails it takes aaaaages (60 sec execution timeout almost) for the script to react to the header () redirection. Also, in task manager it shows PHP taking 99% of CPU cycles. Any idea why this, and why OpenFile () fails with a existing URL type path (allow_url_fopen is On)?

Main code:
$file = OpenFile ($_POST['path']);
if (!$file) header ("location: $self?errcode=10");
// parse file


function OpenFile ($path) {
if (file_exists ($path)) {
if ($file = fopen ($path, "rb")) return ($file);
return FALSE;
}
return FALSE;
}

PHP 5.1.4 running as CGI (php-cgi.exe) under Apache 2.0.59 on WinXP Pro w/SP2

I would like to help you, but Im noob my self and having really hard to read your code.
I always use else statement to keep code readable.
I think your code is not returning the FALSE as it should and that could do that the main code continues without the header.
Try to write it with else commands and but in some debug to see what the code is doing.

/mike
Yeah I think that's the problem, but had the same thing happen when I rewrote the funct with the else blocks. Also, it doesn't really explain why it fails to open perfectly working URLs (e.g. http://localhost/dedi/setup.cfg, which opens when I put it in the browser's address bar)

Thanks for the contribution
have you tried. not guaranteed to work, just what i would try next.

<?php 
function OpenFile ($path) {
 if (
file_exists ($path)) {
  if (
$file fopen ($path"rb")) return ($file);
  return 
FALSE;
 } else {
    exit (
'file not found');
 return 
FALSE;
}
?>

I tried this earlier (I don't have net where I code so I can't try that now but I'll give it a go when I get back):

<?php 
function OpenFile ($path) {
  if (
file_exists ($path)) {
    
$file fopen ($path"rb");
    if (
$file) {
      return (
$file);
    } else {
      return 
FALSE;
    }
  } else {
    return 
FALSE;
  }
}
?>

How do I get that PHP code area, btw? :S

Thanks glyphon
|
\/
[php^] [/php^] (without the ^ obviously)
#7 - Krane

<?php 
$file 
OpenFile ($_POST['path']);
?>

NEVER EVER DO THAT!

Always check incoming data that it is what it is supposed to be and that it doesn't cointain any known dangerous characters and that the character encoding is set.


But if that is not going to be in the internets, only on your localhost that isn't accessible from the internets, then it doesn't matter of course...
#8 - filur
It's definitely not the code missing some else's, the fact that PHP gets stuck suggests some kind of lengthy timeout for fopen, maybe related to using an url to open the file, but it still shouldn't consume any noticeable amount of cpu.

Why do you want to open the file through an url? Why do you want to manually read an ascii text file as binary? Why are you not using the apache php module?


<?php 
file 
file_get_contents(path/to/file);
?>

I'm not a programmer, and have never been taught any coding as such (apart from one uni module in PHP).

I don't rly know how else to read the file as easily (see code below). All I really want is to be able to loop thru a server config file, store each key=value pair in an array (or object), and put them in a nice form so that I can easily modify the settings and save it on the system that's running the script.

Why is get_file_contents () better than using it as a resource? Also, would it be better then to write the entire config from a string to a file with file_put_contents ()? Currently I open (or create) the file for write access as a resource and fwrite each /key=value pair into it and close it.

This class is the config template which is used hold the settings when reading from file, URL or form (when saving to file). I don't have the actual code in front of me, so it's off my head.

<?php 
class LFSConfig extends DCFramework {
 var 
$host "Host Name";
 
// ... all server options

 
function Parse ($file// $file = fopen ("http://localhost/foo/bar.cfg", "rb"); from OpenFile()

  
foreach ($line as $key => $value) {
   
$line fgets ($file);
   if (
$key != "") { // iggy blank lines had some probs with if (!$key) {
    
if (!preg_match ('/^\/\//'$line) && preg_match ('^/\//'$line)) { // only select lines starting with '/')
     
$this->$key $value;
    }
   }
  }
 } 
// end function
}
?>

I want to open it as binary because of newline issues. I thought there might be problems of compatibility between filesystems that use different newlines (\n, \r\n) and reading in text mode. Am I wrong?

I am not using the apache php module because I cannot get the PHP module to talk to MySQL. I'd prefer to use the module but everything I've tried has failed in terms of getting PHP to load the MySQL extension. PHP does work as a module, viewing phpinfo () output the extension dir and php.ini path are correct, but uncommenting the appropriate line (which works in CGI) does not work. Any ideas?

Quote from Krane :NEVER EVER DO THAT!

The script isn't accessible to anyone else and before I find a solution to this frigging delay problem I can't be bothered to write anything extra to prevent PHP injections (isn't that what they are called?).
Quote from NotAnIllusion :Why is get_file_contents () better than using it as a resource? Also, would it be better then to write the entire config from a string to a file with file_put_contents ()? Currently I open (or create) the file for write access as a resource and fwrite each /key=value pair into it and close it.

I guess it achieves exactly the same result, but with less / shorter code.


<?php 
php
  
class LFSConfig extends DCFramework {
    function 
Parse ($file) {
      foreach(
$file as $line) {
        if (
preg_match("/^\/[a-z]/"$line)) {
          
$opt preg_split("/=/"$line2PREG_SPLIT_NO_EMPTY);
          
$opt[0] = preg_replace("/^\//"""$opt[0]);
          
$this->$opt[0] = trim($opt[1]);
        }
      }
    }
  }
  
LFSConfig->Parse(explode("\n"file_get_contents("setup.cfg")));

  function 
write_stuff($cfgObj$file$out "") {
    foreach(
$cfgObj as $key => $item) {
      
$out .= "/$key=$item\n";
    }
    
file_put_contents($file$out);
  }

?>

Quote from NotAnIllusion : .. uncommenting the appropriate line (which works in CGI) does not work. Any ideas?

IIRC apache will want the extensions in path/to/apache/bin, for mysql i think you need libmysql.dll in there too.
Ah yes, I see how the preg_split and explode would do it, thanks I'm almost certain I didn't have to copy anything anywhere previously when I used PHP 5 and Apache 2 but maybe I'm just dreaming of the way I want things done.. I'll copying the files (I do have libmysql.dll in the ext and php root dirs btw, I'll copy that too).

One more thing though, about newlines. You put "\n" there, but some (file?)systems use other endline chars. Is there any reliable way to determine which ones they are, or is there an abstracted method to read from all these different things?
Quote from NotAnIllusion :One more thing though, about newlines. You put "\n" there, but some (file?)systems use other endline chars. Is there any reliable way to determine which ones they are, or is there an abstracted method to read from all these different things?

You could 'detect' the newline type through php_uname(). Splitting by \n and trim()'ing the string is a neat way of getting an identical result on Linux/Unix and Windows. Do Mac's still use only \r ?
Yes, Mac's I think still use \r only (sod 'em, I say ). Currently I read PHPOS to determine the system and hence the endline, but that's kind of faulty since it shows the system PHP was built on. I'll have a look into the php_uname() thingy.

Copying php_mysqli.dll and libmysql.dll to apache/bin did get PHP as a module talking to MySQL. Thank you very much! Also as I suspected, none of that weird delay when accessing files/URLs, it was simply the CGI devil's work.

Hmm, what to ask next..
-
(thisnameistaken) DELETED by thisnameistaken
Sounds good to me. Ironically, I was doing that on some InSim packets but didn't notice I could do that here too. Cheers!

Nooby file access problem in PHP
(14 posts, started )
FGED GREDG RDFGDR GSFDG