Jump to content

How to print all images stored in a file Directory?


Jenksuy
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
}
 ?>
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 . '" />';
                    } 
        ?>
Link to comment
Share on other sites

Which mean that your glob() function returns false. False is not an array and that is why the foreach fails.

 

Now, why is it returning false? Because of some error! Have you turned on error reporting?

Link to comment
Share on other sites

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 . '\'))" />';
    }
}


?>
Link to comment
Share on other sites

  • 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');
    }
?>
Link to comment
Share on other sites

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.