php中的mkdir和递归复制

发布于 2024-11-16 23:21:16 字数 2004 浏览 3 评论 0原文

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

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

//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 挂载的文件路径时,我会检查目录结构是否存在于 < code>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’已加载”

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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白日梦 2024-11-23 23:21:17

use :

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

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

use :

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

where 0777 is chmod and bool true activate recursive mode

简美 2024-11-23 23:21:17

不幸的是,问题很简单。我原来的挂载点没有设置 root 的写入权限。将挂载点所有者和组更改为 root 后,它就可以工作了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文