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
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 have a file "tw.txt" with the text "test text" in it.
If I try to write "lol" in "tx.txt" with fwrite, the content ("test text") is simply erased and not replaced.
There is no error displayed by the server, however, I can see my Error: can't write in file.
CHMOD is set to 777 in every files and folders, from the "var" rep to the website folder. If I try to read a file with fopen, no problem. I tried to change the chmod with PHP... no success. I tried to append, it erase.
The code works fine on two other servers.
Any clues ? Thanks.
<?php
ini_set('display_errors', 'On');
ini_set('allow_url_fopen', '1');
error_reporting(E_ALL);
$fd=fopen("tw.txt","w") or die("Error: can't open file.");
//chmod("tw.txt", 511);
fwrite($fd,"lol") or die('Error: can't write in file.');
fclose($fd);
?>
Have you tried other opening modes?
If you need to append some data, you should try something like:
$fd=fopen("tw.txt","a+")
$myFile = 'tw.txt';
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, 'lol');
fclose($fh);
I am trying to create a file using php to a dirctory which has cmod 0777 so it should be fine.
Here is the code:
$fh = fopen("/_myfiles/myfile.txt", "w+");
if ($fh==false)
{
die("unable to create file");
}
But all I get is "unable to create file". Any ideas on what it could be?
Note: For the path I've also tried:
$fh = fopen($_SERVER['DOCUMENT_ROOT']."/_myfiles/myfile.txt", "w+");
with no success.
fopen() generates an E_WARNING message on failure.
I recommend using error_reporting(E_ALL) to show the warning and this should help you to troubleshoot the problem from there.
Check write permissions on the directory you want to create the file in.
Also the directory "_myfiles" should exist (it won't be created automatically).
If they are correct, then this will create the file in the same directory where the PHP script is located:
$basedir = dirname(__FILE__);
$fh = fopen($basedir . DIRECTORY_SEPARATOR . 'myfile.txt', 'w+');
I'm trying to create a php file which I can edit straight away without manually set the permissions.
I'm trying this...
<?php
$var = '<?php $mycontent = new Content(); echo $mycontent->block($p_name);?>';
$myFile = "testFile.php";
$fh = fopen($myFile, 'w+') or die("can't open file");
$stringData = $var;
fwrite($fh, $stringData);
fclose($fh);
?>
...it creates the file, but when I try to edit the file in my IDE it won't let me of course. I have to manually set the permission of the file created. Is there any way I can create the file and have the permission already set?
Thanks in advance
Mauro
Yes, you can thanks to PHP CHMOD
// Read and write for owner, read for everybody else
chmod("/somedir/somefile", 0644);
Since this aspect wasn't covered in previous answers I'll add it here:
chmod() will only take a path string as the 1st argument. So you cannot try to pass to resource that was open with fopen(), in this case $fh.
You need to fclose() the resource and then run chmod() with the file path. So a proper practice would be storing the filePath in a variable and using that variable when calling fopen() rather than passing it a direct string in the first argument.
In the case of the example code in the answer this would simply mean running chmod($myfile, 0755) (the permission code is only an example and be different of course.)
full code after corrections:
<?php
$var = '<?php $mycontent = new Content(); echo $mycontent->block($p_name);?>';
$myFile = "testFile.php";
$fh = fopen($myFile, 'w+') or die("can't open file");
$stringData = $var;
fwrite($fh, $stringData);
fclose($fh);
// Here comes the added chmod:
chmod($myFile, 0755);
?>
Php has chmod, works just like the Linux version.
I want to create a file on the webserver dynamically in PHP.
First I create a directory to store the file. THIS WORKS
// create the users directory and index page
$dirToCreate = "..".$_SESSION['s_USER_URL'];
mkdir($dirToCreate, 0777, TRUE); // create the directory for the user
Now I want to create a file called index.php and write out some content into it.
I am trying:
$ourFileName = $_SESSION['s_USER_URL']."/"."index.php";
$ourFileHandle = fopen($ourFileName, 'x') or die("can't open file");
fclose($ourFileHandle);
// append data to it
$ourFileHandle = fopen($ourFileName, 'a') or die("can't write to file");
$stringData = "Hi";
fwrite($ourFileHandle, $stringData);
But it never gets past the $ourFileHandle = fopen($ourFileName, 'x') or die("can't open file"); Saying the file does not exist, but that is the point. I want to create it.
I did some echoing and the path (/people/jason) exists and I am trying to write to /people/jason/index.php
Does anyone have any thoughts on what I am doing wrong?
PHP 5 on a linux server I believe.
-Jason
First you do :
$dirToCreate = "..".$_SESSION['s_USER_URL'];
But the filename you try to write to is not prefixed with the '..', so try changing
$ourFileName = $_SESSION['s_USER_URL']."/"."index.php";
to
$ourFileName = '..' . $_SESSION['s_USER_URL'] . '/index.php';
or probably tidier:
$ourFileName = $dirToCreate . '/index.php';
You are probably getting the warning because the directory you are trying to write the file into does not exist
It could be a result of one of your php ini settings, or possibly an apache security setting.
Try creating the dir as only rwxr-x--- and see how that goes.
I recall a shared hosting setup where "safemode" was compiled in and this behaviour tended to occur, basically, if the files/dirs were writable by too many people they would magically stop being acessible.
Its probably doc'd in php, but ill have to check.
why not use:
file_put_contents( $filename, $content )
or you could touch the file before writing to it.
Does the file 'index.php' already exist? When you fopen with the 'x' mode, if the file exists fopen will return FALSE and trigger a warning.
What i first noticed is you are making a directory higher in the tree, then attempting to make the php file in the current folder. Correct me if i'm wrong, but aren't you trying to make the file in the new created folder? if i recall php correctly (pardon me it's been a while, i'll probably add something from another language in here not noticing) here is an easier to understand way for a beginner, of course change the values accordingly, this simply makes a directory and makes a file then sets permissions.
<?php
$path = "..".$_SESSION['s_USER_URL'];
// may want to add a tilde (~) to user directory
// path, unixy thing to do ;D
mkdir($path, 0777); // make directory, set perms.
$file = "index.php"; // declare a file name
/* here you could use the chdir() command, if you wanted to go to the
directory where you created the file, this will help you understand the
rest of your code as you will have to perform less concatenation on
directories such as below */
$handle = fopen($path."/".$file, 'w') or die("can't open file");
// open file for writing, create if it doesn't exist
$info = "Stack Overflow was here!"; // string to input
fwrite($handle, $info); // perform the write operation
fclose($handle); // close the handle
?>