file creation user and permission problems - file created can't be accessed

pizzipie1

New member
I have a script which creates a Directory to contain CSV files from a MySql database and the related .csv data files. I want to access the directory and use the .csv files to write reports with LibreOffice. If I run the following code snippet below , contained in the script, FROM THE COMMAND LINE the user:group is rick:www-data and all is ok as I can now run the new .CSV file in LibreOffice.

If I run the following code snippet below, contained in the script, FROM THE BROWSER the user:group is www-date:www-data and all is NOT ok as I CANNOT now run the new .CSV file in LibreOffice.

How can I change the owner:group of the Directory to rick:www-data.

Apparently the command line uses HOST owner while browser uses PHP owner ???

Please help with a simplistic answer. I have been two days looking at many ways to manipulate permissions, most of which is overly complicated(to me).

Code:
$dir="/home/rick/DBases/Dbmysql/contacts2/reports/";

if(!is_dir($dir)) {
	mkdir($dir);
	exec("chmod 766 $dir"); 
	exec("chown -R rick $dir");
	 }
	 
$filename=$dir.$csvExt;
echo $filename."\n\n";
bye(25);
 
First of all, how offen you create that directory? You are trying to change these permissions only while directory is created. Second thing is that you are using the exec command instead of php functions that are works better with php.
Then whenever you create a directory or csv file you can set different permission.
Create a new group:
Code:
sudo groupadd csv-editors
Then add your and www-account to the new group:
Code:
usermod -a -G csv-editors rick
usermod -a -G csv-editors www-data

Then while creating the directory or file use chgrp function:
Code:
chgrp($path, 'cvs-editors');
chmod($path, 0774);
Then the autor of the file and each users from new groups are fully permissions to the files and other users can only read the file.
 
Well, first thank you for your reply. Second Happy new Year.

First of all, how often you create that directory?

You are correct, Not very often. However, this has gotten to be an academic challenge to me at this point. I tried using your creating new group solution and managed to get everything messed up. I ended up with user:group for rick and www-data like:
Code:
rick:x:1000:1001:rick,,,:/home/rick:/bin/bash
www-data:x:33:1001:www-data:/var/www:/usr/sbin/nologin
The 1001 that shows is left over from the 'groupdel ' I did. How do you get rid of the 1001. I t doesn't seem to do anything, it just annoys me!

Anyway, I found this solution.

The working directory is /home/rick/DBases/Dbmysql/contacts2. All files have rick:www-data user:group.
Starting with no 'reports' directory I create the 'csvData_xxx' file which is created with 'www-data:www-data' user:group and stored in working directory.

Then I run this bash script - sudo ./CSV_perm.sh.

Code:
#!/bin/bash

mkdir -p -v -m 0764 reports

    chown -R rick reports
    
    chown -R rick csvData*
    chmod -R 0764 csvData*
    
    mv csvData* reports

This creates the 'reports' directory, if it doesn't exist, then changes the 'reports' directory ownership and assigns permissions to the 'reports' directory. Then the same with csvData* file(s) and finally moves the csvData* files to the 'reports' directory. This works very well except for having to run the script file as a separate operation.
I STILL DO NOT UNDERSTAND WHY THIS CAN'T BE DONE IN PHP ALONE!!!

Thanks for helping,

R
 
Michalio said:
First of all, how offen you create that directory? You are trying to change these permissions only while directory is created. Second thing is that you are using the exec command instead of php functions that are works better with PHP. I recently built the website https://pokieslab.com/real-money-pokies/ and I'm having the same problem.
Then whenever you create a directory or csv file you can set different permission.
Create a new group:
Code:
sudo groupadd csv-editors
Then add your and www-account to the new group:
Code:
usermod -a -G csv-editors rick
usermod -a -G csv-editors www-data

Then while creating the directory or file use chgrp function:
Code:
chgrp($path, 'cvs-editors');
chmod($path, 0774);
Then the autor of the file and each users from new groups are fully permissions to the files and other users can only read the file.

Check your DNS settings and make sure they are correct.
Try using a different internet connection.
Make sure you have the latest version of Java installed.
Try disabling any proxy settings you may have enabled.
Check your router settings and make sure they are not blocking the website.
Try using a different device to access the website.
Make sure you have the latest version of your operating system installed.
Try disabling any parental control settings you may have enabled.
Check your modem settings and make sure they are not blocking the website.
 
To change the owner and group of a directory using PHP, you can make use of the `chown` and `chgrp` functions. Here's how you can modify your code to change the owner to "rick" and the group to "www-data":

```php
$dir = "/home/rick/DBases/Dbmysql/contacts2/reports/";

if (!is_dir($dir)) {
mkdir($dir);
chmod($dir, 0766);
chown($dir, "rick");
chgrp($dir, "www-data");
}

$filename = $dir . $csvExt;
echo $filename . "\n\n";
bye(25);
```

In the updated code, `chown($dir, "rick");` changes the owner of the directory to "rick", and `chgrp($dir, "www-data");` changes the group to "www-data".

Make sure that the user executing the PHP script has sufficient permissions to change the ownership and group of the directory. If the PHP process is running as a user with limited permissions, you may need to use `sudo` or consult with your server administrator to grant the necessary permissions.

Additionally, ensure that the directory path is correct and that the user running the PHP process has access to the directory and its parent directories.
 
Back
Top