如何创建一个文件夹,并复制它的所有文件到新的WordPress插件文件夹


How to create a folder and copy all it's files to new WordPress plugin folder?

所以情况是WordPress通常安装在有限的权限或不完美配置的web主机上,并且不可能通过Php代码函数创建目录,复制或移动文件,或复制或移动整个目录到新位置。我有问题时:

  1. rename(.., ..); given - 'do not have required permission'错误,
  2. CHMOD(..., 0777);也没有使目录可写,
  3. mkdir(..., 0777);没有在web用户下创建可写dirs,而是在apache用户(1000)下创建了0755 dirs,其中WordPress引擎的Php脚本无法写入。

我正在寻找一个最佳的工作解决方案(或工作区)复制所有文件从一个文件夹到另一个文件夹时,常规重命名(..),chmod(..)和mkdir(..)Php函数没有按预期工作

所以我做了一个解决方案-一个类的工作,并提供了它的代码在这个主题的答案。我希望这会为你节省半天时间,就像现在为我节省半天时间一样。

我写了一个类来解决这个问题,我相信这将对你们中的许多人有所帮助,他们为WordPress开发插件,并且必须对文件进行操作。

用法示例如下:

$uploadDir = wp_upload_dir();
$fromFolder = ABSPATH."wp-content/plugins/old-cool-plugin/gallery"
$toFolder = $uploadDir['basedir']."/cool-plugin-gallery";
$copied = WordPressFileManager::copyFolder(fromFolder, $toFolder);
if($copied)
{
   echo 'All old folder files copied successfully to new directory';
} else
{
   echo 'Your server setup is so bad, that it is unbeatable even with perfect scripts';
}

类代码如下:

class WordPressFileManager
{
    public static function createWritableDirectory($paramToFolderWithoutEndSlash)
    {
        // Create an upload dir if not exist
        $newDirectoryExists = TRUE;
        if (!file_exists($paramToFolderWithoutEndSlash))
        {
            // The mkdir doesn't work in WordPress setup on regular servers (DirectAdmin+Ubuntu based etc.)
            //$uploadDirectoryExists = mkdir($paramToFolderWithoutEndSlash, 0777, TRUE);
            $newDirectoryExists = wp_mkdir_p($paramToFolderWithoutEndSlash);
        }
        // Check if folder iss writable
        $newDirectoryWritable = FALSE;
        if($newDirectoryExists)
        {
            if(is_writable($paramToFolderWithoutEndSlash) === FALSE)
            {
                chmod($paramToFolderWithoutEndSlash, 0777);
                if(is_writable($paramToFolderWithoutEndSlash))
                {
                    $newDirectoryWritable = TRUE;
                }
            } else
            {
                $newDirectoryWritable = TRUE;
            }
        }
        return $newDirectoryWritable;
    }

    /**
     * Because copy($moveFolderAndAllFilesInsideFrom, $paramToFolder) - does NOT work in this WordPress setup (because of CHMOD rights),
     * so we need a workaround function - and this is the main reason why we have a function bellow, which DOES WORK!
     * @param $moveFolderAndAllFilesInsideFrom
     * @param $paramToFolderWithoutEndSlash
     */
    public static function recurseCopy($moveFolderAndAllFilesInsideFrom, $paramToFolderWithoutEndSlash)
    {
        $sourceDirectory = opendir($moveFolderAndAllFilesInsideFrom);
        while (FALSE !== ( $file = readdir($sourceDirectory)) )
        {
            if (( $file != '.' ) && ( $file != '..' ))
            {
                if ( is_dir($moveFolderAndAllFilesInsideFrom.'/'.$file))
                {
                    static::recurseCopy($moveFolderAndAllFilesInsideFrom.'/'.$file, $paramToFolderWithoutEndSlash.'/'.$file);
                } else
                {
                    copy($moveFolderAndAllFilesInsideFrom.'/'.$file, $paramToFolderWithoutEndSlash.'/'.$file);
                }
            }
        }
        closedir($sourceDirectory);
    }
    /**
     * Copy folder and all it's files from it's old location to new location
     * @param $copyAllFilesFromFolderWithoutEndSlash
     * @param $paramToFolderWithoutEndSlash
     * @return bool
     */
    public static function copyFolder($copyAllFilesFromFolderWithoutEndSlash, $paramToFolderWithoutEndSlash)
    {
        $copied = FALSE;
        if(file_exists($copyAllFilesFromFolderWithoutEndSlash))
        {
            $toDirectoryIsWritable = static::createWritableDirectory($paramToFolderWithoutEndSlash);
            if($toDirectoryIsWritable)
            {
                // NOTE: copy() does NOT work in this WordPress setup (because of CHMOD rights)
                //$copied = copy($moveFolderAndAllFilesInsideFrom, $paramToFolderWithoutEndSlash);
                static::recurseCopy($copyAllFilesFromFolderWithoutEndSlash, $paramToFolderWithoutEndSlash);
                $copied = TRUE;
            }
            // DEBUG
            //echo "<br />[{$copyAllFilesFromFolderWithoutEndSlash}] SOURCE FOLDER (TO MOVE FILES FROM IT) DO EXISTS, ";
            //echo "destination folder is writable: "; var_dump($toDirectoryIsWritable);
        } else
        {
            // DEBUG
            //echo "<br />[{$copyAllFilesFromFolderWithoutEndSlash}] SOURCE FOLDER (TO MOVE FILES FROM IT) DO NOT EXISTS";
        }
        return $copied;
    }
}