To drag and drop over a named folder to read each .txt file name and contents

The script below displays the named file, which is located in the "myplugin" folder (the folder where the script itself is located), and runs the file_get_contents () file to load the contents into memory, and then does some preprocessing on the contents before finally paste it as an entry into the WordPress database using 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 very well. However, this method forces me to write a different procedure for each file that I want to use as the basis for a new page.

"" .txt, for , , ( .txt) .

, :

.txt .txt

WordPress, " ", , . , , " " .

, .txt , , .

for .

"posts", , .

.

@clientbucket:

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);   
        }
}

: "UnexpectedValueException" "DirectoryIterator:: __ construct (./pages/) [directoryiterator.-- construct]: : ' C:\xampplite\htdocs\mytestsite\-\Plugins\myplugindirectory\myplugin.php: 339

339 > $directory_pages = new DirectoryIterator ();

+3
2

.

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.
}
+2

PHP :

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

:

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

, . , :

<?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");
}
?>
+1

All Articles