Just to get you going, here some plugin code which might be useful to to add a bunch of files in some directory structure to wordpress post database. They are visible at least.
Much nicer would be to use wordpress own functions of course.
<?php
/*
Plugin Name: scanuploaddir
Plugin URI:
Description: scans upload dir and adds to posts.
Todo:
- create thumbnails and add metadata
- skip already available attachments in posts
- add option for file-description
Version: 0.1
Author: HendrikJan
Author URI:
*/
/*
License: GPL
*/
// sure we can get this from WP somewhere
$wp_path="/wp";
/* Add a tab to the administration panel, just below dashboard */
add_action('admin_menu', 'show_tree');
function show_tree() {
if (function_exists('add_submenu_page')) {
add_submenu_page("index.php", "ShowTree", "ShowTree", 10, __FILE__, 'showtree'); }
}
function addfiles($options){
// GENERATES SQL FOR INSERTION
global $wpdb,$wp_path;
$abspath=realpath(dirname(__FILE__)."/../uploads/");
foreach($options as $options){
$mimetype=mime_content_type($options);
$relpath=str_replace($abspath,"",$options);
$filepath=$relpath;
$relpath="https://".$_SERVER[HTTP_HOST].$wp_path."/wp-content/uploads".$relpath;
$query="INSERT INTO $wpdb->posts (post_status,post_title,post_name,guid,post_mime_type) VALUES ('attachment','$filepath','$filepath','$relpath','$mimetype')";
echo $query;
echo "<br/>";
//$wpdb->query($query); //uncomment for running the insertions
}
}
function listfiles(){
// DISPLAYS THE UPLOADS DIR
$abspath=realpath(dirname(__FILE__)."/../uploads/");
echo $abspath;
map_dirs($abspath);
}
function map_dirs($path) {
// MAKES RECURSIVE LIST OF FILES FOR FORM
$i=1;
if(is_dir($path)) {
//echo "<h2>".$path."</h2>";
if($contents = opendir($path)) {
while(($node = readdir($contents)) !== false) {
if($node!="." && $node!="..") {
$full= $path."/".$node;
if(is_file($full)){
$i++;
$mimetype=mime_content_type($full); //Warning, mime_content_type is depreciated
echo "<br/><input name=\"options[]\" value=\"$full\" type=\"checkbox\" CHECKED> $full $mimetype";
}
map_dirs("$path/$node");
}
}
}
}
}
function showtree () {
global $options;
echo "<pre>";
//print_r($_SERVER);
echo "</pre>";
// SHOW FORM
_e('<div class="wrap"><h2>List of files in upload dir</h2><p>Please select which files should be added to DB</p><form method="post">');
listfiles();
_e('<p class="submit"><input type="submit" name="submit" value="add files" /></p></form></div>');
// SUBMIT THE THING
if (isset($_POST['submit'])) {
_e('<div class="wrap">');
_e('<H1>Files added:</H1>');
addfiles($_POST['options']);
_e('</div>');
}
}
?>