0
programattically我正在扫描一个directoy的文件(完成),在我插入每个文件后作为一个职位(完成),同时每个职位必须作为附件插入并保存在wordpress uploads文件夹中。编程添加wordpress后附附件(pdf,word,excel或ppt文件)
我需要插入附件的详细信息,并保存相应的文件,我坚持的那部分,这里是我写
<?php
$current_user = wp_get_current_user();
$userRoleArray = $current_user->roles;
$current_userid = $current_user->ID;
$directory = 'D:\BD Repository'; //Base Directory
$project = "bdqueries";
/*** Scan the Directory ***/
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($directory); // Function to get all the folders and subfolders
foreach($folder_list as $folder_name){ // Saving Folders as Tags in wp_bdqueries_tags table
$folder_slug = strtolower(preg_replace('/\s+/', '-', $folder_name));
global $wpdb;
$wpdb->insert("wp_bdqueries_tags", array(
"tag_group_id" => 44,
"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')
));
echo $folder_name.'***'.$folder_slug.'<br>';
}
/*** Get All the Files | Insert the posts and attachments | Copy files ***/
$allfiles = array();
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
while($it->valid()) {
if (!$it->isDot()) {
$SubPath = $it->getSubPath();
$path = $it->key();
$filetype = wp_check_filetype(basename($path),null); // Check the type of file for'post_mime_type'
$filename = preg_replace("/\.[^.]+$/", "", basename($path));
$file_slug = strtolower(preg_replace('/\s+/', '-', $filename));
echo '<br>Filename: <a href="'.$path.'" alt="Download" target="_blank" title="Download" >'. $filename . "</a><br>";
$allfiles[] = $filename; //saving all file names in array
/*Inserting each file as a post*/
$file_post = array(
'post_title' => $filename,
'post_status' => 'publish',
'post_date' => date('Y-m-d h:i:s'),
'post_date_gmt' => gmdate('Y-m-d h:i:s'),
'post_author' => $current_userid,
'post_name' => $file_slug,
'post_type' => 'bdlibrary',
'guid' => ''
);
// Insert the post into the database
//wp_insert_post($file_post);
$post_id = wp_insert_post($file_post, $wp_error);
/*** END *** Inserting each file as a post*/
/*Inserting each attachment details*/
$wp_upload_dir = wp_upload_dir(); // Get the path to the upload directory.
$target_dir = UPLOADS."/bd_library_uploads/";
$target = $wp_upload_dir['basedir'].'/bd_library_uploads';
// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename($path),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($path)),
'post_content' => '',
'post_name' => $file_slug,
'post_status' => 'inherit',
'post_date' => date('Y-m-d h:i:s'),
'post_date_gmt' => gmdate('Y-m-d h:i:s'),
'post_type' => 'attachment',
);
// Insert the attachment.
$attach_id = wp_insert_attachment($attachment, $target, $post_id);
/*** END *** Inserting each attachment details*/
$tags = explode("\\", $SubPath);
echo "Tags: ";
/*** Use post_id and Insert into wp_tags_association table ***/
foreach ($tags as $tag) {
if($tag !== end($tags)){
echo $tag.' | ';
}
else{
echo $tag."<br>";
}
$tagSlug = strtolower(preg_replace('/\s+/', '-', $tag));
echo $tagSlug;
global $wpdb;
$taginfo = $wpdb->get_row("SELECT tag_id,tag_group_id FROM wp_bdqueries_tags WHERE tag_slug = '".$tagSlug."'", ARRAY_A);
$tag_id = $taginfo['tag_id'];
$tag_group_id = $taginfo['tag_group_id'];
if($tag_id){
$tassc_project = $project;
$tassc_userid = $current_userid;
$tassc_tagged_under = 'bdlibrary';
$tassc_tag_assc_itemid = $post_id;
//echo "apan idhar hai";
$wpdb->insert("wp_tags_association", array(
"tassc_tagid" => $tag_id,
"tassc_tag_groupid" => $tag_group_id,
"tassc_project" => $tassc_project,
"tassc_userid" => $tassc_userid,
"tassc_tagged_under" => $tassc_tagged_under,
"tassc_tag_assc_itemid" => $tassc_tag_assc_itemid,
"tag_added_date" => date('Y-m-d h:i:s')
));
}
}
}
$it->next();
}
?>
感谢fot的回复,即使我们的附件是word或pdf文件,这项工作是否也能正常工作? – immazharkhan
我没有测试它的文档/ pdf文件,但我认为它也应该在这些文件上工作。你需要测试它。 – gskhanal