mkdir和php中的递归副本

时间:2011-06-23 12:35:28

标签: php copy mkdir shell-exec recursive-datastructures

我的功能是在创建内容并将其复制到新目录时遇到问题(我也不确定这是否是执行此操作的最佳方式,因此欢迎使用其他建议。)

我通过/etc/fstab安装了2个网络驱动器,如下所示:

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

基本上,当提示来自uslonsapp003 mount的文件路径时,我检查uslonsnas001中是否存在目录结构,如果没有则创建递归目录。然后,我将内容从uslonsapp003复制到uslonsnas001中的新结构位置。这是我的代码:

$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 );

我一直在犯这两个错误,'未能成功......'并且'未能复制......'

这是通过Apache执行的,我假设这是一个权限问题,但这只是我的“预感”。请帮忙!

我尝试将sudo添加到shell_exec()的开头,但这仍然无效。

UPATED1

我发现mkdir失败了,因为当我创建/home/lv_admin/uslonsnas001目录时,我没有将其中的mod,所有者和组更改为将使用它的那个(www-data)。执行以下操作修复了该部分:

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

但是我仍然遇到复制命令的问题,现在说“模块'ODBC'已经加载了”

2 个答案:

答案 0 :(得分:0)

使用:

mkdir("path/to/your/directory", 0777, true);

其中0777是chmod和bool true激活递归模式

答案 1 :(得分:0)

不幸的是问题很简单。我的原始挂载点未设置为root的写入权限。将安装点所有者和组更改为root后,它可以正常工作。