Display Images From A Folder (slideshow) from external folder / URL - php

I have the code below
<?php
$directory = 'images/slideshow';
try {
// Styling for images
echo '<div id="myslides">';
foreach ( new DirectoryIterator($directory) as $item ) {
if ($item->isFile()) {
$path = $directory . '/' . $item;
echo '<img src="' . $path . '"/>';
}
}
echo '</div>';
}
catch(Exception $e) {
echo 'No images found for this slideshow.<br />';
}
?>
copied from this topic:
Display Images From A Folder (slideshow)
I would like to know if its possible to load the images externally instead, or from a specific directory of a URL? Thanks in advance.

It is possible, but you would need to know the urls of each photo. Though that would be easy if you can make them follow a certain pattern I.e. www.example.com/dir/photo1, www.example.com/dir/photo2.
The external server you point must allow remote linking as well.
Then it's just a matter of making the links:
for ($i = 1; $i < NUMBER_OF_PHOTOS; ++$i) {
echo '<img src="http://www.example.com/dir/photo'.$i.'"/>';
}

Related

PHP image load function reads from the controller instead of domain/folder/to/image

Dear stackoverflow community,
Loading images from function works when I am in the indexAction (index of the controller, e.g gallery). When I want to load images from function while in a sublink of the main controller (e.g gallery/family) it doesn't load the images.
So when going to: domain.com/gallery everything is working.
When going to domain.com/gallery/family the images will be trying to find "domain.com/gallery/app/assets/image/gallery/family/" instead of "domain.com/app/assets/image/gallery/family/".
Image location is at "app/assets/image/gallery/family/" then running this function:
public function insertFamilyImages()
{
$directory = 'app/assets/image/gallery/family/';
$extension = '.jpg';
$html = '';
if ( file_exists($directory) ) {
foreach ( glob($directory . '*' . $extension) as $file ) {
$html .= '<li>';
$html .= '<img src="' . $file . '" alt="">';
$html .= '</li>';
}
} else {
echo 'directory ' . $directory . ' doesn\'t exist!';
}
return $html;
}
I am stuck at this and google doesn't help me. Neither I can find a similiar post on stackoverflow regarding this issue.
Thanks in advance,
Caleb
From looking at this it looks like when you create your html you point the img src to the file system path and not the uri. This html will be loaded in a remote browser and the browser can't access a remote file system. It needs an uri.
E.g.
Let say your domain name is example.com and en example image is called eg.png
Your image is located on your disk at app/assets/image/gallery/family/eg.png (relative).
However your images are accessible externally at https://example.com/gallery/family/eg.png.
You code would look something like this:
public function insertFamilyImages()
{
$directory = 'app/assets/image/gallery/family/';
$uri = 'https://example.com/gallery/family/';
$extension = '.jpg';
$html = '';
if ( file_exists($directory) ) {
foreach ( glob($directory . '*' . $extension) as $file ) {
$file = $uri.basename($file);
$html .= '<li>';
$html .= '<img src="' . $file . '" alt="">';
$html .= '</li>';
}
} else {
echo 'directory ' . $directory . ' doesn\'t exist!';
}
return $html;
}
You'll have to adjust this code to fit your case, but it should point you in the right direction where the problem lies. I used a complete uri here to point to the image to illustrate the point, but this could be made relative as well to the web root.
Never load / read / import any file with relative path. Otherwise your code will be faulty unpredictable.
Always use absolute path. In first php file like index.php which is entry point to your application define global root path like this:
define('ROOT', __DIR__);
then use ROOT as your constant at all files access/manipulation in your application.
$directory = ROOT.'/app/assets/image/gallery/family/';
You will never have problems with paths.

Showing a list of images using PHP

I have got a directory which contains several folders with images. What I want to do is to read all the images using php script and showing them with div in a browser. The code I have tried to implement is the following:
<?php
function listFolderFiles($dir)
{
echo '<ol>';
foreach (new DirectoryIterator($dir) as $folder) {
if (!$folder->isDot()) {
// echo '<li>' . $folder->getFilename() . '<br>';
if ($folder->isDir()) {
$user = $dir . '/' . $folder;
foreach (new DirectoryIterator($user) as $file) {
if ($file != '.' && $file != '..') {
// echo '<li>' . $file->getFilename();
$image = $user . '/' . $file;
echo $image . '<br>';
echo '<div>';
echo '<img src="' . $image . '" width="500" height="500" alt="';
echo '"/>';
echo '</div>';
}
}
}
echo '</li>';
}
}
echo '</ol>';
}
listFolderFiles('images');
The command echo $image.'<br>'; prints the names of all images. With the following command I manage to create div, however without the images been displayed inside them:
echo '<div>';
echo '<img src="' . $image . '" width="500" height="500" alt="';
echo '"/>';
echo '</div>';
Am I doing something wrong? The $image paths are correct.
EDIT: I move the folder of images in the same folder with the php file in the wamp folder. Now for example the image file is the following images/01virarias/1_.jpg. I change the call of my function as listFolderFiles('images');. However I am getting the same empty divs.
You need to use an URL. It seems you are using local files. Your browser is the 'problem'. Although it is a matter of security.
The images should be in your server environment.
And it seems you forgot a <li> start tag.
if your path is correct do a inspect element on your browser i think it's quoting issue try
echo '<img src="'. $image .'" width="500" height="500" alt=""/>';
You can use images path relative/absolute like
relative :- ../images/your_image
absolute :- http://localhost/your_image_path

php code to display images from directory not working

i have this code to display images, where each user has his own, i'll comment it to save your time
<?php
session_start();
$name=$_SESSION['valid_user']; //saved current username in variable
$loc="./uploads/"; //location of image directory
$path=$loc.$name; //current user's folder to save his images
echo $path."<br>"; //i used this to make sure the path is ok, only for testing
if(is_dir($path)) //if directory exists, show it exists,otherwise show it
{ //doesnt exists
echo "<br>exists";
}
else
{
echo "<br>not exists";
}
$files = glob($path."/");
for ($i=1; $i<count($files); $i++)
{
$num = $files[$i]; //picture number
print $num."<br />";
echo '<img src="'.$path.'" alt="random image" height="100" width="100"/>'."<br /><br />";
} //shows the picture till the last one
?>
the output that i get is this this
./uploads/user_name
exists
but it does not show the images, even though the folder is not empty (upload script works fine).
EDIT; solved it (low rep, cant answer my own question).
got it. For anyone who cares, this line here
echo '<img src="' . $path . '/' . $files[$i] . '" <!-- etc --> />';
wasn't working because i added $files, which already contained the path, and it was giving input to img src as
/uploads/username/uploads/username
so that was two times the same path.Upon removing $path, and using just
<img src="' . $files[$i] . '"
did the trick. Thank you all for your help.
I think you need to pass a wildcard path to glob: glob($path . '/*'). You are also not printing the filename in the image source attribute:
echo '<img src="' . $path . '/' . $files[$i] . '" <!-- etc --> />';
Also, your $num is actually the filename, not the picture number - that is $i. You could really simplify that loop using the foreach construct:
foreach($files as $filename) {
// etc
}
you need to add a pattern for using glob afaik
$files = glob($path."/*.*"); // all files
$files = glob($path."/*.jpg"); // all jpgs etc.pp
foreach($files as $idx => $file)
{
$num = $idx+1; //idx starts with 0 so we add one here
print $num."<br />";
echo '<img src="'.$path.'/'.$file'" alt="random image" height="100" width="100"/>'."<br /><br />";
}

path to image folder on server

i have a wordpress inside Public html folder on server.
i want to dispaly images from the folder Public_html--->Trial-->Wordpress_site-->uploads
below is page.php code
<?php
$directory = dirname(__FILE__).'/uploads';
echo $directory;
try {
// Styling for images
foreach ( new DirectoryIterator("/" . $directory) as $item ) {
if ($item->isFile()) {
echo "<div class=\"expand_image\">";
$path = "/" . $directory . "/" . $item;
echo $path;
echo "<img src=/"". $path . "\" width=861 height=443 />";
echo "</div>";
}
}
}
catch(Exception $e) {
echo 'No images found for this player.<br />';
}
?>
The images arent getting displayed..
anyone knows about the same??
edit1
I think there is problem in this sentence
echo "<img src=/"". $path . "\" width=861 height=443 />";
is it?
edit2
//home/softwar2/public_html/Pradnnya_blog/wordpress_site/wp-content/themes/deep-red/our_results/4thpanelfinal.jpg
is the path that i get when echoed.
__FILE__ gives you the path of the current file on the filesystem; however, when you visit the webpage and you see a link in the tag, you'll try to access that as a URL instead of a file. For this, you might find $_SERVER['PHP_SELF'] useful, or another one of the $_SERVER elements. It might be better to have the URL in a configuration file though, because $_SERVER may sometimes not be set.
Good catch, there's a bit of a syntax error:
echo "<img src=\"". $path . "\" width=861 height=443 />";
You'll want to use the backslash to escape the double quote.

List files on directory and build the html

I'm making a page with some flash games, but, since in the past will be many games it will be very bored to add all those games one by one on the site. I know that using PHP we can get the list of the files in the directory and I know that, using this method, I can display all the games on the directory to the visitors page but, I was thinking if you can help me to add an image to each game that corespond with the name of the game. Ex:
In "games" folder we will have games: play1.swf, play2.swf, play3.swf
and, in "images" directory we will have images: play1.jpg, play2.jpg, play3.jpg.
To display the list of the games, can we make a combination of jpg files with swf files? Play1.jpg will be the image of play1.swf game and, when a visitor clicks on the image, will be redirect to a page named ex: playgames.php?play1
assuming this directory structure:
/foo/images
game1.jpg
game2.jpg
/foo/games
game1.swf
game2.swf
Something like this:
<?php
$dir = "/foo";
$files = new DirectoryIterator($dir . DIRECTORY_SEPARATOR . 'games');
$games = array();
foreach($files as $file){
if (!$file->isDot()) {
$potential_image_location = $file->getPath() . '/../images/' . $file->getBasename('.swf') . '.jpg';
if (file_exists($potential_image_location)) {
$games[$file->getBaseName()] = array(
'game_path' => $file->getPathname(),
'image_path' => realpath($potential_image_location),
);
}
}
}
if (!count($games)) {
die("Sorry, couldn't find any game / image combos");
}
?>
<table>
<?php
foreach ($games as $game_name => $info) {
echo '<tr><td><img src="' . htmlentities($info['image_path']) . '" alt="' . htmlentities($game_name) . '"></td>' . PHP_EOL;
echo '<td>' . $info['game_path'] . '</td></tr>';
}
?>
</table>
its all about using opendir in a clever way, heres the docs on it http://php.net/manual/en/function.opendir.php

Categories