Can't write file with PHP on Linux Server - php

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

Related

Writing string to a file in php via web

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

Make a copy of a File with PHP or JavaScript

How can I make a copy of a file on my server from a site either in PHP or JavaScript?
I tried the following:
<?php
$file = 'example.txt';
$newfile = 'example.txt.bak';
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
?>
But I keep getting the "failed to copy" message
I created a copy.php file in my var/www/html folder (Using Apache in Ubuntu) and I ran the copy.php file in my browser http://localhost/copy.php
My example.txt is sitting right in the same folder that copy.php
Thanks
You need to set destination folder permissions to 646. Use your domain cpanel or equivalent to do that
In ubuntu you would need to set your folder permissions so navigate to your project directory using cd in the command prompt 'cd /var/www/html/myproject'
var/www/html/myproject
Lets say the folder your want to copy to is located at
var/www/html/myproject/copied_files
now run the command 'sudo chown -R <your username on ubuntu>:www-data <folder name>
It should look like this
Lets say my user name is prince
prince#computer: var/www/html/myproject $ sudo chown -R prince:www-data copied_files
Essentially your giving the group "www-data" which is your server permission to write files to that directory while you still have ownership to it. The '-R' says that all subfolders should be writable by 'www-data' (your server) so you don't have to manually make it writeable and the 'copied_files' is the name of directory for your server to write too :)

PHP - Unable to create file

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.

php writting to a file from a website not working

Im trying to write to file using php but it doesn't see to work.
$file = fopen("test.txt","w");
fwrite($file,"Hello World!");
fclose("$file");
I published the site using the school server. and the php file/text file are all in public_html folder. The problem is, running this into terminal writes the file. Running it as a website doesn't.
How do i fix this?
P.S. I've tried chmod 644, 777, 755
Remove the quotes in fclose :
$file = fopen("test.txt","w");
fwrite($file,"Hello World!");
fclose($file);
Also, you have to grant all permission : see that answer.
Hmm... is it a UNIX/Linux server?
You probably want to give the group www-data permissions to write the folder.
Assuming your web folder is /var/www, try doing this:
sudo chown `whoami`:www-data /var/www
sudo chmod 775 /var/www
And try again.
If you're working in your personal web folder (http://domain.ext/~yourusername), the commands should be:
chown `whoami`:www-data ~/public_html
chmod 775 ~/public_html
Does it work running the script now?

Not creating file via my browser

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

Categories