LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 07-08-2017, 08:11 PM   #1
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Rep: Reputation: 12
mkdir() will not make the directory


Don't understand why mkdir() will not produce the new directory in Desktop. All permissions on php script are set to 777. Desktop permissions are set to 777 to test this.

One attachment shows the output from Firefox browser. The other shows the output from the command line in a terminal.



Code:
<?php
error_reporting (E_ALL);
error_reporting(-1);

$user=get_current_user();
$desktop="/home/$user/Desktop/photos";

if(isset($_GET['choice'])) {
	$choice=$_GET['choice'];
}
else {
	$choice="/media/rick/CANON_DC/102CANON";
	$cflag=true;
}

$endDir=basename($choice);
$processDate=date("YMd-G:i");
//$transDir=trim($desktop."/".$processDate."TransFile_".$endDir);
 $transDir=trim($desktop."/TransFile_"); //.$processDate."/".$endDir);


$transDir="/home/rick/Desktop/someNewDir";

mkdir($transDir);

chdir($transDir);

$src=getcwd();

if($cflag) { // command line execution
	echo  "\nOutput from Command: php -f trantest.php - \n\nCurrent Dir: ".$src."\n\nTransfer Dir: ".$transDir."\n\n";
}
else {   // browser execution 
	echo  "\nCurrent Dir: ".$src."\n\nTransfer Dir: ".$transDir."\n\n";
}
	
/*  THE REST OF THIS FUNCTION COPIES ALL PHOTO IMAGE FILES TO THE TRANSFER DIR TO BE PROCESSED ELSEWARE

$cmd= "rsync -avz --dry-run ".$src."  ".$transDir;  // Dry Run -From Photo Source to Transfer Directory 
//$cmd= "rsync -avz ".$src."  ".$transDir;  // From Photo Source to Transfer Directory 
 
echo shell_exec($cmd);  // Store Photos in Transfer Directory on Desktop

return $transDir;
*/


?>
Attached Thumbnails
Click image for larger version

Name:	Browser-Firefox.png
Views:	38
Size:	16.2 KB
ID:	25417   Click image for larger version

Name:	CommandOutput.png
Views:	34
Size:	30.6 KB
ID:	25418  
 
Old 07-08-2017, 09:43 PM   #2
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
You are trying to create a sub-directory without creating its parent. IOW you need the equivalent of "mkdir -p".

See the recursive flag: http://php.net/manual/en/function.mkdir.php
 
Old 07-09-2017, 09:23 AM   #3
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Quote:
Originally Posted by norobro View Post
You are trying to create a sub-directory without creating its parent. IOW you need the equivalent of "mkdir -p".

See the recursive flag: http://php.net/manual/en/function.mkdir.php
That sounds like it is the problem.
 
Old 07-09-2017, 12:20 PM   #4
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Thanks for your replys.

Using Firefox, I now have a directory created. However it has the Owner/Group set as www-data and Permissions set to 0744. I had set mode to 0766 but that was ignored.

When run from the Command line in a Terminal this doesn't happen. Owner/Group is set to $user:$user and Permissions were set to 0764, however,as above I had set mode to 0766 and this was ignored.

chown() doesn't work so I can't change the Owner back to $user:$user.

How do I fix this???

R
 
Old 07-09-2017, 01:43 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Fix what? Please be more precise.
One thing is a given: you cannot change the owner of a file/directory, if you aren't the root-user.
 
1 members found this post helpful.
Old 07-09-2017, 03:03 PM   #6
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
How do you prevent mkdir() from changing the the Owner/Group to www-data?
 
Old 07-09-2017, 03:18 PM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by pizzipie View Post
Thanks for your replys.

Using Firefox, I now have a directory created. However it has the Owner/Group set as www-data and Permissions set to 0744. I had set mode to 0766 but that was ignored.

When run from the Command line in a Terminal this doesn't happen. Owner/Group is set to $user:$user and Permissions were set to 0764, however,as above I had set mode to 0766 and this was ignored.

chown() doesn't work so I can't change the Owner back to $user:$user.

How do I fix this???

R
You need to realize that when "using Firefox" you are actually using your web server and running as the web server user, www-data in your case. The web server and the PHP it processes are running in a totally different context than when run as a system user, rick or root, in the shell.

The www-data user should not have access permission to system user directories and sane defaults and built-in limits prevent it from doing known, potentially harmful things such as creating directories and files where it ought not do so.

Trying to write a PHP script to work the same when run in a shell by a system user, and when run in web server context is problematic at best and probably not what you want to do.

If you really want your user rick to be able to do these things in his home directory using PHP running under a web server, then add him to the web server group (www-data) and create a virtual host with a server root directory under /home/rick owned by rick:www-data. Then the web server will be able to write and make directories there and rick can run from a shell there.

Additionally, never, ever, do this...

Quote:
All permissions on php script are set to 777. Desktop permissions are set to 777 to test this.
...you only introduce additional confusion at best, and trigger other problems.

Why would you even try that as a troubleshooting method (rhetorical question)? Because you do not yet understand the how and why of those permissions and you think, mostly incorrectly, that 777 will mask permission problems temporarily, "to test this".

Realize that doing this is a shot in the dark with a loaded weapon and _always_ creates additional problems! Learn not to do that!

The next time the you think, "I'll set perms to 777...", pause and try to explain to yourself what you expect that to accomplish. When you realize that you probably can't explain it to yourself, refrain from doing so and try to better understand what is actually happening.

For example, in this case the answer would have been, "Because the script in Firefox cannot create the directory. Why is that?". And the answer would have been, "Because when 'running in Firefox' I am running as the web server user, www-data, which does not have access to rick's home directory...". Which would lead to a solution, not another problem wearing a mask.

So I would suggest that you divide the problem into its natural parts:

1. How to manage these images from a shell, maybe using PHP as the scripting language.
2. How to write a web server application in PHP which does the same tasks (code).
3. How to configure a web server virtual host to support this application (paths, ownerships, permissions).
4. How much code can be shared between shell and web server application, and what parts should be separate?

The method and understanding the various aspects of the problems will lead to a solution.

Last edited by astrogeek; 07-09-2017 at 03:25 PM. Reason: typos, grammar
 
1 members found this post helpful.
Old 07-09-2017, 03:26 PM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> How do you prevent mkdir() from changing the the Owner/Group to www-data?

mkdir cannot do such thing. If your scripts runs as www-data user, the new directory's owner will be www-data, too.
 
1 members found this post helpful.
Old 07-10-2017, 09:20 PM   #9
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Thanks astrogeek,

You are pointing me in the right direction. I am beginning to understand this problem. The only reason for me to enable this program in terminal and browser is for testing purposes to see what is going on immediately.

My goal is to blend in the choice possibilities ( radio buttons, checkboxes, etc of html) with AJAX calls to php scripts to do the server side of creating the directories, to rename the files and to transfer the files.

I'll keep at it.

R
 
  


Reply



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
[SOLVED] mkdir: cannot creat directory. How to enable mkdir for user in usb dir? Foxbat1155 Linux - Newbie 13 02-16-2012 11:20 AM
mkdir fails ( even with -p ): No such file or directory axisofevil Linux - Software 2 06-01-2010 11:11 AM
Mounted NFS directory can't mkdir rcmonroig Linux - Newbie 5 11-04-2009 10:57 PM
Trying to make a directory with mkdir using the date command. Altheastronut Linux - General 5 06-09-2009 12:14 PM
mkdir : cannot create directory : no such file or directory patcheezy Linux - Newbie 6 05-13-2009 11:26 AM

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

All times are GMT -5. The time now is 02:56 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