Search code examples
phpcopymkdirshell-execrecursive-datastructures

mkdir and recursive copy in php


My function is having problems creating and copying the contents to the new directory (also I'm not sure if this is the best way to do this, so alternate suggestions are welcome).

I have 2 network drives mounted via /etc/fstab like this:

//128.251.108.xxx/Data/Agilent_Data /home/lv_admin/uslonsnas001 cifs cred=/etc/.na02passwd,rw,umask=0000,uid=www-data,gid=webgroup 0 0
//128.251.108.xx/c$/Agilent /home/lv_admin/uslonsapp003 cifs cred=/etc/.na02passwd,rw,umask=0000,uid=www-data,gid=webgroup 0 0

Basically, when prompted with a file path from uslonsapp003 mount I check to see if the directory structure exists in uslonsnas001 and create the recursive directory if not. Then I copy the content from uslonsapp003 to the new structure location in uslonsnas001. Here is my code:

$pImagePath = "http://uslonsapp003:8080/boardtests/2011/4/29/12/30/8051/Images/E_1-c274.jpg";
//strip off the path name up to '2011' and take off the image name at the end
    $startpos = strpos( $pImagePath, "/boardtests/" ) + strlen( "/boardtests/" );
    $endpos   = strpos( $pImagePath, "/Images/" );
    $file_dir = substr( $pImagePath, $startpos, ( $endpos - $startpos ) );
    $orig_dir = "/home/lv_admin/uslonsapp003/ITFSS/DataStore/BoardTest/" . $file_dir;
    $new_dir  = "/home/lv_admin/uslonsnas001/BoardTest/" . $file_dir;
    if( !is_dir( $new_dir ) )
        if( !shell_exec("mkdir -p $new_dir") )    return array( "status" => 0, "errordesc" => "failed to make dir: '" . $new_dir . "'" );
    if( !shell_exec("cp -r $orig_dir $new_dir") ) return array( "status" => 0, "errordesc" => "failed to copy from: '" . $orig_dir . "' to: '" . $new_dir . "'" );
    return array( "status" => 1 );

I've been getting both errors, 'failed to make dir...' and 'failed to copy from...'

This is executed through Apache, I'm assuming it's a permissions problem but thats just my 'hunch'. Please help!

I've tried adding sudo to the beginning of the shell_exec()'s but that still doesn't work.

UPATED1

I figured out that the mkdir was failing because when I created the /home/lv_admin/uslonsnas001 directory I didn't change the mod, owner, and group on it to the one that would be using it (www-data). Doing the following fixed that part:

$ sudo chmod 775 ~/uslonsnas001
$ sudo chown www-data ~/uslonsnas001
$ sudo chgrp webgroup ~/uslonsnas001

But I'm still having problems with the copy command, now saying "Module 'ODBC' already Loaded"


Solution

  • Unfortunately the problem was simple. My original mount point was not setup with write permissions for root. After changing the mount point owner and group to root it works.