Jump to content

How to print all images stored in a file Directory?


Go to solution Solved by Jenksuy,

Recommended Posts

I have been trying for a day now to print all the images stored in a directory, however at the minute i can only get one to appear.

 

could anyone point me in the right direction?

function wptuts_setting_logo_preview() {
	$wptuts_options = get_option( 'theme_wptuts_options' ); 
    $user_id = get_current_user_id();
	$files = glob('wp-content/uploads/'.$user_id.'/*');
?>
    
	<div id="upload_logo_preview" style="min-height: 100px;">
		<img style="max-width:100%;" src="<?php echo esc_url( $wptuts_options['logo'] ); ?>" />
	</div>
	<?php
}
 ?>

glob will return the filenames/paths to all files that match the expression in the directory that you give it as an array. So you will need a foreach loop to loop through each of the files and do with them what you would like:

 

 

$files = glob(path/to/images/*);
 
foreach($files as $file){
   echo $file; //this will echo the complete filename/path
}

 

Start playing around with that and post back if you need more help.

 

Luke

I thought that might be what i needed as i used it before in the frontend of my plugin... i have now integrated it into the backend

 

here is my code

function wptuts_setting_logo_preview() {
	$wptuts_options = get_option( 'theme_wptuts_options' );  ?>
	<div id="upload_logo_preview" style="min-height: 100px;">
		<img style="max-width:100%;" src="<?php $files = glob('wp-content/uploads/'.$user_id.'/*');
                foreach($files as $file) {
                    echo $file;
                    } ?>" />
	</div>
	<?php
}

?>

When i inspect element in my source i get invalid argument supplied for foreach()

 

I cant see what Im missing from my code... What do you think?

Also, I would imagine that image tag needs to go inside the foreach loop.

<?php
$files = glob('wp-content/uploads/'.$user_id.'/*');
foreach($files as $file) {
     echo '<img style="max-width:100%;" src="' . $file . ' />';
}
?>

It isn't the user id as I have now taken it out...

function wptuts_setting_logo_preview() {
	$wptuts_options = get_option( 'theme_wptuts_options' );  ?>
	<div id="upload_logo_preview" style="min-height: 100px;">
		<?php 
                $files = glob('wp-content/uploads/1/*');
                foreach($files as $file) {
                    echo '<img src="/' . $file . '" />';
                    } 
        ?>
	</div>
	<?php
}

?>

I have also put the full image tag inside the echo... the error i get is..

 

Warning: Invalid argument supplied for foreach() in public_html/wp-content/plugins/roundabout-with-thumbs-and-lightbox/wptuts-options.php on line 166

Use a var_dump on $files, its likely that its not what you expect it to be.

<?php 
                $files = glob('wp-content/uploads/1/*');
echo '<pre>' . print_r($files, true) . '</pre>';
var_dump($files);
die();

                foreach($files as $file) {
                    echo '<img src="/' . $file . '" />';
                    } 
        ?>

Yes I have error reporting on, but I am not getting any other errors.

 

here is the code for my frontend of the plugin... as you can see I have used glob the exact same way in here but it is working?

<?php  
    /* 
    Plugin Name: Roundabout with Thumbs and Lightbox 
    Plugin URI: http://ourface.co.uk 
    Description: Displays the Jquery roundabout plugin with both Thumbnails and Lightbox 
    Version: 1.0 
    Author: Chris Jenks
    Author URI: http://twitter.com/jenksuy
    */  

require_once( 'wptuts-options.php' );

function display_roundabout()
{
    $user_id = get_current_user_id();
    $files = glob('wp-content/uploads/'.$user_id.'/*');
    natcasesort($files);
    foreach($files as $file) {
        echo '<li>
        <img id="' . $file . '" src="/' . $file . '" />
        <a href="/' . $file . '" rel="lightbox[roadtrip]"><img src="/' . $file . '" alt="" id="link" /></a>
        </li>';
    }
}

function add_thumbnails()
{
    $user_id = get_current_user_id();
	$files = glob('wp-content/uploads/'.$user_id.'/*');
	natcasesort($files);
	foreach($files as $file) {
        echo '<img src="/' . $file . '" onclick="fakeClick(event, document.getElementById(\'' . $file . '\'))" />';
    }
}


?>
  • Solution

Finally got it working... started my code from Scratch and made it much simpler which finally has it working... Ill paste it here for anyone who needs help!

<?php
    /** Step 2 (from text above). */
    add_action( 'admin_menu', 'my_plugin_menu' );
    
    /** Step 1. */
    function my_plugin_menu() {
        add_menu_page( 'Roundabout', 'Roundabout', 'manage_options', 'my-unique-identifier', 'my_roundabout_options' );
    }
    
    /** Step 3. */
    function my_roundabout_options() {
        if ( !current_user_can( 'manage_options' ) )  {
            wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
        }
        echo '<div class="wrap">';
        echo '<label for="upload_image">';
        echo '<input id="upload_image" type="text" size="36" name="ad_image" value="http://" />';
        echo '<input id="upload_image_button" class="button" type="button" value="Upload Image" />';
        echo 'Enter a URL or upload an image';
        echo '</label>';
        echo '</div>';
    }

    add_action('admin_enqueue_scripts', 'my_admin_scripts');
     
    function my_admin_scripts() {
        wp_enqueue_media();
        wp_register_script('my-admin-js', WP_PLUGIN_URL.'/roundabout-with-thumbs-and-lightbox/js/backend.js', array('jquery'));
        wp_enqueue_script('my-admin-js');
    }
?>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.