php fopen function not working through linked script - php

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

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

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

grant permission to execute .c file

i created a file by the following code
$dir = '/home/srikanth/Desktop';
if ( !file_exists($dir) ) {
mkdir ($dir, 0777);
}
//$code contains the code C code which i want to execute
file_put_contents ($dir.'/test.c', $code);
$path = "/home/srikanth/Desktop/test.c";
$Command = "gcc $path 2&>1";
exec($Command,$return_val,$error);
when i open my error log file
i see the following error
sh: 1: cannot create 1: Permission denied
gcc: error: 2: No such file or directory
i tried the following commands to change the permission ,I am currently using Ubuntu 14.04.2 and Apache server
chmod 0777 /home/srikanth/Desktop/test.c
sudo chmod -R 777 /home/srikanth/Desktop/test.c
sudo usermod -aG www-data root
addgroup www-data
I used these commands to add www-data to root group
but i still keep on getting the same file in my error log
As mentioned in the comments, this issue is a permissions issue, in so much as the server user can not create folders/files in the /home/srinkanth/Desktop directory. Or something in the gcc compiler can not create files during compilation of your c code.
Adding 777 to that DIRECTORY (not the test file on its own) will likely fix the problem. However, good practice is to add exits/exceptions in your code to give you more visibility on problems if and when they happen and what caused them.
Here is my advice:
$dir = '/home/srikanth/Desktop';
if (!is_dir($dir)) {
$mkDir = mkdir($dir, 0777);
if (!$mkDir) {
exit('Failed to create ' . $dir);
}
}
Then you can add additional de-bugging info to your put file code:
$codePath = $dir.'/test.c';
$created = file_put_contents($codePath, $code);
if (!$created) {
exit('Failed to create ' . $codePath);
}
The if you reach this stage more to your execute file code:
$codePath = $dir . '/test.c';
$command = "gcc $codePath 2&>1";
$output = [];
$returnStatus = '';
$lastLine = exec($command, $output, $returnStatus);
Then you can output your whole gcc compile by glueing the output array pieces back together with a new line as exec parses each line of output as a record in the output array:
echo implode("\n", $output);
Hope this helps.
Goodluck.

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.

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