Jump to content

read file


The Little Guy

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.