I have been making a blog, but I have a problem. I can't display image from folders images. I need to display one image every time when name and comment put on the page. Every time another image and need to stay on page.
Here is link
http://slimhamdi.net/lina/demos/blog-post-dark.html?name=hhhhhhh&email=hh%40hotmail.com&comment=h&send=
I need same like this blog, I would like to avoid MySQL. Any help will be appreciated.
<?php
$images = array("user1.jpg", "user2.jpg", "user1.jpg");
$images = glob('images/*');
shuffle($images );
foreach($images as $image ) {
break;
}
?>
<img src="<?php echo "$image"; ?>" />
You are overwriting variables:
<?php
$images = array("user1.jpg", "user2.jpg", "user1.jpg");
$images = glob('images/*'); // Overwrites the previous $images
That way the $images in the foreach is something else.
There is also no need to use a break in the foreach loop
<?php
$images = array("user1.jpg", "user2.jpg", "user1.jpg");
shuffle($images );
foreach($images as $image ) {
echo '<img src="'.$image.'" />';
//echo '<img src="images/'.$image.'">';// With file path
}
why do you use break in foreach?
you should echo the img tag in foreach
if your images are in images folder, you shouldn't you glob, and instead you can concat it to path.
<?php
$images = array("user1.jpg", "user2.jpg", "user1.jpg");
shuffle($images);
foreach($images as $image ) {
echo "<img src='images/{$image}' />";
}
?>
Related
I have a folder called loop-images which has a set of images. I also have the php loop below:
foreach ($feed as $feed_id => $feed_title){
echo '<img src="/loop-images/01.jpg" border="0"><br>';
echo ''.$feed_title.'';
}
In the loop I have a fixed image, However I am looking to use the image dynamically from the loop-images folder. So basically, on the first loop it should use the first image and so on. If it runs out of images in the loop then it starts from the first image again.
Any ideas how I can achieve this?
Thanks
Here is something which you can try
$directory = "/loop-images"; // path to loop images folder
$images = glob($directory . "*.jpg");
$imagescounter = 0; foreach ($feed as $feed_id => $feed_title){
if($imagescounter>count($images)){
$imagescounter = 0;
}
echo '<img src="/loop-images/'.$images[$imagescounter].'.jpg" border="0"><br>';
echo ''.$feed_title.'';
$imagescounter++;
}
Maybe what you're trying to accomplish is something like that.
Using the scandir function and a regex to match all images using preg_match php function
foreach(scandir('/path/to/loop-images/') as $key => $file) {
if (preg_match('/[\s\S]+\.(png|jpg|jpeg|tiff|gif|bmp)/iu', $file)) {
echo '<img src="loop-images/' . $file . '" border="0"><br>';
}
}
I have an uploader that make me upload files to the server.
I would like that when I upload an image, this image is automatically visible in a page of the site. Eg, if I have a site of a music band, if I upload from an administrator password protected page the poster of the next concert, and automatically in the page "events" I can see that poster.
In the same way if I upload (from another uploader) the text for that concert, this text appears in the event page in a "description of the concert" div.
Is it possible?
thank you!
If you need to do that, then using the database and store the uploaded items in its coloumn is the better way..
But to answer your question
You can do something like this.
Step 1 :
Read all the files in your directory
$dir = 'up/';
$files = scandir($dir);
Step 2 :
Do a foreach and print it in your file (If you need to display an image it can be possible and you can have links over there )
$i = 1;
foreach ($files as $key)
{
echo $i.' - '.$key.'<hr>';
echo "<a href='up/".$key."'><img src ='up/".$key."'>".$key."</a>";
$i++;
}
So you can have something like this..
<?php
$dir = 'up/';
$files = scandir($dir);
echo '<h1>List of Uploaded Files</h1> <br><hr>';
$i = 1;
foreach ($files as $key)
{
echo $i.' - '.$key.'<hr>';
echo "<a href='up/".$key."'><img src ='up/".$key."'>".$key."</a>";
$i++;
}
echo 'End of Files';
?>
Note :
I am using the directory name as up you can have it any name of your own.
If you have this file as list.php then it should have a sub folder named as up and you should have files inside it.. You can have image or text file as you need.
Update :
If you just want to list the files, then you just need to echo it
<?php
$dir = 'up/';
$files = scandir($dir);
echo '<h1>List of Uploaded Files</h1> <br><hr>';
$i = 1;
foreach ($files as $key)
{
echo $i.' - '.$key.'<hr>';
$i++;
}
echo 'End of Files';
?>
Here's the eval link
Update 2 :
As the OP needs to have an anchor tag and wants to remove the directory level Here's the updated code
<?php
$dir = 'up/';
$files = scandir($dir);
echo '<h1>List of Uploaded Files</h1> <br><hr>';
$i = 1;
foreach ($files as $key)
{
if ($i>3)
{
$j = $i-3;
echo $j."<a href='up/".$key."'>".$key."</a><hr>";
}
$i++;
}
echo 'End of Files';
?>
Here's the Eval
I am trying to echo out all of the images in a folder directory with a couple of exceptions/ignores.
This is working ok apart from it also echoes out a blank photo for every photo it echoes out?
why is this happening can someone please show me where I'm going wrong thanks.
<?php
$dirname = "./data/photos/".$profile_id."/";
$images = scandir($dirname);
$ignore = Array("_cover.jpg", "_default.jpg");
foreach($images as $curimg){
if(!in_array($curimg, $ignore)) {
echo "<img src='./data/photos/".$profile_id."/$curimg'/ class=\"profile_photos\"><br>\n";
};
}
?>
You appear to have an extra slash after your image source:
echo "<img src='./data/photos/".$profile_id."/$curimg'/ class=\"profile_photos\"><br>\n";
//--------------------------------------------------------^ here
This may be interefering with how the browser parses the DOM and causing an extra image to appear.
Also, small suggestion, try using this line instead:
echo "<img src='".$dirname.$curimg."' class=\"profile_photos\"><br>\n";
You should ensure that $curimg is actually a jpg file:
if(!in_array($curimg, $ignore) && preg_match("/\.jpg$/i", $curimg)) {
scandir returns not just files but subdirectories, including . and ...
I've been having trouble properly outputting the directory for images in a slider using Custom Fields. The code I'm using is following:
<div class="container">
<?php
//path or directory where the images are stored
$directory = "echo get_post_meta($post->ID, 'directory', true)";
//read all files with a .jpg extension
$images = glob($directory . "*.jpg");
//output the required HTML code to display the images in the gallery
foreach($images as $image)
{
echo '<div class="content"><div><img src="'.$image.'" width="120" height="80" alt="this is a test" class="thumb" /></div></div>'."\n";
}
?>
</div>
The value I want to dynamically output is the $directory = "", where it would normally be something like $directory = "images/product1/". I have my custom field 'directory' set as images/product1/. Any ideas? Thanks for the help!
It looks like the issue is with this line:
$directory = "echo get_post_meta($post->ID, 'directory', true)";
will result in trying to glob like this:
glob("echo get_post_meta($post->ID, 'directory', true)*.jpg");
which is obviously invalid.
Instead you should actually be calling the get_post_meta() function
$directory = get_post_meta($post->ID, 'directory', true);
<?php
/* settings */
$image_dir = 'gallery/';
$per_column = 3;
$count=0;
if ($handle = opendir($image_dir)) {
while (false !== ($file = readdir($handle)))
{
if ($file != '.' && $file != '..')
{
if(strstr($file,'-thumb'))
{
$files[] = $file;
}
}
}
closedir($handle);
}
if(count($files))
{
foreach($files as $file)
{
$count++;
echo '<a class="thumbnail" rel="one-big-group" href="',$image_dir,str_replace('-thumb','',$file),'"><img src="',$image_dir,$file,'" width="100" height="100" /></a>';
if($count % $per_column == 0) { echo '<div class="clear"></div>'; }
}
}
else
{
echo '<p>There are no images in this gallery.</p>';
}
?>
How can I add captions to each of the images?
Thank you very much for the answer!
Your code is traversing the files within the specified directory looking for files containing ...-thumb... in the filename, appending them to an array, and then looping over the array to generate HTML for displaying the thumbnail image gallery.
Adding additional information to something like this, given the very limited code you've provided, can be done in any number of ways
You could implement a database with a column for each filename and a
related column containing captions/descriptions. This might be more
trouble than it's worth depending on what you're trying to achieve.
You could use a flat file which you could parse line by line (instead of traversing the
folder for ...-thumb... images), containing some format like
file1-thumb.png|some caption here
file2-thumb.png|some caption here
file3-thumb.png|some caption here
...
You could includ a small caption in the filename itself, and parse/format the caption from the filenames. This would probably be the quickest route, but most limiting in terms of flexibility in the length/characters allowed for the captions.
- file1-thumb--some_caption_here.png
- file2-thumb--some_caption_here.png
- file3-thumb--some_caption_here.png
To actually add the caption to the generated HTML, you can use a title attribute as #rockerest suggested, however I'd personally add such a caption to to the image itself, as that is what the caption is describing (not the link)
<img src="..." title="..." ... />
UPDATE
To answer your comment (this provides better formatting for code), Let's say we have a file with name file1-thumb--some_description_here.jpg, you can parse and format the caption with preg_replace
$filename = 'file1-thumb--some_description_here.jpg';
$caption = preg_replace(array('/^.+-thumb--/', '/\.(jpg|jpeg|gif|png|bmp)$/', '/_/'), array('','',' '), $filename);
$caption is now some description here
The html title offers a "caption" when the mouse is hovered over the image.
foreach($files as $file)
{
$count++;
echo '<a title="',$caption,'" class="thumbnail" rel="one-big-group" href="',$image_dir,str_replace('-thumb','',$file),'"><img src="',$image_dir,$file,'" width="100" height="100" /></a>';
if($count % $per_column == 0) { echo '<div class="clear"></div>'; }
}