I am trying to create a file on my web server with an option chosen from a web form. The file never gets created and I keep getting the "Can't Open File" message.
<?php
if(isset($_POST["style"])) {
$boots = $_POST["style"];
}
$file = "bootsstyle";
if(!file_exists($file)) {
touch($file);
chmod($file, 0777);
}
$openFile = fopen($file, "w+") or die("Can't open file");
fwrite($openFile, $boots);
fclose($openFile);
?>
I have been scouring the Internet for an hour and am not sure where I am going wrong. I have checked the permissions in the directory and they are 0777.
$boots = "your data";
$file = "bootsstyle";
if(!file_exists($file)) {
touch($file);
chmod($file, 0777);
}
$openFile = fopen($file, "w+") or die("Can't open file");
fwrite($openFile, $boots);
fclose($openFile);
$myfile=fopen($file, "r");
echo fread($myfile,filesize($file));
fclose($myfile);
The problem is with the permissions for the directory in which you are trying to create the file. The directory in which the php script is located must be writable by the user group named www-data. There are two methods to solve this.
Method 1: Changing permission of directory
You can just change the permission of directory to 777. To do this, open a terminal and navigate to the directory in which your php script resides. Now execute the command sudo chmod 777 ./
Method 2: Making www-data as owner of directory
Open a terminal and navigate to the directory containing the php script. Now, execute the command sudo chown www-data ./ && sudo chmod 774 ./
You can learn more about Linux permissions and the difference between 774 and 777 from https://linode.com/docs/tools-reference/linux-users-and-groups/
Note:Opening a file in w+ mode create the file if it does not exist. So you do not have to touch it.
Note:The above code is tested on kali linux 2017.3.2 running LAMP server.
Related
Heys Guys! I'm trying to create a new xml file with php on my ubuntu server. The code looks like
$myfile = fopen("test.xml", "w") or die ("Unable to write file");
fwrite($myfile, "Test");
fclose($myfile);
But whenever this php file is called I get "Unable to write file". Does anybody know what the issue is and how to fix it? Thanks
In the Linux server, it may be a file permission issue try this command below in your directory. I am assuming your directory is /var/www/html if not replace this with your directory path and run commands.
chown -R www-data:www-data /var/www/html
chmod -R 755 /var/www/html
Hi i need help writing data to a file in php via web, for example i use http://1.1.1.1/file.php?&file=lol.txt and it doesn't write but when i take out $_GET and just put a random file name and run through the terminal it works, any ideas on why it wont work via web?
I've tried switching to file_put_contents() but it did the same thing, didn't work via web but worked in terminal.
my code below
<?php
$file = $_GET['file'];
$file_name = "/var/www/html/$file";
$myfile = fopen($file_name, "a+") or $myfile = fopen($file_name, "r");
chmod($file_name, 0777);
$text = "hello there\n";
fwrite($myfile, $text);
fclose($myfile);
?>
it doesn't show any error messages i just wanted to know if i was missing anything or?
Change folder premissions:
// Folder owner: apache (Centos) or www-data (Debian)
// owner:group
chown -R apache:yourusername /var/www/html
// Folder permissions 775 or 777 for all
chmod -R 775 /var/www/html
Btw. secure file name first and test file extension:
$file_name = "/var/www/html/".basename($file);
I'm running the following PHP in a virtual server on my mac:
$dir = './setup/';
$files = glob($dir.'*',GLOB_MARK);
foreach($files as $file) { unlink($file); }
rmdir($dir);
It's meant to delete all files in a directory called setup then delete the setup directory.
It deletes the files fine, but then throws the following error:
Warning: rmdir(./setup/) [function.rmdir]: Permission denied
I have read and write permissions set on the setup directory for everyone. What else can I do to get this to work?
I think this will do the trick:
Open Terminal on your Mac
Type "chmod 777 [path]" and press Enter. Change [path] to the location of the folder you are trying to delete, for example /var/www/folder, like "chmod 777 /var/www/folder"
Now you should be able to delete the folder.
Hi I have created a php that creates a file in php using fopen it works fine by itself but when I access it through another php script it fails to create the file.
I tried sudo chown -R root /directory on the directory but it doesn't work and sudo chcon -R -t httpd_sys_script_rw_t /directory, but still to no avail.
<?php
$my_file = '../file.js';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
//check if the file exists
if (file_exists('../file.js')){
echo "file exists <br />";
}
?>
when I run my page on my local host it does not create the file. I have given read and write permissions to the folder site. i have used system and exec functions and when i run the page via the terminal it creates a file but via my browser it doesnt.
$my_file = '/var/www/site/file.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
The following commands will correctly apply permissions to the directory.
cd /var/www/site
sudo chmod 664 *
sudo find . -type d -exec chmod 755 {} \;
If the error persists, please post any PHP errors that are thrown.
Did you try with a relative adresse?
$my_file = 'site/file.txt';