On a server I have the file some.php. I try to read the contents using file_get_content and fread. Both these don't read my content.
If I try some other URL like yahoo.com, these functions are reading the contents. Why does this happen?
$my_conetn = file_get_contents("http://example.com");
echo $my_conetn;
The above snippet is not working.
$handle = fopen($filename, "r");
$contents = fread($handle, 20);
echo $contents;
fclose($handle);
Also the above snippet is not working.
How to check my server? Is there any file read operation locked or not?
edit: I am not sure, but it is only my server file that can't be read. I can read the other server links. So I contact my hosting provider, then I get back to you guys/gals.
When using file functions to access remote files (paths starting with http:// and similar protocols) it only works if the php.ini setting allow_url_fopen is enabled.
Additionally, you have no influence on the request sent. Using CURL is suggested when dealing with remote files.
If it's a local file, ensure that you have access to the file - you can check that with is_readable($filename) - and that it's within your open_basedir restriction if one is set (usually to your document root in shared hosting environments).
Additionally, enable errors when debugging problems:
ini_set('display_errors', 'on');
error_reporting(E_ALL);
Related
I feel like this should be a pretty straightforward process.
I have the following code:
<?php
$filename = "c:/TestFolder/config.txt";
echo "Attempting to read: ".$filename."<br/>";
$fh = fopen($filename, 'r') or die("file doesnt exist");
$readtext = fread($fh, filesize($filename));
fclose($fh);
echo "The Text is: ".$readtext;
?>
I have checked that I do indeed have "config.txt" in a folder called "TestFolder" on my C:/ drive... but I keep getting an error that the file doesn't exist.
I checked my PHPInfo to ensure that "allow_url_fopen" is turned on.
I have also tried different file path variations, such as:
C:\\TestFolder\\config.txt
C:\/TestFolder\/config.txt
This doesn't seem to make a difference.
Any idea what might be preventing me from opening this file?
Edits:
It should be noted that this file is uploaded to my web host, while I am attempting to access a file on my local machine. Not sure if this changes things at all.
This is not possible. "local files" are files on the server where PHP is running, not the client running the web browser. While they might be the same machine when you're testing locally, once you upload the script to a web host, PHP tries to read files on the web host, not your machine.
The only way for PHP to access a file on the client machine is for the application to upload it.
Whether I'm trying to create a file, or use file_get_contents on a external url I just can't seem to get fopen (or curl) code to work. I have checked phpinfo and allow_url_fopen, allow_url_include & curl are all set to on and even looked at the php5/apache2/php.ini and they are all set to on.
But not matter what I do no php code works. Below are some examples (that work on wamp/localhost but not when moved to live web server)
create a html file in folder (works on windows localhost/wamp server)
if (!file_exists('media/folder')) {
mkdir('media/folder', 0755, true);
}
$config_file = 'media/folder/config.html';
$config_file_handle = fopen($config_file, 'w') or die('Cannot open file: '.$config_file); //implicitly creates file
$config_file = 'media/folder/config.html';
$config_file_handle = fopen($config_file, 'w') or die('Cannot open file: '.$config_file);
$config_data = 'add to my file content'.PHP_EOL.'
';
fwrite($config_file_handle, $config_data);
Does not work on live server. Quite frustrating as this needs to work and have tried many curl options but fail as well.
Below is another php code snippet that works locally but not live.
<?php
//works locally if external url used
//works locally if local file used
//Does not work live server if external url used
//Does work live server if local server url used
$string = file_get_contents('my url');
$json_a = json_decode($string, true);
foreach ($json_a as $person_name) {
//Person Name
echo $person_name['PersonName'];
}
?>
Cant really achieve what is have set out to do if I can't get this to work.
Any Help appreciated
An extremely common error, you may need to allow the user www-data to access your desired files. Simply right click on the file or folder you want click properties, go to the permissions tab and set www-data to the owner.
I'm making a utility that provides a GUI to easy edit certain values in a csv file on a remote server. My boss wants the utility in php running on the private webserver. I'm new to php, but I was able to get the GUI file modifier working locally without issues. The final piece now is rather than the local test file I need to grab a copy of the requested file off of the remote server, edit it, and then replace the old file with the edited one. My issue is uploading and downloading the file.
When I searched for a solution I found the following:
(note in each of these I am just trying to move a test file)
$source = "http://<IP REMOTE SERVER>/index.html";
$dest = $_SERVER['DOCUMENT_ROOT']."index.html";
copy($source, $dest);
This solution ran into a permissions error.
$source ="http://<IP REMOTE SERVER>/index.html";
$destination = $_SERVER['DOCUMENT_ROOT']."newfile.html";
$data = file_get_contents($source);
$handle = fopen($destination, "w");
fwrite($handle, $data);
fclose($handle);
This also had a permissions error
$connection = ssh2_connect('<IP REMOTE SERVER>', 22);
ssh2_auth_password($connection, 'cahenk', '<PASSWORD>');
ssh2_scp_recv($connection, '/tmp/CHenk/CHenk.csv', 'Desktop/CHenk.csv');
This solution has the error Fatal error: Call to undefined function ssh2_connect() which I have learned is because the function is not a part of the default php installation.
In closing, is there any easy way to read/write files to the remote server through php either by changing permissions, having the php extension installed, or a different way entirely that will work. Basically I'm trying to find the solution that requires the least settings changes to the server because I am not the administrator and would have to go through a round about process of getting any changes done. If something does need to be changed instructions on doing so or a link to instructions would be greatly appreciated.
Did you set the enable-url-fopen-wrapper in your php.ini?(only if your php version is older)
Please look # php remote files storing in example 2
I am trying to open an XML file in remote server through fopen() function.
I have two remote servers and the file permission of the xml is set to 777 in both servers.
i am able to open the xml from one server , but not from other. Both files can be opened in browser.
What all permission i need to set or what parameters i need to set in fopen() function.?
this is the function
function getFileData($ProjectName)
{
$file = fopen($ProjectName, "r") or exit("Unable to open File! ".$ProjectName);
$fileContent="";
while(!feof($file))
{
$fileContent.=fgets($file);
}
fclose($file);
echo $fileContent;
}
getFileData('http://serverA.com/myxml.xml'); // gives the content
getFileData('http://serverB.com/myxml.xml'); // gives the error fopen(http:/serverB.com/myxml.xml) [function.fopen]: failed to open stream: HTTP request failed!
You only have one / after http: in the last two lines - but I doubt that's the problem. Can you open the file in the browser?
Perhaps try using file_get_contents . It does the same as your function, except it's ready made and fully tested by the PHP guys.
I think there should be http://server and not http:/server.
Apart from the visible problem with URL, also check what is the value for allow_url_fopen set in your php.ini, if you can open file from one server but not from other server this may be correctly set for you, did you check about timeouts?
I have XAMPP installed on a windows 2000 server. everything is working great except the PHP fopen function. I can neither create nor open files with it. the strange thing is that I can include/require/file_get_contents/other file related functions; also fopen does not generate any errors or notices it just returns NULL.
I have gone as far as to grant full control of the file and all enclosing folders to everybody but i still get NULL instead of a file pointer.
I have tried this on php 5.2.9, 5.2.13, and 5.3.1 with the same effect. I have gone through the php.ini file looking for something that is breaking it; I have even tried deleting and using the basic ini file from a linux box where fopen is working, and still nothing.
I know I have to restart apache after changing my ini and all that and have been (I have even restarted the server) so thats not it.
I am at this poing assuming it is an apache configuration issue somehow, tomorrow im going to run a test through php-cli to make sure.
I really don't want to bruise my head anymore over this can some apache/php wizard come to my aid?
Thanks for the responses. You are right is is not any config problem. The problem has to be with one of my dlls or one of my included files. I just tried the same code that isn't working in a new file without any include. I disabled my custom libraries and it worked.
For the record here is what I was doing that wasn't working:
$test_file = 'c:\\test.csv';//everybody has full control. is very large.
if(file_exists($test_file) && is_readable($test_file)){
$fp = fopen($test_file, 'r');
echo var_export($fp, true);//outputs NULL. on my linux box this is a number.
if($fp !== false){
//do the work
fread($fp, 10);//throws the error that $fp is not a valid file handle
}
}
something that I am including must be breaking fopen somehow. Works as expected in new file with no includes.
Thanks for the responses. You are right as it is not any config problem. The problem has to be with one of my dlls or one of my included files. I just tried the same code that isn't working in a new file without any include and i disabled my custom libraries and it worked.
for the record here is what I was doing that wasn't working:
$test_file = 'c:\\test.csv';//everybody has full control. is very large.
if(file_exists($test_file) && is_readable($test_file)){
$fp = fopen($test_file, 'r');
echo var_export($fp, true);//outputs NULL. on my linux box this is a number.
if($fp !== false){
//do the work
fread($fp, 10);//throws the error that $fp is not a valid file handle
}
}
something that i am including must be breaking fopen somehow. works as expected in new file with no includes.
So we're to assume you're using this kind of code:
$fp = fopen( "my_file.txt", "r" );
$contents = fread($fp, filesize( "my_file.txt" ) );
fclose($fp);
Am I to assume the filesize() function returns null? Also try the stat($filename) function to see if you get a relatively complete array.
Let me know your results.