I'm trying to save some text that comes into a php script via a POST to a file. Here is my script:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$file = $path . '\test.txt';
$text = $_POST['text'];
// save it to a file
if (file_exists($file))
chmod($file, 0777);
$handle = fopen($file, 'w');
fwrite($handle, $text);
fclose($handle);
echo "success";
?>
I'm getting this error:
Warning:
fopen(D:\Hosting\11347607\html\test.txt) [function.fopen]: failed to open stream:
Permission denied in
D:\Hosting\11347607\html\test_file_saver.php on line
12
I've tried a number of different attempts and read many posts. The directory is set to RWX permission. How can I get permission to write to this file? Thanks!
If you don't have permissions to even open the file, what makes you think you have permission to change permissions?
That wasn't a question or a comment. You don't have permission to change the permission. Give your PHP process proper permissions.
Related
I am trying to get a PHP script to take input from an html form to create a csv file. My PHP script is below:
<?php
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$row = [$fname, $lname, $email];
$file = fopen("mini4.csv", 'w');
for ($i=0; $i<3; $i++){
fwrite($file, $row[$i]);
if($i!=2){
fwrite($file, ", ");
}
}
fclose($file);
$file = fopen("mini4.csv", "r");
$data = fgetcsv($file);
echo file_get_contents( "data.csv" );
fclose($file);
?>
My PHP file has the following permissions : -rw-r--r--. The directory the PHP file is held in has the following permissions: drwxr-xr-x.
From everything I've read, issues like this are due to permissions, although, looking at other posts my permissions on the PHP file and directory above it are correct.
I saw some answers suggesting chmod -R 777 to the directory, but I am worried about potential security issues this might cause.
When I look at the access log on XAMPP, i see the following:
::1 - - [27/Feb/2022:17:23:14 -0500] "POST /mini4/mini4.php HTTP/1.1"
500 -
I turned on screen reporting and I am getting this:
Warning: fopen(mini4.csv): Failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/mini4/mini4.php on line 12.
Although, I do not see how this can be given the permissions my file and directory have.
The permissions assigned to your PHP file are irrelevant. The permissions assigned to the folder your PHP script is trying to write to are important because they must grant read/write access to the user that Apache is using. Typically, that's the user/group apache:apache and that's different from the user you'll be logged in as.
Mostly it's enough to chmod g+rw <myFolder>, but this depends on the existing setup.
FWIW, your code is inordinately convoluted. You could just do this:
$file = fopen("mini4.csv", 'w');
fputcsv($file, [$_POST['fname'], $_POST['lname'], $_POST['email']]);
fclose($file);
readfile("mini4.csv");
Be aware that this need additional validation before using it in anger.
I am trying to create and write a file to ec2 instance. I am getting the error below despite the folder that i am writing possessing all the permissions required.
$handle = fopen("thumbnailFolder/testFile.txt", "r"); // line 4
if($handle != false){
echo 'File created!';
}else{
echo 'File create failed!';
}
The 'thumbnailFolder' has the following permissions:
drwxrwxrwx
Error message:
fopen(thumbnailFolder/testFile.txt): failed to open stream: No such file or directory in /var/www/html/book_aws/my_server/folder/web/thumbnailTest.php on line 4
File create failed!
As the error clearly say. System is failing to open the file which is not there.
$handle = fopen("thumbnailFolder/testFile.txt", "r");
Above code open files in read mode. if there is no files then throws an error.
If you want to open file to write then use try below code, this tries to open file and sets pointer at the begining if file is not there then creates the file in given name. for read and write use w+ instead of w
$handle = fopen("thumbnailFolder/testFile.txt", "w");
There are different modes with respect files. you can check below link for further details.
http://php.net/manual/en/function.fopen.php
I'm getting this error:
Warning: fopen(name.txt): failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/phptests/post.php on line 5
Could not write file
I'm trying to retrieve form data and pass it into a text file like so:
<?php
$name = $_POST['name'];
$surname = $_POST['surname'];
$fh = fopen("name.txt", "w") or die("Could not write file");
fwrite($fh, $name, $surname);
fclose($fh);
?>
I'm using Komodo Edit and my php files are saved in the "htdocs" folder provided by XAMPP.
Can anyone tell me why I'm not able to write the file?
As the error telling you the file is not writable.
To fix it , you can simply update the file permission
cd <directory of name.txt>
chmod 777 name.txt
I think it's because of that you are writing fwrite wrong.
You are doing:
fwrite($fh, $name, $surname);
You should do:
fwrite($fh, $name. ', '. $surname);
Function fwrite() excepts 2 parameters. You are giving 3.
I ran into a really bizarre problem. I am trying to perform writing to file using fopen().
This is what I tried in writetofile.php:
$fw = fopen('/test.txt', 'w');
fwrite($fw, 'hello world' . "\r\n");
fclose($fw);
This is the error I keep getting:
Warning: fopen(/test.txt):
failed to open stream: Permission denied in C:\inetpub\wwwroot\writetofile.php on line 41
Warning: fwrite() expects parameter 1 to be resource, boolean given...
I am 100% sure I have permissions to the server. I am the Administrator. Furthermore, I temporarily gave full permissions to everyone. I even tried running the php script locally, directly from the server using localhost. I am not using apache, I am using IIS. I tried restarting IIS after modifying permissions. I am not running php in safe mode.
Any idea on what might be causing this issue?
/test.txt would be a file in the ROOT directory of your filesystem, where user accounts generally do NOT have write privileges (unless you're running this code as root). This is especially true of PHP running under the webserver's user account.
You probably want just test.txt (no leading slash)` which will try to put the file into the script's "current working directory" - usually the same directory the script itself is in.
1- when you rollout website, delete all logs folder names
2- inside the code create folder name as below and create the logs insides
3- write at top of file. (during init the web)
$ClientUserName = gethostbyaddr($_SERVER['REMOTE_ADDR']);
function Data_Log($dataline)
{
global $ClientUserName;
$dir = 'UserInputLog' ;
$fileName = $ClientUserName. '_ServerWebLog.txt';
if(is_dir($dir) === false)
mkdir($dir);
$fileName = $dir. '\\'.$fileName;
$myfile = fopen($fileName, "a") or die("Unable to open file!");
fwrite($myfile, "$dataline\r\n");
fclose($myfile);
}
I cannot update any txt files using php. When I write a simple code like the following:
<?php
// create file pointer
$fp = fopen("C:/Users/jj/bob.txt", 'w') or die('Could not open file, or fike does not exist and failed to create.');
$mytext = '<b>hi. This is my test</b>';
// write text to file
fwrite($fp, $mytext) or die('Could not write to file.');
$content = file("C:/Users/jj/bob.txt");
// close file
fclose($fp);
?>
Both files do exist in the folder. I just cannot see any updates on bob.txt.
Is this a permission error in windows? It works fine on my laptop at home. I also cannot change the php files on my website, using filezilla.
It may very well be a file permissions issue.
Try your code using a direct pointer to the file instead of a path to it, using the following code:
I added a chmod directive. See the comments above chmod ($file, 0644);
Tested succesfully on my hosted WWW website:
<?php
// create file pointer
$file = "bob.txt";
$fp = fopen($file, 'w') or die('Could not open file, or fike does not exist and failed to create.');
// chmod ($file, 0777); // or use 0777 if 0644 does not work
chmod ($file, 0644);
$mytext = '<b>hi. This is my test</b>';
// write text to file
fwrite($fp, $mytext) or die('Could not write to file.');
$content = file("bob.txt");
// close file
fclose($fp);
?>
Perhaps you have to set the permission from bob.txt to 0777 (or something else). In FileZilla it is very easy, because you can just check the permissions you want.