I have a script that goes through a directory that has 3 images
$imglist='';
$img_folder = "path to my image";
//use the directory class
$imgs = dir($img_folder);
//read all files from the directory, checks if are images and ads them to a list
while ($file = $imgs->read()) {
if (eregi("gif", $file) || eregi("jpg", $file) || eregi("png", $file))
$imglist .= "$file ";
}
closedir($imgs->handle);
//put all images into an array
$imglist = explode(" ", $imglist);
//display image
foreach($imglist as $image) {
echo '<img src="'.$img_folder.$image.'">';
}
but the problem that I am having is it display a 4th img with no image.. yet I only have 3 image in that folder.
There is no need to build a string of images and then explode that string into an array of images instead just add the images directly to an array as Radu mentioned.
Here is the corrected code:
$imglist = array();
$img_folder = "path to my image";
//use the directory class
$imgs = dir($img_folder);
//read all files from the directory, checks if are images and adds them to a list
while ($file = $imgs->read()) {
if (eregi("gif", $file) || eregi("jpg", $file) || eregi("png", $file)){
$imglist[] = $file;
}
}
closedir($imgs->handle);
//display image
foreach($imglist as $image) {
echo '<img src="'.$img_folder.$image.'">';
}
You'll have a space at the end of the $imglist string, which explode() will turn into an empty element. Trim the string:
$imglist = explode(" ", trim($imglist));
Or better yet, just add them to the $imglist array in the first place, instead of making a string and exploding it:
$imglist = array();
/* ... */
$imglist[] = $file;
ereg() is deprecated. You'd probably be better off with:
chdir($img_folder);
$imgs = glob('*.jpg *.gif *.png');
foreach ($imgs as $img) {
echo "<img src=\"{$img_folder}/{$img}\">";
}
glob() does wildcard matching pretty much the same way as most Unix shells do.
Use [glob()][1] function
<?php
define('IMAGEPATH', 'path to my image/'.$imglist.'/');
foreach(glob(IMAGEPATH.'*.jpg') as $filename){
echo '<img src="'.$filename.'" alt="'.$album.'" />';
?>
[1]: http://www.php.net/manual/en/function.glob.php
Related
I have a folder called allfiles, and there are some files in this folder, such as
1212-how-to-sddk-thosd.html
3454-go-to-dlkkl-sdf.html
0987-sfda-asf-fdf-12331.html
4789-how-to-fdaaf-65536.html
I use scandir to list all files, and now I need to find the file by with keywords, example to-dlkkl is the keyword, and I will get the file 3454-go-to-dlkkl-sdf.html.
Glob seems not work, and opendir and readdir are not work well, any ideas?
Use loop foreach and strpos function:
$files = scandir('allfiles');
foreach ($files as $file) {
if (strpos('to-dlkkl', $file) !== false) {
//file found
}
}
I wonder why glob() function not working for it?
The below code should work I guess,
$existing_dir = getcwd();
// path to dir
chdir( '/var/www/allfiles/' );
foreach( glob( '*to-dlkkl*.html' ) as $html_file ) {
echo $html_file . '<br />';
}
chdir( $existing_dir );
you can use strstr to get actual file
$allfiles = scandir('./');
foreach ($allfiles as $file) {
if (strstr($file, 'to-dlkkl')) {
echo "file found"; //do what you want
}
}
If You want to search specific file under directory then you can use preg_match() .
<?php
if ($handle = opendir('/var/www/html/j')) { // here add your directory
$keyword = "index.php"; // your keyword
while (false !== ($entry = readdir($handle))) {
// (preg_match('/\.txt$/', $entry)) {
if (preg_match('/'.$keyword.'/i', $entry)) {
echo "$entry\n";
}
}
closedir($handle);
}
?>
I have used following php code to get all images names from relevant directory.
$dir = "images";
$images = scandir($dir);
$listImages=array();
foreach($images as $image){
$listImages=$image;
echo ($listImages) ."<br>";
}
This one works perfectly. But I want to get all images file names within all sub directories from relevant directory. Parent directory does not contain images and all the images contain in sub folders as folows.
Parent Dir
Sub Dir
image1
image2
Sub Dir2
image3
image4
How I go through the all sub folders and get the images names?
Try following. To get full directory path, merge with parent directory.
$path = 'images'; // '.' for current
foreach (new DirectoryIterator($path) as $file) {
if ($file->isDot()) continue;
if ($file->isDir()) {
$dir = $path.'/'. $file->getFilename();
$images = scandir($dir);
$listImages=array();
foreach($images as $image){
$listImages=$image;
echo ($listImages) ."<br>";
}
}
}
Utilizing recursion, you can make this quite easily. This function will indefinitely go through your directories until each directory is done searching. THIS WAS NOT TESTED.
$images = [];
function getImages(&$images, $directory) {
$files = scandir($directory); // you should get rid of . and .. too
foreach($files as $file) {
if(is_dir($file) {
getImages($images, $directory . '/' . $file);
} else {
array_push($images, $file); /// you may also check if it is indeed an image
}
}
}
getImages($images, 'images');
I have a php script which grabs all of the images in a set directory. The script works without fault. My issue is that I want it to only get none retina images. All of the retina images are named in the format 'image1#2x.jpg' whereas all of the regular images are named in the format 'image1.jpg'. With this in mind I want my script to discount any image that contains '#2x' in it's name.
My script is:
<?php
$directory = 'images/';
$dh = opendir($directory);
while (false !== ($filename = readdir($dh)))
{
$files[] = $filename;
};
$images = preg_grep ('/(\.gif|\.GIF|\.jpg|\.jpeg|\.JPG|\.JPEG|\.png|\.PNG)$/i', $files);
foreach ($images as $image)
{
echo '<img src="'.$directory.$image.'" alt="">';
}
?>
The problem is I don't have the vocabulary to know what to search for in Google so if anyone could point me in the right direction I would appreciate it.
Thanks
I would use stripos put this conditional in your foreach loop
if(stripos($image,'#2x') === false){
echo '<img src="'.$directory.$image.'" alt="">';
}
You can make your script a little shorter by using:
$directory = 'images/';
$allImages = glob($directory.'*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}', GLOB_BRACE);
$nonRetinaImages = preg_grep('/#2x/i', $allImages, PREG_GREP_INVERT);
foreach ($nonRetinaImages as $image)
{
echo '<img src="'.$image.'" alt="">';
}
I need to list all files for example mp4 or avi in my folder /Files and relative subdirectories and after that insert into <a href={$filename}><\a> tag so I need a array i suppose.
I tried with find command but I receive a string and not a Array so I've to split the string and this isn't practical.
Any suggestion?
or use class RecursiveDirectoryIterator - For example :
$dir_iterator = new RecursiveDirectoryIterator(dirname(__FILE__));
$iterator = new RecursiveIteratorIterator($dir_iterator);
foreach ($iterator as $filename)
{
if (dirname($filename) != dirname(__FILE__))
{
if(is_file($filename)) {
$path_parts = pathinfo($filename);
if($path_parts['extension'] == 'mp4' )
{
print ''.basename($filename)."<br />";
}
}
}
}
<?php
$dir ="/Files";
$files = scandir($dir);
foreach($files as $file) {
$fullname = "/Files/" . $file;
echo '<a href='.$fullname.'>File</a>;
}
This should work for you.
my code is not extracting all of the files inside the directory and i donde know why, i dont have any extension restrictions or anything, ok so this is my code what am i doing wrong? there are 24 images with jpg and png extensions and only 13 are detected when i print_r($arr)
:
<?php
function loadimages($dir) {
if(substr($dir, -1) != "/") $dir .= "/";
$rootdir = $_SERVER["DOCUMENT_ROOT"];
$fulldir = $rootdir."/".$dir;
$dir = opendir($fulldir);
$arr = array();
while(readdir($dir)) {
$arr[] = readdir($dir);
}
echo "<h1>".count($arr). "</h1><br />";
foreach($arr as $img) {
echo "<img src='/pages/course-images/{$img}' />";
}
}
loadimages("pages/course-images");
?>
I would use glob() instead.
$images = glob('*.{png,jpg}', GLOB_BRACE);
print_r($images);
http://php.net/manual/en/function.glob.php
It doesn't work because while(readdir($dir)), then you read one value and skips one step forward to the next file. The correct way would be this, and it's explaind in the manual.
while(false !== ($entry = readdir($dir))) {
$arr[] = $entry;
}
http://php.net/manual/en/function.readdir.php
But glob is better, now it only returns images.