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.
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.
While testing the following code on my local machine, no error was reported. But after testing it on our server at work, I got the following strange error: php is not writing files on the server.
code I am using :
$myfile = fopen( "results/co".$a[$i].".txt", "w") or die("Unable to open file!");
fwrite($myfile,"some text");
fclose($myfile);
So no file is created in this case.
when I try to replace the file name "results/co".$a[$i].".txt" with its value : "results/co00112test.txt" :
$myfile = fopen( "results/co00112test.txt", "w") or die("Unable to open file!");
fwrite($myfile,"some text");
fclose($myfile);
it works just fine.
I also tried the following:
$name = "results/co".$a[$i].".txt";
$myfile = fopen( $name, "w") or die("Unable to open file!");
fwrite($myfile,"some text");
fclose($myfile);
yet with no hope.
What could be the reason for this error ?
If your code works fine when you're putting some static filename, it's obvious that your variable $a[$i] is wrong.
You can proceed that way to debug it :
$name = "results/co".$a[$i].".txt";
var_dump($name);
The name should show something weird, and will tell you what's happening to your file.
it might be all because of permissions. Your php user don't have permission to folder, where you want to create file or don't have permission to file, which you are trying to edit (if file is already there).
You can read full article about permissions here (it's about ubuntu, but it doesn't really matter which OS): https://help.ubuntu.com/community/FilePermissions
you can try permission 644 or if it's not working you can use 777. 644 is more safe, because only the owner of the file will be able to edit it, not any user of your OS.
you can change permissions to folder and all files/folders in it like this:
chmod -R 644 /path/to/folder/
I think 644 is standard for files and 755 for directories.
you can change owner of the file/folder like this:
chown name_of_user:name_of_group /path/to/folder
Your file name is starting with double zero char so it might be an issue.
this will not work :
$number = 112;
$file_name = "results/co".$number."test.txt";
this should work :
$number = 112;
$number_pad = str_pad($number, 5, '0', STR_PAD_LEFT);
$file_name = "results/co".$number_pad."test.txt";
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.
I am saving a file on my server through file handling like this
$my_file = $_SERVER['DOCUMENT_ROOT'].'/wp-content/themes/mythemev2.0/'.$name.'.php';
echo $my_file;
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
fwrite($handle, $res->name);
$my_file perfectly shows me the complete path of the file. But when I go to the particular directory I am unable to see the file. What could be the error? What else can I do for debugging ?
Make sure you're calling fclose() after fwrite()
I want to HTTP POST data to a PHP file which will in-turn write the data into a file. The script I used is as follows,
<?php
#error_reporting(E_ERROR | E_PARSE);
$msglen=strlen($_POST["msgarea"]);
$msg=$_POST["msgarea"];
$fp = fopen("dinesh.txt", 'w');
fwrite($fp, $msg);
fclose($fp);
echo "Data Written -> $msg";
?>
I am hosting this script file in sourceforge.
I have already just created the empty file dinesh.txt and placed the file in the same directory as that of the script file. But unfortunately its not written in the file.
What is the reason ?
Thanks in advance
Check that $_POST["msgarea"] is not empty and the file is writeable for the user who tries to write it.
And check the logs for errors of course.
<?php
error_reporting(E_ALL);
var_dump($_POST["msgarea"]);
$msglen=strlen($_POST["msgarea"]);
$msg=$_POST["msgarea"];
$fp = fopen("dinesh.txt", 'w');
$result = fwrite($fp, $msg);
fclose($fp);
if ($result) {
echo "Data Written -> $result";
} else {
echo "Error";
}
I think you are writing in read-only webspace, check file permissions and path
From: http://sourceforge.net/apps/trac/sourceforge/ticket/2772
In generally it is not allowed to write to folders and files that reside in the project's web space. If you need to write to the file system you should use the folder named persistent that is on the same level as htdocs.
I use on my computer:
$ mkdir persistent
$ chmod 0777 persistent
$ scp -r persistent my_account#web.sourceforge.net:/home/project-web/my_project/
And set in PHP:
$fp = fopen("../persistent/dinesh.txt", 'w');
And it works!
Edit:
You can get access for SSH console for 4 hours (https://sourceforge.net/apps/trac/sourceforge/wiki/Shell%20service). And you can go to web directory of your project, make dirs, set privileges, remove files etc. Midnight Commander is available.
you can, check that folder permission, do they have 777 permission
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Bobby Bopper\n";
fwrite($fh, $stringData);
fclose($fh);