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

Links for php scripts

Moderators: gesf, Michalio

Post Reply
pizzipie1
New php-forum User
New php-forum User
Posts: 3
Joined: Wed Nov 23, 2022 10:25 am

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: Select all

$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);
User avatar
Michalio
Moderator
Moderator
Posts: 361
Joined: Sun Jul 18, 2021 1:33 pm
Location: Poland

Did you tried the chmod, chown and chgrp functions from php?
Free coding lessons: https://php-forum.com/phpforum/viewtopic.php?t=29852
pizzipie1
New php-forum User
New php-forum User
Posts: 3
Joined: Wed Nov 23, 2022 10:25 am

With respect, please read my post again.

Those statements will not work in the BROWSER.

Thanks R.
User avatar
Michalio
Moderator
Moderator
Posts: 361
Joined: Sun Jul 18, 2021 1:33 pm
Location: Poland

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: Select all

sudo groupadd csv-editors
Then add your and www-account to the new group:

Code: Select all

usermod -a -G csv-editors rick
usermod -a -G csv-editors www-data
Then while creating the directory or file use chgrp function:

Code: Select all

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.
Free coding lessons: https://php-forum.com/phpforum/viewtopic.php?t=29852
pizzipie1
New php-forum User
New php-forum User
Posts: 3
Joined: Wed Nov 23, 2022 10:25 am

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: Select all

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: Select all

#!/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
webdeveloper90
New php-forum User
New php-forum User
Posts: 1
Joined: Wed Jan 25, 2023 10:11 am

Michalio wrote: Mon Dec 26, 2022 3:16 am 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: Select all

sudo groupadd csv-editors
Then add your and www-account to the new group:

Code: Select all

usermod -a -G csv-editors rick
usermod -a -G csv-editors www-data
Then while creating the directory or file use chgrp function:

Code: Select all

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.
Post Reply