The Little Guy Posted October 26, 2009 Share Posted October 26, 2009 I would like to include a file, but instead of writing out the contents of that file to screen, I would like to read it into a binary safe string. Is there any function or method to do this? I found this at php.net: <?php $string = get_include_contents('somefile.php'); function get_include_contents($filename) { if (is_file($filename)) { ob_start(); include $filename; $contents = ob_get_contents(); ob_end_clean(); return $contents; } return false; } ?> but it tries to parse images as PHP, and I get an error message. before I was using fopen/fread, but that doesn't parse PHP, so any suggestions? Link to comment https://forums.phpfreaks.com/topic/179108-read-file/ Share on other sites More sharing options...
akitchin Posted October 26, 2009 Share Posted October 26, 2009 are you saying that you would like any PHP file to be parsed as PHP and store its output, but any file that ISN'T PHP to be read as a binary document? Link to comment https://forums.phpfreaks.com/topic/179108-read-file/#findComment-944995 Share on other sites More sharing options...
The Little Guy Posted October 26, 2009 Author Share Posted October 26, 2009 I got what I wanted If you have a better way let me know: $info = pathinfo($this->filePath); if($info['extension'] != 'php'){ $handle = fopen($this->filePath, 'rb'); $this->contents = fread($handle, filesize($this->filePath)); $this->set_headers(); fclose($handle); $this->result = $this->contents; }else{ $this->contents = $this->get_include_contents($this->filePath); $this->result = $this->contents; $this->set_headers(); } an additional question: when the php is read, it sometimes throws an error message if there is a PHP error. I am making a php web server, and when something like this happens, it shuts down my server. I would like to output the message to the web page instead of my server, then shutting down my server... C:\Documents and Settings\Owner>php c:\php_files\Pherver\start.php Pherver Service Started! Listening on port: 80 Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in C:\php_files\Pherver\www\home.php on line 6 C:\Documents and Settings\Owner> - line 1 I start the server - lines 2 & 3 gives some short info - line 4 shows the error (after a php page loads in the web browser that has an error) - line 5 shuts down the server I don't want lines 4 & 5 to happen in the server, I would instead like them to be turned into html and go to the web page. Does that make any sense? Link to comment https://forums.phpfreaks.com/topic/179108-read-file/#findComment-945008 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.