PHP readfile vs. file_get_contents
Readfile will read the file directly into the output buffer, and file_get_contents will load the file into memory, when you echo the result the data is copied from memory to the output buffer effectively using 2 times the memory of readfile.
file_get_contents or readfile for displaying filesystem image
If you don't need to manipulate the images (resizing, adding watermarks,...) readfile()
will be the best choice as it writes the file directly to the output buffer. file_get_contents()
will read the file into memory instead - requiring a lot of memory with large files.
PHP performance file_get_contents() vs readfile() and cat
file_get_contents
only loads the data from the file in memory, while both readfile
and cat
also output the data on the screen, so they just perform more operations.
If you want to compare file_get_contents
to the others, add echo
before it
Also, you are not freeing the memory allocated for $foo. There is a chance that if you move the file_get_contents as last test, you will get different result.
Additionally, you are using output buffering, which also cause some difference - just try to add the rest of the functions in an output buffering code to remove any differences.
When comparing different functions, the rest of the code should be the same, otherwise you are open to all kinds of influences.
Do file_get_contents and readfile execute PHP code?
file_get_contents
and readfile
do not execute code. All they do is return the raw contents of the file. That could be text, PHP code, binary (e.g. image files), or anything else. No interpretation of the files' contents is happening at all.
The only situation in which it may appear as if execution is happening is:
<?php ?>
tags will likely be hidden by the browser because it's trying to interpret them as HTML tags, so this may lead to the impression that the PHP disappeared and hence may have been executed.- You're reading from a source which executes the code, e.g. when reading from
http://example.com/foo.php
. In this case the functions have the same effect as visiting those URLs in a web browser: the serving web server is executing the PHP code and returning the result, butfile_get_contents
merely gets that result and returns it.
Difference between file, file_get_contents, and fopen in PHP
The first two, file
and file_get_contents
are very similar. They both read an entire file, but file
reads the file into an array, while file_get_contents
reads it into a string. The array returned by file
will be separated by newline, but each element will still have the terminating newline attached, so you will still need to watch out for that.
The fopen
function does something entirely different—it opens a file descriptor, which functions as a stream to read or write the file. It is a much lower-level function, a simple wrapper around the C fopen
function, and simply calling fopen
won't do anything but open a stream.
Once you've open a handle to the file, you can use other functions like fread
and fwrite
to manipulate the data the handle refers to, and once you're done, you will need to close the stream by using fclose
. These give you much finer control over the file you are reading, and if you need raw binary data, you may need to use them, but usually you can stick with the higher-level functions.
So, to recap:
file
— Reads entire file contents into an array of lines.file_get_contents
— Reads entire file contents into a string.fopen
— Opens a file handle that can be manipulated with other library functions, but does no reading or writing itself.
PHP readfile or file_get_contents in a loop
Some variation of the below is what i would use. YMMV depending on what you're doing. If you post your code we can address your specific implementation instead of just providing alternate solutions :-)
$dir = new DirectoryIterator('/path/to/states');
foreach($dir as $file)
{
if(!$file->isDot() && $file->isFile() && strpos($file->getFilename(), '.txt') !== false)
{
$content = file_get_contents($file->getPathname());
if($content)
{
// do your insert code
}
}
}
file() VS file_get_contents() which is better? PHP
They're unrelated, actually.
file()
reads the file into an array by parsing EOL characters, while file_get_contents()
actually reads the file contents into a string.
Also, this means you are at the mercy of the server operating system, since Windows EOL is different from Linux EOL (for example).
So, the first one is text-friendly, while the second one binary-friendly.
In short, don't try doing file('image.png');
Related Topics
How to Dynamically Build Queries with Pdo
Using PHP as a Template Engine
Cannot Send Session Cookie - Headers Already Sent
Use Openssl_Encrypt to Replace Mcrypt for 3Des-Ecb Encryption
How to Dynamically Populate Options on Dropdown Lists Based on Selection in Another Dropdown
Why Session Object Destruction Failed
PHP String Manipulation: Extract Hrefs
PHP Integer and Float Comparison Mismatch
Mysqli_Stmt::Execute() Expects Exactly 0 Parameters, 1 Given
Trying to Get Property of Non-Object - Codeigniter
Regex/ Code to Fix Corrupt Serialized PHP Data
Why Does Index.HTML Have Priority Over Index.Php
How to Have Codeigniter Load Specific Pages Using Ssl
Get Last Modified File in a Directory
How to Compare 2 HTML Pages, and Output Only the Different Bits in Ruby or PHP