Page 1 of 1
mkdir() will not make the directory
Posted: Fri Jul 07, 2017 9:19 pm
by pizzipie
Hi,
I am running an .html program which uses AJAX to run the PHP code which has this problem. I can run the sample shown from the browser or from the command line with the same negative results.
The following code will not make a new directory and change to it.
When running this program I was in
/home/rick/DB-Web/foolaround. I want to make a new directory
/home/rick/Desktop/2017Jul07-10:04_TransFile_102CANON and change the dir to it. This is a unique directory name so the error can't be coming from a duplicate directory.
Code: Select all
<?php
error_reporting (E_ALL ^ E_NOTICE);
$user=get_current_user();
$desktop="/home/$user/Desktop";
$endDir="102CANON";
$processDate=date("YMd-G:i_");
$transDir=trim($desktop."/".$processDate."_TransFile_".$endDir);
mkdir($transDir);
chdir($transDir);
$src=getcwd();
echo "\ncurrenrDir ".$src."..... \n\ntransDir ".$transDir; // ~/Desktop/photso
?>
Re: mkdir() will not make the directory
Posted: Fri Jul 07, 2017 9:27 pm
by requinix
Was the directory created at all? Are the permissions for Desktop set up to allow the user running the script to create directories?
What happens if you don't turn off E_NOTICEs and instead show
all errors?
Re: mkdir() will not make the directory
Posted: Fri Jul 07, 2017 11:18 pm
by pizzipie
Well now...thats weird.
It just made new directory from command line.
Didn't make the directory when initiated through the browser and AJAX though.
Re: mkdir() will not make the directory
Posted: Sat Jul 08, 2017 1:23 am
by requinix
Sounds like the answer
requinix wrote:Are the permissions for Desktop set up to allow the user running the script to create directories?
is "no".
I suggest putting these new directories in a subdirectory of Desktop. Say, "Photos". Change permissions to 0777 on that and your script should work from the browser.
Re: mkdir() will not make the directory
Posted: Sat Jul 08, 2017 3:02 pm
by pizzipie
Hi, I have cleaned the code up so it is easier to understand. The attachments show output from Firefox and Terminal executions. There are some warnings shown in the Terminal output that make little sense to me as they refer to lines php code. What really bothers me is that I have another Terminal PHP program that has the exact code as here that works fine. What I am working on here is a browser based substitute for that program. hope you can help.
Baffled R.
Code: Select all
<?php
error_reporting (E_ALL);
error_reporting(-1);
$user=get_current_user();
$desktop="/home/$user/Desktop";
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);
mkdir($transDir, $mode = 765);
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;
*/
?>
Re: mkdir() will not make the directory
Posted: Sat Jul 08, 2017 3:24 pm
by pizzipie
spammer, I tried that (home/rick/Desktop/photos/ ...) and it did not work either!!
R
Re: mkdir() will not make the directory
Posted: Sat Jul 08, 2017 4:45 pm
by pizzipie
Something else I tried is: $transDir="/home/rick/Desktop/someNewDir";
This results in making the directory "someNewDir" in the Desktop with the owner and group belonging to www-data.
I totally don't understand this.
Re: mkdir() will not make the directory
Posted: Sun Jul 09, 2017 12:06 am
by requinix
That is incorrect.
1. "$mode=765" will assign the value 765 to the variable $mode. It does not pass 765 as the "mode" parameter to the function. (However, the mode is the second parameter so 765 is actually getting there.)
2. 765 and 0765 are not the same in PHP. File permissions are written in octal with a leading zero: 0765 means rwxrw-r-x while 765 means -wxrwxr-s. Very different.
Assuming you want rwxrw-r-x permissions (and I'm not sure you do) then the code should read
mkdir(): No such file or directory
When creating a directory the parent must already exist. If you are trying to create /home/user/Desktop/TransFile_2017Jul08-12:45/102CANON then /home/user/Desktop/TransFile_2017Jul08-12:45 must already exist. PHP will not create it for you... unless you use the "recursive" parameter to mkdir().
pizzipie wrote:This results in making the directory "someNewDir" in the Desktop with the owner and group belonging to www-data.
Yes. You think you're running PHP from the browser but actually you're using the browser to make Apache run PHP. Apache is using the www-data system account, not your user account.
Meanwhile get_current_user() returns not the current running user (www-data) but the owner of the script being executed (rick). Which is why that seems to be working correctly.
Alright. Try this.
Code: Select all
rick@rick-Latitude-E6510:~/DB-Web/foolaround$ mkdir /home/rick/Desktop/Photos
rick@rick-Latitude-E6510:~/DB-Web/foolaround$ chmod 0777 /home/rick/Desktop/Photos
Code: Select all
<?php
error_reporting(-1);
$user=get_current_user();
$desktop="/home/$user/Desktop/Photos"; // everything in Photos so you don't clutter your desktop
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); // I doubt you need $endDir here
mkdir($transDir, 0777); // rwxrwxrwx and not recursive because the parent of $transDir (=$desktop) already exists
//chdir($transDir); // you don't actually need this now
$src=$choice; // pretty sure you mean this, because getcwd() would now be $transDir
if($cflag) { // command line execution
echo "\nOutput from Command: php -f trantest.php - \n\nSource Dir: ".$src."\n\nTransfer Dir: ".$transDir."\n\n";
}
else { // browser execution
echo "\nSource 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
// when using variables as arguments to commands, use escapeshellarg()
$cmd= "rsync -avz --dry-run ".escapeshellarg($src)." ".escapeshellarg($transDir); // Dry Run -From Photo Source to Transfer Directory
//$cmd= "rsync -avz ".escapeshellarg($src)." ".escapeshellarg($transDir); // From Photo Source to Transfer Directory
echo shell_exec($cmd); // Store Photos in Transfer Directory on Desktop
return $transDir;
*/
?>
Re: mkdir() will not make the directory
Posted: Sun Jul 09, 2017 1:37 pm
by pizzipie
Thanks a lot spammer.
I now have the browser creating the TransFile Directory. I uncommented the code at the bottom of the script, and the program now works properly transferring all the photos from my Cameras SD card in /media... to the new TransFile Directory. GREAT first step.
The rest of the program, not shown, asks the User if they want to re-name the photos with the Creation Date and/or transfer the photos to permanent storage elsewhere on the hard drive.
PROBLEM: The new TransFile Directory is owned by www-data. Since the permissions are all wrong I can't manipulate the image files in the TransFile directory. chown() doesn't work.
HELP!
R
Re: mkdir() will not make the directory
Posted: Sun Jul 09, 2017 11:33 pm
by requinix
Well, you're running this on your own computer, so
Find the Apache configuration where it says to run as www-data user and group and change that to be your user and group. Then restart it.
After you do that you can drop all the permission stuff in the code.
Re: mkdir() will not make the directory
Posted: Mon Jul 10, 2017 3:33 pm
by pizzipie
Well now the program will produce the new directory. Owned/Group by www-data.
I don't know now whether to create a new THREAD but I think this is connected to the original topic. Please correct me if I should create a new THREAD.
The next phase is copying the files from the Camera's SD Card. this is done by:
Code: Select all
$cmd= "rsync -avz ".$src." ".$transDir; // From Photo Source to Transfer Directory
shell_exec($cmd); // Store Photos in Transfer Directory on Desktop
This works correctly(shell_exec($cmd) works) and the photos are transfered TO /home/rick/Desktop/2017Jul10-12:34_TransFile_102CANON/102CANON. Owned/Group by www-data.
Next all Image files are renamed in directory /102CANON with the Creation Date of the Image.
Example : From IMG_8581.JPG To 2017Jun01-13:01_IMG_8581.JPG. Owned/Group by www-data.
The significance of this to me is that the files are manipulated (moved by shell_exec( using rsync ))while being Owned/Group by www-data.
The next step, Copying the renamed images to two different Directories on my hard drive, has a problem.
The two echo shell_exec($cmd) calls to rsync will not copy the files.
Nothing is echoed.
If I
run this program in a terminal it all works: A directory is created on Desktop, the photo files are transfered to this directory, the files in this directory are renamed, and then the renamed files are transfered to two other directories on the hard drive.
I am wondering
when running this program in the browser if the rsync command is messed up by improper permissions. I note that when the program is run in a terminal the end result directories are Owned by $user with proper permissions.
Sorry this is such a long explanation, but I am going batty trying to figure this out !!
Code: Select all
if($copyPhoto) {
storeTransFolderContents($transDir, $user);
}
Code: Select all
// storeTransFolderContents() ... Send Photos To /VB-share/00-Process - /Pictures/00-Process ================
function storeTransFolderContents($renameDir, $user) {
$cmd= "rsync -az --stats ".$renameDir." /home/".$user."/Pictures/00-Process";
echo shell_exec($cmd); // returns ""
$cmd2= "rsync -az --stats ".$renameDir." /home/".$user."/VB-share/00-Process";
echo shell_exec($cmd2); // returns ""
//$cmd= "rsync -az --stats --dry-run ". $renameDir." /home/".$user."/Pictures/00-Process";
//$cmd2= "rsync -az --stats --dry-run ". $renameDir." /home/".$user."/VB-share/00-Process";
} // end function ========================
Re: mkdir() will not make the directory
Posted: Tue Jul 11, 2017 12:57 am
by requinix
Please take some time to learn about file ownership and permissions in Linux. If you understand users, groups, and how permissions work then you should be able to understand why you're having a problem.