Wordpress 从插件将文件夹导入自定义目录
我正在写一个插件。我编写这个函数是为了在根目录中创建一个自定义目录Wordpress 从插件将文件夹导入自定义目录,wordpress,plugins,Wordpress,Plugins,我正在写一个插件。我编写这个函数是为了在根目录中创建一个自定义目录 function create_api_dir() { $upload = wp_upload_dir(); //$upload_dir = $upload['basedir']; $upload_dir = $_SERVER['DOCUMENT_ROOT']; $upload_dir = $upload_dir . '/api'; if (! is_dir($upload_dir))
function create_api_dir() {
$upload = wp_upload_dir();
//$upload_dir = $upload['basedir'];
$upload_dir = $_SERVER['DOCUMENT_ROOT'];
$upload_dir = $upload_dir . '/api';
if (! is_dir($upload_dir)) {
mkdir( $upload_dir, 0700 );
}
}
register_activation_hook( __FILE__, 'create_api_dir' );
此函数正在根服务器中创建名为api
的文件夹
现在,我的插件还包括一个同名api文件夹,其中包含文件。我想允许用户将整个文件夹(解包)导入到api
文件夹,或者在插件激活时将其上载到api文件夹
我怎样才能做到这一点
谢谢我找到了解决办法。请告知是否可以实施。我没有复制文件夹及其子文件夹,而是压缩了整个文件夹
api.zip
。一个用于复制文件,另一个用于在目标中解压文件。给你
/*....function to copy the zip file...*/
function recurse_copy($src,$dst = 0) {
$src = plugin_dir_path( __FILE__ ) . 'api';
$dst = $_SERVER['DOCUMENT_ROOT'] . '/api';
$dir = opendir($src);
@mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
recurse_copy($src . '/' . $file,$dst . '/' . $file);
}
else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
}
add_action('plugins_loaded', 'recurse_copy', 10, 2);
/*...function to unzip the file in the destination ....*/
function unzip_api(){
WP_Filesystem();
$destination = $_SERVER['DOCUMENT_ROOT'] . '/api';
$destination_path = $_SERVER['DOCUMENT_ROOT'] . '/api';
$unzipfile = unzip_file( $destination_path.'/api.zip', $destination_path);
if ( $unzipfile ) {
echo 'Successfully unzipped the file!';
} else {
echo 'There was an error unzipping the file.';
}
}
add_action('admin_init', 'unzip_api');
我找到了一个解决办法。请告知是否可以实施。我没有复制文件夹及其子文件夹,而是压缩了整个文件夹
api.zip
。一个用于复制文件,另一个用于在目标中解压文件。给你
/*....function to copy the zip file...*/
function recurse_copy($src,$dst = 0) {
$src = plugin_dir_path( __FILE__ ) . 'api';
$dst = $_SERVER['DOCUMENT_ROOT'] . '/api';
$dir = opendir($src);
@mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
recurse_copy($src . '/' . $file,$dst . '/' . $file);
}
else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
}
add_action('plugins_loaded', 'recurse_copy', 10, 2);
/*...function to unzip the file in the destination ....*/
function unzip_api(){
WP_Filesystem();
$destination = $_SERVER['DOCUMENT_ROOT'] . '/api';
$destination_path = $_SERVER['DOCUMENT_ROOT'] . '/api';
$unzipfile = unzip_file( $destination_path.'/api.zip', $destination_path);
if ( $unzipfile ) {
echo 'Successfully unzipped the file!';
} else {
echo 'There was an error unzipping the file.';
}
}
add_action('admin_init', 'unzip_api');