Writing string to a file in php via web - php

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

Related

Can't write file with PHP on Linux Server

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

Not able to write php file in php

I am able to write .txt file with the following code but unable to do so with php files(existing or newly created with the following code) on localhost.
Also it echo string length correctly without any error notice or warning
<?php
$file = fopen("test.php","w");
echo fwrite($file,"do not vote me down");
fclose($file);
?>
Thanks in advance
may be a ownership problem. try do chmod -R 775 /var/www/folder and/or chown -R domain:www-data /var/www/folder - where domain is the user of that particular virtualhost or www-data
Also to update a file instead rewrite you can use append option - adding + will help to create the file first time if it is not exists.
$file = fopen("test.php","a+");
echo fwrite($file,"do not vote me down ab");
fclose($file);
And If you want to change the ownership of the file you can use Php chown function-
chown('test.php','apache');
Too change permission of file or folder recusively use this code -
exec ("find /path/to/folder -type d -exec chmod 0750 {} +");
exec ("find /path/to/folder -type f -exec chmod 0644 {} +");
Maybe your file will not open so you can add die() function after fopen(), review below code :-
<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "Mickey Mouse\n";
fwrite($myfile, $txt);
$txt = "Minnie Mouse\n";
fwrite($myfile, $txt);
fclose($myfile);
?>
Your code works. I can rewrite the content, as long as test.php has appropriate file permission.

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 fopen function not working through linked script

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 />";
}
?>

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