There is a directory on shared path, I'm scanning the directory for files, now what i need is to save each of this file in a single subfolder in uploads folder of wordpress. And insert the same post and attachments details of each file saved into db.
/*Function to List all the folders*/
function listfolders($dir) {
static $alldirs = array();
$dirs = glob($dir . '/*', GLOB_ONLYDIR);
if (count($dirs) > 0) {
foreach ($dirs as $d) $alldirs[] = basename($d);
}
foreach ($dirs as $dir) listfolders($dir);
return $alldirs;
}
$folder_list = listfolders('D:\Base Folder');
/***Saving each FOLDER as TAG in wp_bdqueries_tags Table***/
foreach($folder_list as $folder_name){
$folder_slug = strtolower(preg_replace('/\s+/', '-', $folder_name));
echo $folder_name.'***'.$folder_slug.'<br>';
/*global $wpdb;
$wpdb->insert("wp_bdqueries_tags", array(
"tag_group_id" => 27,
"tag_name" => $folder_name,
"tag_slug" => $folder_slug,
"tag_status" => 'approved',
"tag_description" => '',
"tag_postedby" => 0 ,
"tag_moderatedby" => 0,
"tag_addedon" => date('Y-m-d h:i:s')
));*/
}
/***Listing all the Files and thier FOLDERS/TAGS***/
$directory = 'D:\Base Folder';
$allfiles = array();
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
while($it->valid()) {
if (!$it->isDot()) {
$SubPath = $it->getSubPath();
$tags = explode("\\", $SubPath);
echo "Tags: ";
foreach ($tags as $tag) {
if($tag !== end($tags)) echo $tag.' | ';
else echo $tag;
}
$path = $it->key();
echo $path;
echo '<br>Filename: <a href="'.$path.'" alt="Download" target="_blank" title="Download" >'. basename($path) . "</a><br>";
$allfiles[] = basename($path);
echo "<br>";
}
$it->next();
}
echo '<ol>';
foreach ($allfiles as $file) {
echo '<li>'.$file.'</li>';
}
echo '</ol>';
Using RecursiveIteratorIterator() function mentioned above i was able to retrieve all the files info from the Repository. Then using copy() method of php by passing the upload directory path and the target destination path i was able to copy/uploads the repository file to my custom uploads folder.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.