Write into ubuntu folder with debian apache - php

I have a debian 9 running apache2 with php7.
I have an ubuntu which has a shared folder (samba).
I have used fstab on debian 9 to attack the shared folder.
I can create folder with php and create file, but I cant write into the file?! What can be the problem?
$myfile = fopen("./mntdp/dp_handler/newfile2.txt", "w") or die("<br>Unable to open file!");
$txt = "Mickey Mouse\n";
fwrite($myfile, $txt);
$txt = "Minnie Mouse\n";
fwrite($myfile, $txt);
fclose($myfile);
Warning: fopen(./mntdp/dp_handler/newfile2.txt): failed to open stream: Permission denied in /var/www/html/dir.php on line 10
So the newfile2.txt is created on the ubuntu share, but I cant write in it?! Why?
I used chmod -R 777 on dp_handler the owner is the user what I use in the fstab, so the admwz user is the owner and the group is root.
I have tried so many stuff can somebody help?!

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

PHP script only executing from command line when command is run from script's directory

I am trying to run this script from the command line:
#!/usr/local/bin/php
<?php
$myfile = fopen("cc.log", "a") or die("Unable to open file!");
$txt = "Success \n";
fwrite($myfile, $txt);
fclose($myfile);
?>
And it works, but only when I cd into the directory of the script and run:
/usr/local/bin/php test_script.php
This also works:
/usr/local/bin/php /home/<username>/public_html/test_script.php
but not when I leave the directory.
Solved by changing line 3 of test_script.php from:
$myfile = fopen("cc.log", "a") or die("Unable to open file!");
to an absolute path:
$myfile = fopen("/home/<username>/public_html/cc.log", "a") or die("Unable to open file!");
The original script created a cc.log file in the directory from where the command was called and appended about 50 "Success" messages xD
A more formal answer to the original question:
Your file (test_script.php) needs to be made executable to be able to be run from anywhere. This can be done with chmod:
chmod 755 test_script.php
and results in the file being able to be run by invoking.
./test_script.php
And now, drumroll please, you should be able to run the file by invoking it like this:
./path/to/test_script.php
That dot at the front of the path should allow you to run the script as an executable without doing anything else.
Hope it helps/works!
(If it doesn't work comment and I'll boot up my Linux machine and try to replicate it!)
EDIT
Ok, that's weird that it doesn't work! Shoot! Is the PHP CLI (command line interface) installed on your Linux server? If so I would encourage you to try:
php /path/to/your/test_script.php
Hope that second suggestion helps!
EDIT 2
Aha moment number 2!!
I believe your php should be installed here: /usr/bin/php, having it in the local folder will not allow you to properly invoke it from another directory!
To download yourself a new installation of php, run
sudo apt-get install php5 libapache2-mod-php5
You will also have to change your script header to #!/usr/bin/php.
This was a good source for this third edit! I really hope it helps!

PHP5 - simple write to file 500 error with cgi-bin. Code validated and .PHP file set to 755

I am running a server on Debian, using php-5
My php file is called pwrite.php, has 755 rights and is located at:
/usr/lib/cgi-bin
<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
?>
I am expecting it to write 'newfile.txt' in cgi-bin folder
Using the error logs for apache2 at
tail -f /var/log/apache2/error.log
I see 2 errors;
[[cgid:error] [pid 1328:tid 1996410880] (8)Exec format error: AH01241: exec of '/usr/lib/cgi-bin/phptest.php' failed
[[cgid:error] [pid 1268:tid 1963979824] [client ::1:35573] End of script output before headers: phptest.php
Both of these errors only happen when I try and execute code on my server using CGI.
I have a working bash.sh script in the CGI folder that displays system information.
Didn't find a lot of info on 500 errors, except they suck. I did see this link and wonder if using cgi-bin is the issue here?
https://answers.stanford.edu/solution/error-cgi-exec-format-error-when-trying-access-data-file-my-script-created
I ultimately want my PHP to resolve the URL requested (site.com/ar1/arg2) and parse this into two variables in order to write pages dynamically (/var/www/html/ar1/arg2/result.json)
I'm sure that my php file has the correct permissions, and I'm pretty sure I've enabled .PHP files in my default cgi-bin folder to run without additional parsing.

PHP: fopen - Permission denied - chmod 777 already done

I am setting up a new CentOS 6.6 machine running PHP 5.5 and migrating our site to the server but am getting the error in my httpd error log:
PHP Warning: fopen(/ssd-media/www/app/storage/logs/laravel.log): failed to open stream: Permission denied in /ssd-media/www/index.php on line
I have already checked and the httpd process is being run as the user/group "apache" and have done the chmod -R 775 storage and verified that the file has the right user / group configured and permissions and then ran the following code:
<?php
$myfile = fopen("/ssd-media/www/app/storage/logs/laravel.log", "w") or die("Unable to open file!");
$txt = "test\n";
fwrite($myfile, $txt);
fclose($myfile);
?>
I have moved the DocumentRoot to /ssd-media/www which is not where CentOS is installed, it is an SSD RAID that is only for the apache / mysql files. Other than that it is a fairly simple configuration for which I followed this guide:
https://webtatic.com/packages/php55/
What other reasons would I get a permissions denied on that file? I tried 777 permissions as well to no avail.

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.

Categories