开发者

For loop over named folder to read each .txt filename and contents

开发者 https://www.devze.com 2023-02-16 17:21 出处:网络
The script below takes a named file that resides in the \"myplugin\" folder (the folder that the script itself resides in) and runs file_get_contents() on it to load the contents into memory, then doe

The script below takes a named file that resides in the "myplugin" folder (the folder that the script itself resides in) and runs file_get_contents() on it to load the contents into memory, then does some preprocessing on the contents before finally inserting it as a post into the WordPress database via the wp_insert_post method.

$my_post3 = array();
$my_post3['post_title'] = 'Privacy Policy';
if(file_exists(ABSPATH.'/wp-content/plugins/myplugin/pages/privacy_policy.txt'))
{
$my_privacy_policy = file_get_contents(ABSPATH.'/wp-content/plugins/myplugin/pages/privacy_policy.txt');
}
else
{
$my_privacy_policy = "";
}
$my_post3['post_content'] = addslashes($my_post3_replace);
$my_post3['post_type'] = 'page';
$my_post3['post_status'] = 'publish';
wp_insert_post($my_post3);

This method works pretty good. However, this method forces me to write a different routine for every file I want to use as the basis of a new page.

What I would like to do instead, is create a folder called "pages" and place m开发者_Go百科y .txt files in that, then run a for loop on the contents of the folder, creating a new page for each file in the folder. I'd like to use the file name (minus the .txt extension) as the name of the page.

For example, the pages folder may have these files:

About Us.txt Contact Us.txt

And the routine would result in the creation of two new pages in WordPress site, one called "About Us" containing the content found in that file. The other page would of course be "Contact Us" with the contents of that file.

In this way, I can just drop an unlimited number of named and prepopulated .txt files into that folder and when I activate my plugin, it creates those pages.

I just need some help with the for loop and how to reference the folder and files.

I will also have a folder called "posts", which will do the same for posts that this routine does for pages.

Thanks in advance for your help and suggestions.

Update based on @clientbucket answer:

DEFINE ('PAGES', './pages/');
$directory_pages = new DirectoryIterator(PAGES); 
foreach ($directory_pages as $files) {
    if ($files_pages->isFile()) {
        $file_name_page = $files_pages->getFilename();
        $my_page_content = file_get_contents(PAGES. $file_name_page);
        $my_page['post_content'] = addslashes($my_page_content);
        $my_page['post_title'] = $file_name_page;
        $my_page['post_type'] = 'page';
        $my_page['post_status'] = 'publish';
        wp_insert_post($my_page);
    }
}

DEFINE ('POSTS', './posts/');
$directory_posts = new DirectoryIterator(POSTS);
foreach ($directory_posts as $files_posts) {
    if ($files_posts->isFile()) {
        $file_name_post = $files_posts->getFilename();
        $my_post_content = file_get_contents(POSTS. $file_name_post);
        $my_post['post_content'] = addslashes($my_post_content);
        $my_post['post_title'] = $file_name_post;
        $my_post['post_type'] = 'post';
        $my_post['post_status'] = 'publish';
        $post_id = wp_insert_post($my_post);
        stick_post($post_id);   
        }
}

Fatal error: Uncaught exception 'UnexpectedValueException' with message 'DirectoryIterator::__construct(./pages/) [directoryiterator.--construct]: failed to open dir: No such file or directory' in C:\xampplite\htdocs\mytestsite\wp-content\plugins\myplugindirectory\myplugin.php:339

Line 339 is here > $directory_pages = new DirectoryIterator(PAGES);


Here is another way you could try.

DEFINE ('PAGES', './pages/'); //Define the directory path
$directory = new DirectoryIterator(PAGES); //Get all the contents in the directory 
foreach ($directory as $files) { //Check that the contents of the directory are each files  and then do what you want with them after you have the name of the file. 
    if ($files->isFile()) {
        $file_name = $files->getFilename();
        $my_page = file_get_contents(PAGES. $file_name); //Collect the content of the file.
    } else {
        //Insert nothing into the $my_privacy_policy variable.
    }
    echo $my_page; // Do what you want with the contents of the file.
}


From the PHP manual here:

http://php.net/manual/en/function.glob.php

They provide this solution for finding all text files in a directory:

<?php
foreach (glob("*.txt") as $filename) {
    echo $filename . "\n";
}
?>

Given this example, your actual request is to be able to create a file based on the name in another directory. I'll leave the hard work to you - but this is a simple implementation:

<?php
$source_dir = "/your/directory/with/textfiles";
$target_dir = "/directory/to/create/files/in";
foreach (glob($source_dir . DIRECTORY_SEPARATOR . "*.txt") as $filename) {
    $filepart = explode('.',$filename);
    file_put_contents($target_dir . DIRECTORY_SEPARATOR . $filepart[0] . ".php");
}
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消