file_get_contents subtitute function or methods? - php

In one's server, file_get_contents is disabled for security reasons. I need to retrieve xml data. So, what is the best thing to do to :
Verify that file_get_contents is supported by a server ?
Is there any subtitute methods of file_get_contents ?

You can check whether or not you can use url's in file_get_contents() (and the fopen() family of functions) by checking the ini directive allow_url_fopen:
ini_get('allow_url_fopen');
You can get around these restrictions by using:
cURL
fsockopen()
I strongly recommend cURL. fsockopen() is a lot dirtier.

http://www.phpbuilder.com/board/showthread.php?t=10349521 - this seems to be a releavant topic
I would try to use simply fopen and fread functions.
First approach:
$bufferSize = 1024;
$file = fopen($file,'r');
while($cont = fread($file, $bufferSize)){
$file_content .= $cont;
}
fclose($file);
var_dump($file_content);
Second approach:
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);

Related

Why fread (PHP) don't read entire file?

I have this code on PHP that load a local file:
$filename = "fille.txt";
$fp = fopen($filename, "rb");
$content = fread($fp, 25699);
fclose($fp);
print_r($content);
With this code I can see all the contents of the file. But when I change the $filename to a external link, like:
$filename = "https:/.../texts/fille.txt";
I can't see all the contents of the file, he appears cut to me. Whats the problem?
The fread() function can be used for network operations. But network connections work different than file system operations. A network cannot read a bigger file in a single attempt, that is not how typical networks work. Instead they work package based. So data arrives in chunks.
And if you take a look into the documentation of the function you use then you will see that:
Reading stops as soon as one of the following conditions is met:
[...]
a packet becomes available or the socket timeout occurs (for network streams)
[...]
So what you observe actually is documented behavior. You need to continue to read packages in a loop to get the whole file. Until you received an EOF.
Take a look yourself: https://www.php.net/manual/en/function.fread.php
And further down in that documentation you will see that example:
Example #3 Remote fread() examples
<?php
$handle = fopen("http://www.example.com/", "rb");
if (FALSE === $handle) {
exit("Failed to open stream to URL");
}
$contents = '';
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);
?>

PostgreSql Large Object to file through PHP PDO

This code outputs LO to browser:
...
$my_pdo_connect->beginTransaction();
$stream = $my_pdo_connect->pgsqlLOBOpen($oid, 'r');
fpassthru($stream);
...
But I stuck with writing LO to file system.
I am aware about pg_lo_export and lo_export, but there is
restriction to use PHP PDO capabilities only.
Obviously I should use some php-function instead of fpassthru($stream) to write the stream into a file, can't find suitable docs or example.
Finally I found how to solve the issue:
$my_pdo_connect->beginTransaction();
$stream = $my_pdo_connect->pgsqlLOBOpen($oid, 'r');
$file = fopen('my_file', 'w');
stream_copy_to_stream($stream, $file);
fclose($file);

Is there an alternative to php readfile() in safe mode server?

I host my site on a shared hosting, which lately changed the server to safe mode (without even notifying that).
I use a feature that download files from the server, using the readfile() function (I use php).
Now, in safe_mode, this function is no longer available.
Is there a replacement or a workaround to handle the situation that the file will be able to be downloaded by the user?
Thanks
As I wrote in comments, readfile() is disabled by including it in disable_functions php.ini directive. It has nothing to do with safe mode. Try checking which functions are disabled and see if you can use any other filesystem function(-s) to do what readfile() does.
To see the list of disabled functions, use:
var_dump(ini_get('disable_functions'));
You might use:
// for any file
$file = fopen($filename, 'rb');
if ( $file !== false ) {
fpassthru($file);
fclose($file);
}
// for any file, if fpassthru() is disabled
$file = fopen($filename, 'rb');
if ( $file !== false ) {
while ( !feof($file) ) {
echo fread($file, 4096);
}
fclose($file);
}
// for small files;
// this should not be used for large files, as it loads whole file into memory
$data = file_get_contents($filename);
if ( $data !== false ) {
echo $data;
}
// if and only if everything else fails, there is a *very dirty* alternative;
// this is *dirty* mainly because it "explodes" data into "lines" as if it was
// textual data
$data = file($filename);
if ( $data !== false ) {
echo implode('', $data);
}
I assume you are using readfile to load remote files, as you said "from the server". If that is correct, your problem is not safe mode but that opening URLs with normal php file functions is not permitted anymore (setting allow_url_fopen disabled).
In that case, you can use PHP's curl functions to download files. Also, file_get_contents is a valid alternative.

Alternative way to read raw I/O stream in PHP

I am trying to find an alternative to reading php://input. I use this for getting XML data from a CURL PUT.
I usually do this with:
$xml = file_get_contents('php://input');
However, I'm having a few issues with file_get_contents() on Windows.
Is there an alternative, perhaps using fopen() or fread()?
Yes, you can do:
$f = fopen('php://input', 'r');
if (!$f) die("Couldn't open input stream\n");
$data = '';
while ($buffer = fread($f, 8192)) $data .= $buffer;
fclose($f);
But, the question you have to ask yourself is why isn't file_get_contents working on windows? Because if it's not working, I doubt fopen would work for the same stream...
Ok. I think I've found a solution.
$f = #fopen("php://input", "r");
$file_data_str = stream_get_contents($f);
fclose($f);
Plus, with this, I'm not mandated to put in a file size.

Best way to download a file in PHP

Which would be the best way to download a file from another domain in PHP?
i.e. A zip file.
The easiest one is file_get_contents(), a more advanced way would be with cURL for example. You can store the data to your harddrive with file_put_contents().
normally, the fopen functions work for remote files too, so you could do the following to circumvent the memory limit (but it's slower than file_get_contents)
<?php
$remote = fopen("http://www.example.com/file.zip", "rb");
$local = fopen("local_name_of_file.zip", 'w');
while (!feof($remote)) {
$content = fread($remote, 8192);
fwrite($local, $content);
}
fclose($local);
fclose($remote);
?>
copied from here: http://www.php.net/fread
You may use one code line to do this:
copy(URL, destination);
This function returns TRUE on success and FALSE on failure.

Categories