Unable to open file while writing php file - php

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");
// ...

Related

PHP fopen doesn't find existing file

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");

How can I read .txt file from my server [PHP]?

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';

PHP Write File Error

I have a page called index.php which is calling a function "writelog" in includes/Logger.php
I have file located at includes folder and code is as below.
function writelog($logText){
$myFile = "testlog.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $logText + "\n";
fwrite($fh, $stringData);
fclose($fh);
}
It shows errror "can't open file" . I have set FullPermission for everyone and still it says it cant access file.I tried to put file in same folder as index.php and same error. What can be possible cause ? Am I having wrong path ?
Try using the full path of the log file
$myFile = "/full/path/to/testlog.txt";
I am assuming this file is also in includes, I'm guessing this is called from another script so the path would be one of the calling script. You can use this:
$prevdir = getcwd();
chdir(dirname(__FILE__));
$myFile = "testlog.txt";
chdir($prevdir);
But it's best to use absolute paths

How to use the php fopen() correctly

I am learning php, trying to use the fopen() function.
The php file I am coding is in this directory /domains/xxxxx.com.au/public_html/phpfile.php
What path do I specify for the file to be opened, the example I am looking at is based on a server on a pc where this is the file path $filename = "c:/newfile.txt"; not an online server.
UPDATE!
This is the whole script, I have the file location correct, now the4 script is returning "couldnt create the file" does this have something to do with ther permission of the folder location of the file?
<?php
$filename = "/domains/xxxxxxxx.com.au/public_html/newfile.txt";
$newfile = #fopen($filename, "w+") or die ("couldnt create the file");
fclose($newfile);
$msg = "<p>File Created</p>";
?>
<HTML>
<HEAD>
</HEAD>
<BODY>
<? echo "$msg" ?>
</BODY>
</HTML>
Assuming that your php file is inside the public_html too, you could use :
$fp = fopen( "newfile.txt", "rt" );
Or, giving the full path :
$fp = fopen( "/domains/xxxxx.com.au/public_html/newfile.txt", "rt" );
This will open it if it already exists.
Refer to this for further details of opening flags.
UPDATE:
You can even use the is_writable/is_readable function to check file access before trying to open it.
Read http://us2.php.net/manual/en/function.fopen.php Example #1 is relevant for a Unix system.

How to map file path from different folder location easily?

how to map the path to the file easily?
public_html/api/function.php
<?php
function writetologfile($content)
{
$filename = 'logfile/testing_randomstring.txt';
if (!$handle = fopen($filename, 'a'))
{
echo "Cannot open file ($filename)";
exit;
}
fclose($handle);
}
?>
the actual path of the text file is in public_html/r/admin/logfile/testing_randomstring.txt
so if I run the script at public_html/folder1/folder2/addlog.php, it won't be able to find the path to the testing_randomstring.txt
addlog.php
<?php
include("../../api/function.php");
writetologfile('hahaha');
?>
How I can able to easily point to this text file path, no matter where my php calling script is from.
I tried to change $filename = 'logfile/testing_randomstring.txt'; inside writetologfile function by enforcing it to absolute fix path,
something like $filename='/r/admin/logfile/testing_randomstring.txt',
but it is not working
Instead of using a relative path, you could specify an absolute path. Assuming public_html is in your home directory, try this:
$filename = '/public_html/r/admin/logfile/testing_randomstring.txt';
fopen(getenv('HOME') . $filename, 'a');
This uses getenv to read the contents of the environment variable $HOME.

Categories