【问题标题】:mkdir and recursive copy in phpphp中的mkdir和递归复制
【发布时间】:2011-06-23 12:35:27
【问题描述】:

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

我通过/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'已经加载”

【问题讨论】:

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


    【解决方案1】:

    使用:

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

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

    【讨论】:

    • 我在使用 mkdir() 时遇到了同样的错误,虽然我不知道 php 错误是什么。是否有可以打印的 mkdir() 抛出的错误的错误恢复名称?
    • 可能您的问题来自对网络驱动器的根目录权限。检查这些根目录的访问用户,它可能是由另一个用户而不是root创建的
    • 再次检查我的 OP,我的 fstab 行显示我正在使用 www-data 用户安装它。但这在过去没有造成问题,我还有其他创建文件的程序(当然我从未创建过目录)。
    • this page,对你有帮助
    • 不幸的是,这没有帮助。告诉我的只是我的 fstab 挂载点中的 umask 是正确的,我应该能够写入挂载。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2011-01-04
      • 1970-01-01
      • 2011-05-27
      • 2012-04-19
      • 1970-01-01
      • 1970-01-01
      • 2013-11-06
      • 2011-09-20
      • 2011-02-27
      相关资源
      最近更新 更多