因此,情况是WordPress通常安装在有限的权限或没有完美配置的Web主机上,并且无法通过Php代码功能创建目录,复制或移动文件,或复制或移动整个目录到新位置。 我遇到问题:
rename(.., ..);
给了 - “没有必要的权限”错误,CHMOD(..., 0777);
也没有使目录可写,- 醇>
mkdir(..., 0777);
没有在网络用户下创建可写dirs,但是在apache用户(1000)下创建了0755 dirs,其中来自WordPress引擎的Php脚本无法写入。
我正在寻找最佳工作解决方案(或WorkAround),以便在常规重命名(..),chmod(..)和mkdir(..)Php函数不能正常工作时将所有文件从一个文件夹复制到另一个文件夹预期
答案 0 :(得分:-1)
所以我提出了一个解决方案 - 一个有效的课程,并在本主题的答案中提供了它的代码。我希望这会为你节省半天,因为它为我节省了。
我写了一个解决这个问题的课程,我相信这对你们很多人有帮助,他们为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;
}
}