LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-25-2022, 03:50 PM   #1
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 410

Rep: Reputation: 10
file creation user and permission problems - file created can't be accessed


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

/* This works on Command Line but not in Browser */

if(!is_dir($dir)) {
	mkdir($dir);
	exec("chmod 766 $dir"); 
	exec("chown -R rick $dir");
	 }
	 
$filename=$dir.$csvExt;
echo $filename."\n\n";
bye(25);
 
Old 12-26-2022, 12:55 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,428
Blog Entries: 1

Rep: Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679
You cannot change the owner of a file/directory if you are not root-user. But you can change the group of the user/directory.
For example:
Code:
mkdir($dir);
chmod($dir, 0775);
chgrp($dir, 'www-data');
Edit: chgrp and chmod won't work either if the directory already exists and the user running this script is not the owner of the directory (or root-user).

Last edited by NevemTeve; 12-27-2022 at 02:01 AM.
 
Old 12-26-2022, 03:57 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,968

Rep: Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177
I was thinking along a similar vein from the point of view that the directory has not changed between the comnmand line version and the browser version, do you not simply need to make sure the group has the correct permissions?
 
1 members found this post helpful.
Old 01-04-2023, 09:47 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,414

Rep: Reputation: 418Reputation: 418Reputation: 418Reputation: 418Reputation: 418
Hi

By default, only the creator of files/directories can change. And you need to be root or use sudo for chwon. You cannot do it as www-data.

A simple solution to this is to use umask. You can put this before the mkdir in the PHP script:

Code:
umask(0);
Now everyone can change it. It also applies to files written after this command.

From the command line, this command does the same:

Code:
umask 0
Now www-data has full acceess.
 
Old 01-04-2023, 10:09 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,428
Blog Entries: 1

Rep: Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679
> Now everyone can change it.

Can change what? Guess you mean create and remove files in the directory.

Last edited by NevemTeve; 01-04-2023 at 10:55 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Number of times a particular file is accessed in the system since it was created rahil khera Linux - Newbie 5 03-03-2014 07:18 PM
.bash_profile file not created after creation of user. manoj.linux Linux - Enterprise 3 08-01-2013 10:38 AM
/dev/dsp can't be accessed (permission denied) KDE tells me wilsonsamm Linux - Desktop 3 04-08-2008 09:45 AM
System user creation with mysql user creation moos3 Programming 1 08-11-2007 09:01 PM
file created/accessed dates plainkeyman Linux - General 4 10-27-2005 08:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration