Jenksuy Posted June 13, 2013 Share Posted June 13, 2013 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/279108-how-to-print-all-images-stored-in-a-file-directory/ Share on other sites More sharing options...
denno020 Posted June 13, 2013 Share Posted June 13, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/279108-how-to-print-all-images-stored-in-a-file-directory/#findComment-1435703 Share on other sites More sharing options...
Jenksuy Posted June 13, 2013 Author Share Posted June 13, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/279108-how-to-print-all-images-stored-in-a-file-directory/#findComment-1435713 Share on other sites More sharing options...
cyberRobot Posted June 13, 2013 Share Posted June 13, 2013 This is just a guess, but it looks like you dropped the line of code which defined $user_id. $user_id = get_current_user_id(); Quote Link to comment https://forums.phpfreaks.com/topic/279108-how-to-print-all-images-stored-in-a-file-directory/#findComment-1435723 Share on other sites More sharing options...
cyberRobot Posted June 13, 2013 Share Posted June 13, 2013 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 . ' />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/279108-how-to-print-all-images-stored-in-a-file-directory/#findComment-1435724 Share on other sites More sharing options...
Jenksuy Posted June 13, 2013 Author Share Posted June 13, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/279108-how-to-print-all-images-stored-in-a-file-directory/#findComment-1435728 Share on other sites More sharing options...
DaveyK Posted June 13, 2013 Share Posted June 13, 2013 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 . '" />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/279108-how-to-print-all-images-stored-in-a-file-directory/#findComment-1435737 Share on other sites More sharing options...
Jenksuy Posted June 13, 2013 Author Share Posted June 13, 2013 with you code in davey... it prints... bool(false) Quote Link to comment https://forums.phpfreaks.com/topic/279108-how-to-print-all-images-stored-in-a-file-directory/#findComment-1435741 Share on other sites More sharing options...
DaveyK Posted June 13, 2013 Share Posted June 13, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/279108-how-to-print-all-images-stored-in-a-file-directory/#findComment-1435746 Share on other sites More sharing options...
Jenksuy Posted June 13, 2013 Author Share Posted June 13, 2013 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 . '\'))" />'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/279108-how-to-print-all-images-stored-in-a-file-directory/#findComment-1435755 Share on other sites More sharing options...
Solution Jenksuy Posted June 14, 2013 Author Solution Share Posted June 14, 2013 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'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/279108-how-to-print-all-images-stored-in-a-file-directory/#findComment-1435915 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.