从一个目录(在共享路径上)获取所有文件,并将它们保存在wordpress uploads文件夹中


Get all the files from a directory(on a shared path) and save them in wordpress uploads folder

在共享路径上有一个目录,我正在扫描目录中的文件,现在我需要的是将每个文件保存在wordpress上传文件夹中的单个子文件夹中。并插入相同的帖子和附件的每个文件的详细信息保存到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>';

使用上面提到的RecursiveIteratorIterator()函数,我能够从存储库中检索所有文件信息。然后使用php的copy()方法通过传递上传目录路径和目标目标路径,我能够复制/上传存储库文件到我的自定义上传文件夹。