How can I fetch pictures from a directory into an array? - php

I am looking for a php function to grab images from a directory and load them into an array so that I can output them automatically
For example instead of creating such an array on my own:
$pics = array('../photos/t.png','../photos/t1.png','../photos/t2.png','../photos/t3.png','../photos/t4.png');
It would be much easier if I had a function that fetches all the (.jpg, .png, .jpeg, .bmp) extension files and load them into an array
Your ideas will be very helpful.

You could try something like this:
<?php
$directory = "/var/site/images";
$images = array();
if ($handle = opendir($directory)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
$image = realpath("{$directory}/{$entry}");
array_push($images, $image);
}
}
closedir($handle);
}
?>
This will loop through all the files in your images directory and store their path off to the images array. You could even use a substring function to identify images as you loop through (if you have other filetypes in your images folder) and only add the allowed file types to the array.
This is not all my code, some was borrowed from the PHP manual on readdir().

Related

How to delete file in PHP

I want to delete files in a specific directory in PHP. How can I achieve this?
I have the following code but it does not delete the files.
$files = array();
$dir = dir('files');
while ($file = $dir->read()) {
if ($file != '.' && $file != '..') {
$files[] = $file;
}
unlink($file);
}
I think your question isn't specific, this code must clear all files in the directory 'files'.
But there are some errors in that code I think, and here is the right code:
$files= array();
$dir = dir('files');
while (($file = $dir->read()) !== false) { // You must supply a condition to avoid infinite looping
if ($file != '.' && $file != '..') {
$files[] = $file; // In this array you push the valid files in the provided directory, which are not (. , ..)
}
unlink('files/'.$file); // This must remove the file in the queue
}
And finally make sure that you provided the right path to dir().
You can get all directory contents with glob and check if the value is a file with is_file() before unlinking it.
$files = glob('files/*'); // get directory contents
foreach ($files as $file) { // iterate files
// Check if file
if (is_file($file)) {
unlink($file); // delete file
}
}
If you want to remove files matching a pattern like .png or .jpg, you have to use
$files = glob('/tmp/*.{png,jpg}', GLOB_BRACE);
See manual for glob.

How can I foreach all files in a dictionary?

I am working on a method of saving CPU by loading all my resources into ram before starting the my game server, rather then loading it into RAM on the fly.
So I save all my packet data in a dictionary. The files have random names. How can I foreach every file in the dictionary? I need something like this:
$path = //path to dictionary
foreach(//get dictionary files as $packet){
$filename = //getfile name
if(!isset($this->chunkCache[$filename])){
$this->chunkCache[$filename] = $packet;
}
}
Is this possible?
Check this out : readdir()
This bit of code should list all entries in a certain directory:
$path = //path to dictionary
if ($handle = opendir($path)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "$entry\n";
}
}
closedir($handle);
}

Match string to a filename from folder using php

Hi I wonder if it is possible to match a string to a file from folder using php.
For example I have a folder called uploads and inside, I have different files like image1.png, image2.jpg, doc1.doc, and doc2.pdf.
Assuming I have this code on my php file:
<?php
$string = "image2";
// I need some function to display the image2 on my webpage.
// If string "image2" is found in the uploads folder
// then it should display the image
?>
Thanks!
I think this one should do what you want
$dir = "uploads";//the path to your folder
if(file_exists($dir)){
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if (!is_dir($file)) {
if ($file == "image2"){
// your code
}
}
}
}
}

ajax php check how many folder i had in path src

<?php
header("content-type: application/json");
$files = array();
$dir = "Img/House"; //folder src path
$dirHandle = opendir($dir);
while(($file = readdir($dirHandle) !== false)){
if ($file !== "." && $file !== "..")
{
$files[] = $file;
}
}
echo($files);
//echo json_encode($directoryfiles);
?>
I am using ajax to php to return how many folder I had inside that src path , I can count the folder number on ajax , but something wrong with my php file , it seem wont check how many folder I have.
My intention is to use ajax and php check how many folder i have and push those name into the array $files. Can anyone help me take a look. I have no experience one this.
If you only want to return the number of directories in the given path, you can easily use count and glob, see below
// this is not needed unless you output json
// header("content-type: application/json");
$dir = "Img/House"; //folder src path
$dirs = glob($dir . "/*",GLOB_ONLYDIR);
print count($dirs);
// or directly
// print count(glob($dir . "/*",GLOB_ONLYDIR));
// if glob returns the current and parent dirs, "." and ".."
// just remove 2 from the count
// test by doing
print_r($dirs);
// then
print $count($dirs)-2;

Get a list of files and filenames without an extention using php

I'm trying to make a jQuery slider that automatically loads all the images in a specified folder. So I'm using a small PHP script that makes a list of all the files in that directory. For the captions of the slider I wanted to use the filename (without extension).
I'm using the following script, using PHP. It can list all the files with the extensions, but I can't find a way to also display the filename (for the captions) without the extensions.
Anyone an idea?
Thanks in advance!
<?
$path = "img";
$dir_handle = #opendir($path) or die("Unable to open $path");
while ($file = readdir($dir_handle)) {
if($file == "." || $file == ".." || $file == "index.php" )
continue;
echo "$file";
}
closedir($dir_handle);
?>
Here you have more OO way:
$iterator = new DirectoryIterator($directory);
foreach ($iterator as $fileinfo) {
if ($fileinfo->isFile()) {
$fileinfo->getBasename('.' .$fileinfo->getExtension());
}
}
You can get the filename without its extension using pathinfo():
$filename = pathinfo( $file, PATHINFO_FILENAME);

Categories