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

时间:2015-08-31 06:51:14

标签: php wordpress

共享路径上有一个目录,我正在扫描目录中的文件,现在我需要的是将这个文件保存在wordpress的uploads文件夹中的单个子文件夹中。并插入保存到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>';

1 个答案:

答案 0 :(得分:0)

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