I'm creating a Telegram Bot in PHP.
My aim now is read from a .txt file a integer and put him in a variable.
This text.txt is uploaded in a server. I tryed to do this:
$filename = "http://<MY HOST NAME>/test_bot/file/text.txt";
$fp = fopen($filename, "r+");
$send = fgets($fp);
fclose($fp);
echo $send;
But when I try to open my index.php, nothing comes out written.
am I doing something wrong?
Since you are opening from a remote host you can't open for read/write, just read. Try this instead:
$fp = fopen($filename, "r");
If you are trying to open a file on the same server as the PHP script then do not use the "http://..." path but instead the local file path and then you can open it for read/write. In the case of your script it does not appear you need write access so "r" should be sufficient.
To access without http just use the path to the file:
$filename = '/path/to/file/text.txt';
$fp = fopen($filename, "r+");
Or if you want a relative path from the script itself I prefer:
$filename = dirname(__FILE__) . '/../some/relative/path/text.txt';
Related
I am trying to use fopen and fwrite
$book="http://bittotb.com/synthesis_study_material/student_admin/include/uploaded/epub/923960_hotel (1).epub";
$path=$book.".php";
$myfile =fopen($path, "w");
$txt = "<?php include('hello.php'); ?>";
fwrite($myfile, $txt);
fclose($myfile);
when I just write the file name in fopen like
$myfile =fopen("abc.php", "w");
then it's making a file in the same directory but I want to make that file in another directory. while using path it's not working if I echo the $path then I am getting
http://bittotb.com/synthesis_study_material/student_admin/include/uploaded/epub/923960_hotel (1).epub.php
this is correct file name and path but still, it's giving me Unable to open the file and my folder permission shows 0777
You have to use path on the server, not the URL of page.
For example, you page can have URL http://example.org/index.php. The file can be on the server known as /var/www/example.org/index.php.
Use this code to determine your directory:
<?php
echo getcwd();
If the code above shows /var/www/example.org/, file http://example.org/test.php has file path /var/www/example.org/test.php. But it is better to use relative paths. (see below)
If you have page http://example.org/index.php and you want create http://example.org/test.php, use this:
$file = fopen("test.php", "w");
fwrite($file, "<?php echo 'Hello World'; ?>");
fflush($file);
fclose($file);
If you want to write to file http://bittotb.com/synthesis_study_material/student_admin/include/uploaded/epub/file.php from script http://bittotb.com/synthesis_study_material/student_admin/module/corses/file.php, use relative path:
$file = fopen("../../include/uploaded/epub/file.php", "w");
// ...
I'm currently writting a login-system with PHP, for that I need to read the files with some user-information in it.
But after changing the folder system, PHP fopen doesn't read the files anymore.
Both the users.php and userinf.csv files are in the samle folder.
I allready tried to change the filepath, hard-coded the filepath , recreated the file. All of which file.
//Read file
$fp = fopen("userinf.csv", "r");
if(!$fp)
{
echo "File couldn't be read";
return false;
}
Before changing the file system, it worked. But now I am geting the error:
Warning: fopen(userinf.csv): failed to open stream: No such file or directory in FILEPATH on line 45
When you use the fread function without any reference it could fail. I always say that you need to check your path first with getcwd()
<?php
echo getcwd(); //Current Working Directory
?>
Use absolute paths, always. It removes any ambiguity. Using a relative path may change based on where your script is located, among other things, depending on your system.
$fp = fopen("/home/somewhere/blah/userinf.csv", "r");
You can always use a variable for the path as well:
// Somewhere in your code
define('ROOT_PATH', "/home/somewhere/blah");
// In the implementation
$fp = fopen(ROOT_PATH . "/userinf.csv", "r");
I have Ubuntu installed on an Odroid. I need this box to connect to a remote website to check a .txt file. I have used the code below on two remote servers and I get the result I need. However, running the code on the odroid, I get "Unable to open file!".
I am sure this must be something to do with the PHP settings but I am now at a loss as to what it could be. Any help would be greatly appreciated.
$filename = "ftp://username:password#80.1.1.1/version.txt";
$handle = fopen($filename, "r") or die("Unable to open file!");
$contents = fgets($handle);
echo "Version = " . $contents . "<br>";
fclose($handle);
Try to open file by providing document root path with filename.
$rootPath = $_SERVER['DOCUMENT_ROOT'];
$filename = $rootPath.'version.txt';
I'm writing a function in php, client side I have a canvas image which I use toDataUrl() along with a file name to save the image on the server. The here's the code:
<?php
$imageData=$GLOBALS['HTTP_RAW_POST_DATA'];
$data = json_decode($imageData, true);
$file = $data["file"];
$image = $data["data"];
$filteredData=substr($image, strpos($image, ",")+1);
$unencodedData=base64_decode($filteredData);
$fp = fopen( 'image/' . $file , 'wb' );
fwrite( $fp, $unencodedData);
fclose( $fp );
?>
The thing is that this code works. And for two out of three of the pages I used it on it works fine. The problem is when I copy and pasted it a third time to implement it again, for some reason the file is made on the server except that no data get's written into the file. I don't think it's a problem client side because I write in a debug alert message in the javascript and a debug echo into the PHP and both are able to print out the data fine. I made this short debug file:
<?php
$fp = fopen('data.txt', 'wb');
if(is_writable('data.txt')){
echo "file is writable<br>";
}
if(fwrite($fp, 'test') == FALSE){
echo "failed to write data<br>";
}
fclose($fp);
?>
And the output is
file is writable
failed to write data
I've tried using chmod and setting everything, the folder, the text file before I write to it to 0777 and I still get the same result; the file is made but no data is written into it. Is there anything I'm missing or any other approaches that might help. I haven't found anything on google and am still baffled as to why the same code worked exactly as expected twice before suddenly stopping for no apparent reason.
Thanks in advance.
I know this is an old post, but I had a very similar problem and found a solution (for me at least)! I ran out of disk space on my server, so it could create a 0 byte file, but wouldn't write to it. After I cleared out some space (deleted a 13gb error.log file) everything started working again as expected.
If fopen works but fwrite mysteriously doesn't, check your disk space. 'df -h' is the command to check disk space on a linux server.
instead of $fp = fopen('data.txt', 'wb'); give $fp = fopen('data.txt', 'w'); and try
Changed "wb" to "w"
When you write $fp = fopen('data.txt', 'w'); for your domain website.com having root at /var/www/website/ and if the php file is located at /var/www/website/php/server/file/admin.php or something similar, it will actually create a file at /var/www/website/data.txt
Try giving absolute path or path relative to your domain root to create files like,
$fp = fopen('php/server/file/data.txt', 'w');
Try the find command to see if the file is created anywhere else in the folder directory by using the following in Ubuntu,
find /var/www/website/ -name 'data.txt'
I had this issue, probably can help you solve if you have similar issue.
I'm writing tests for my CodeIgniter site using simpletester.
One of the things that it does is offer autogenerated files.
I want to test if the output of a file is correct.
However, if I do this:
function testFunction1(){
$url = site_url('downloader/function1');
$handle = fopen($url,'r');
$contents = stream_get_contents($handle);
echo $contents;
fclose($handle);
}
It outputs a 404.
It works perfectly when I put "www.google.com" as the url.
I can also download the file if I copy paste the URL in the browser.
Thanks!
edit:
Even if I try to download a regular file (so not a php function), with the full url, it gives the 404.
when your using relative paths, simply remove site_url() for relative path instances.
fopen accepts the following:
$handle = fopen("/home/rasmus/file.txt", "r");
$handle = fopen("/home/rasmus/file.gif", "wb");
$handle = fopen("http://www.example.com/", "r");
$handle = fopen("ftp://user:password#example.com/somefile.txt", "w");
But site_url seems to be adding something.
also be sure to check the relative path directory is corrent.
If you're accessing locally then just use
$url = "main/function":
$file_content = file_get_contents("http://localhost/codeignitor/" . $url);