I got an issue with my centos server, It, from 3 days ago won't allow me to edit files (write into .txt) create files or create directories with mkdir..
I checked the file ownage, runned whoami and checked the php file had the right permissions but it doesn't want to work.
Might someone suggest me troubleshootings? Also I tried switching from nginx to httpd and repeat the permission checks but still nothing.
running ls -l on the php script it outputs the following:
-rwxrwxr--. 1 nginx nginx 63 Dec 21 14:31 cartella.php
and after : chown apache:apache -R /usr/share/nginx/html/cartella.php
-rwxrwxr--. 1 apache apache 63 Dec 21 14:31 cartella.php
also permissions I tried setting them to 644/755/777 but neither one worked.
Php code that worked till a few days ago:
exec(" > test.txt"); //to empty the file and set the right permissions
$myfile = fopen("test.txt", "a") or die("Unable to open file!");
fwrite($myfile, $text);
fclose($myfile);
Related
I'm new to PHP and just faced the problem with creating and opening a file with fopen(). Here is my code:
<?php
$new_file = fopen('file.txt', 'w') or die("Cannot create a file");
$text = <<<_END
Line 1
Line 2
Line 3
_END;
fwrite($new_file, $text) or die('Cannot write to the file');
fclose($new_file);
When I try to run the file by opening it in the browser I see the next message: 'Cannot create a file'. But when I start debug session everithing works as it supposed to. I suspect that there is some issue with permissions and XDebug uses root access unlike the usual interpreter?
to write and read permission
chown -R www-data:www-data /var/www/dicrctoy
chmod -R 775 /var/www/dicrctoy
I have tested in my local environment no error was found
if you're using mac you should check the file permission
use chmod command to change permission
What is the meaning of chmod 777?
readable, writable and executable
Setting 777 permissions to a file or directory means that it will be readable, writable and executable by all users
To solve this problem first of all I was needed to check PHP user with the next command:
<?php echo `whoami`; ?>
It outputs:
www-data
This is the default PHP user. Next I checked the owner of the folder with this command:
ls -dl /var/www/html/test
It outputs:
drwxrwxr-x 2 username username 4096 Jun 26 12:49
Next I've sat permissions to the PHP user by running:
sudo chown -R www-data /var/www/html/test
Checking once again if the owner changed
ls -dl /var/www/html/test
And now it outputs
drwxrwxr-x 2 www-data username 4096 Jun 26 12:55
Done. Now I'm able to create and write to the file.
I am trying a POC running a python script in a back-end implemented in PHP. The web server is Apache in a Docker container.
This is the PHP code:
$command = escapeshellcmd('/usr/local/test/script.py');
$output = shell_exec($command);
echo $output;
When I execute the python script using the back-end we are getting a permission denied error for creating the file.
My python script:
#!/usr/bin/env python
file = open("/tmp/testfile.txt","w+")
file.write("Hello World")
file.close()
This is the error I'm getting:
IOError: [Errno 13] Permission denied: 'testfile.txt'
For the directory im working with the permissions are as follows,
drwxrwsr-x 2 1001 www-data 4096 May 8 05:35 .
drwxrwxr-x 3 1001 1001 4096 May 3 08:49 ..
Any thoughts on this? How do I overcome this problem?
To start is is incredibly bad practice to have relative paths in any scripting environment. Start by rewriting your code to use a full path such as /usr/local/test/script.py and /tmp/testfile.txt. My guess is your script is attempting to write to a different spot than you think it is.
When you know exactly where the files are being written go to the directory and run ls -la and check the permissions on the directory. You want it to be writeable by the same user or group as the web server runs.
Looking at the permissions you have shown you don't have the user able to write to the directory, just everyone and the group. You need to add user write permissions - chmod u+w /tmp will do the job.
I believe the problem is that you are trying to write to an existing file in the /tmp/ directory. Typically /tmp/ will have the sticky permission bit set. That means that only the owner of a file has permission to write or delete it. Group write permissions on files do not matter if the sticky bit is set on the parent directory.
So if this is the contents of your /tmp
$ ls -al /tmp
drwxrwxrwt 5 root root 760 Apr 30 12:00 .
drwxr-xr-x 21 root root 4096 Apr 30 12:00 ..
-rw-rw---- 2 1001 www-data 80 May 8 12:00 testfile.txt
We might assume that users in the group www-data should be able to write to testfile.txt. But that is not the case, since . (the /tmp/ directory itself) has the sticky bit set (the t in the permissions section indicates this).
The reason why the sticky bit is set here is that everyone should be able to write files there, but not have to worry that other users might modify our temporary files.
To avoid permission errors, you can use the standard library tempfile module. This code will create a unique filename such as testfile.JCDxK2.txt, so it doesn't matter if testfile.txt already exists.
#!/usr/bin/env python
import tempfile
with tempfile.NamedTemporaryFile(
mode='w',
prefix='testfile.',
suffix='.txt',
delete=False,
) as file:
file.write("Hello World")
I'm seeing weird Ubuntu permission issues only on a single .csv file when trying to access with php. The file is list.csv, owned by ubuntu:www-data, and ubuntu user is part of www-data group. The rest of the site works fine, no permission issues, but I see the following error when trying to load this file with php:
PHP Fatal error: Uncaught exception 'RuntimeException' with message 'SplFileObject::__construct(xxx/list.csv): failed to open stream: Permission denied'
If i do chown www-data:www-data list.csv, i can load with php. Why is this file not being loaded by php if owned by ubuntu, even if ubuntu is part of www-data group?
Additionally, I can open this file as ubuntu user (vi list.csv) read and write.
Edit: Permissions on the file are: -rwxr-xr-x 1 ubuntu www-data
Edit again: Changed permissions to 0644 -rw-r--r-- 1 ubuntu www-data no luck
Edit some more: File lives in a directory that has x permission:
drwxr-xr-x 3 ubuntu www-data 4096 Jul 28 23:09 content/
Not sure it has anything to do with execute permissions as I can change owner to www-data:www-data and the file gets loaded. It would seem its something to do with user ubuntu owning the file and not www-data
Edit: Its this line of code that errors out, trying to load .csv file into php SplFileObject:
SplFileObject::__construct("xxx/content/list.csv");
I can see in the OP that the only difference between the owner and group is write permission (you have tried xr and r with the group). So, I suspect your PHP is trying to open the file in write mode.
To confirm this try:
$myCSV = new SplFileInfo('YOUR CSV');
$myCSV->isReadable(); // should be true
$myCSV->isWritable(); //expecting this to be false
Edit: If that's whats going with your program you can open the file just in read mode using: $myCSV->openFile('r')
Your server might have the PHP open_basedir parameter set which would prevent access to any files outside of this directory.
To verify this have a look into your relevant php.ini file (for example /etc/php5/apache2/php.ini) and check if the open_basedir line is set.
I'm running XAMPP 1.8.2 with the default PHP 5.4.19 on OS X 10.8.5.
My PHP application is supposed to have read/write access to a certain local directory, but it's not working. For testing, I distilled it down to a very simple script:
<?php
$file = "/Volumes/RAID/AT_RISK/copra/uploadedfiles/file.txt";
$current = "hello world\n";
file_put_contents($file, $current);
?>
Warning: file_put_contents(/Volumes/RAID/AT_RISK/copra/uploadedfiles/file.txt): failed to open stream: Permission denied in /Applications/XAMPP-1.8.2/xamppfiles/htdocs/filetest.php on line 4
I've tried this command, but it doesn't help.
sudo chmod -R 777 /Volumes/RAID/AT_RISK/copra/
Here you can see the permissions for yourself:
$ pwd
/Volumes/RAID/AT_RISK/copra/uploadedfiles
$ ls -l
total 32
-rwxrwxrwx 1 elliott staff 7 Oct 12 22:31 file.txt
Silly me. Although the folder had correct permissions, the volume itself (/Volumes/RAID) was 700. Problem solved.
So, I have this error:
Warning: fopen(/path/to/test-in.txt) [function.fopen]: failed to open stream: Permission denied
Performing ls -l in the directory where test-in.txt is produces the following output:
-rw-r--r-- 1 $USER $USER 1921 Sep 6 20:09 test-in.txt
-rw-r--r-- 1 $USER $USER 0 Sep 6 20:08 test-out.txt
In order to get past this, I decided to perform the following:
chgrp -R www-data /path/to/php/webroot
And then did:
chmod g+rw /path/to/php/webroot
Yet, I still get this error when I run my php5 script to open the file. Why is this happening? I've tried this using LAMP as well as cherokee through CGI, so it can't be this.
Is there a solution of some sort?
Edit
I'll also add that I'm just developing via localhost right now.
Update - PHP fopen() line
$fullpath = $this->fileRoot . $this->fileInData['fileName'];
$file_ptr = fopen( $fullpath, 'r+' );
I should also mention I'd like to stick with Cherokee if possible. What's this deal about setting file permissions for Apache/Cherokee?
Check if the user that PHP runs under have "X" permission on every directory of the file path.
It will need it to access the file
If your file is: /path/to/test-in.txt
You should have X permission on:
/path
/path/to
and read permission on /path/to/test-in.txt
Another reason of this error may be that the directory of file does not exist.
I just add php code before fopen:
if(!file_exists(dirname($file)))
mkdir(dirname($file));
This help me out.