Can somebody help me? I have many pictures with current name like : "black_abc","black_bcd","black_cde","white_abc". How can I get only file with filename contains "Black"?
glob will help you find all files containing "Black" in their filename:
$folder = "images"; //the folder containing all your images
$pattern = "*Black*"; //the word you are looking for
$files = glob($folder. '/' . $pattern, GLOB_BRACE);
foreach($files as $filename) {
//Display all pictures
echo "<img src='"$folder . "/" . $filename . "' />";
}
strpos($filename, 'black') !== false
Something like this [Make use of stripos() [Case-Insensitive]
<?php
$files=array("black_1","white_2","black_3");
for($i=0;$i<count($files);$i++)
{
if(stripos($files[i],'black'))
{
echo "Filename is $files[$i]";
}
}
Related
How can I check only file name not extension like jpeg, jpg, doc, xls. and then copy its complete name like example.jpeg or example.doc
and if possible can we store its upper two parent directory like
if example.jpeg stored in
main_dir/second_dir/example.jpeg
so I want to store this path in php variable.
I know I can use glob()
$result = glob ("./uploads/filename.*");
and check $result value.
But I want to store complete file name and possible its two parent directory path.
Below is my code
$filename = '/www/test1/'. $file . '*' ;
if (count(glob($filename)) > 0) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
Edit
Updated query as per luweiqi solution
foreach(glob("/uploads/".$productnumber."/". $productnumber."b.*", GLOB_NOSORT) as $file) {
echo "Filename: " . $file . "<br />";
$image2 = "/uploads/".$productnumber."/".$file;
}
Last letter of image varies like a to f . so can you make some correction in it.
I want to check image is exist on uploads/productnumber/productnumber (a/b/c/d/e/f).jpg or png or jpeg etc. and store that file name in php variable.
You can use file_exists() function.
$pngFile = '/path/to/foo.png';
$docFile = '/path/to/foo.doc';
// Returns TRUE if the file or directory specified by filename exists; FALSE otherwise.
if (file_exists($filename) || file_exists($docFile)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
OR
Use glob function
$files=[];
$result = glob ("/path/to/foo.*");
foreach ($result as $file) {
echo "$file size " . filesize($file) . "\n";
$files[] = $file;
}
echo '<pre>'; print_r($files);
You can use:
<?php
foreach(glob("../directory/*{a,b,c,e,f}.*", GLOB_BRACE) as $file) {
echo "Filename: " . $file . "<br />";
}
?>
This code would get the file name and echo it, you can change it accordingly if you want to assign it to a variable.
I have a text file with 10 paths to files saved on my server, that looks like this:
C:\pictures\123.jpg
C:\pictures\124.jpg
C:\pictures\125.jpg
I'd like to show the pictures from the text file on a website. I can't directly put the links into a php script because the file is dynamically generated and has different paths everytime.
Any ideas on how to do this?
This should work for you:
(Here i use file() to get all lines of the file in an array. After this i simple loop through them and print it as an image path)
<?php
$lines = file("test.txt");
foreach($lines as $line)
echo "<img src='" . trim($line) . "'>";
?>
You can try and draw a neat table of images from your file .
NOTE: I am assuming all your files in the folder are images.
$root = 'C:\pictures';
$files = scandir( $directory );
echo '<table>';
foreach( $files as $file )
{
if(is_file($file))
{
echo '<tr><td><img src="' . $root . $file . '" /></td><td>' . $file . '</td></tr>';
}
}
echo '</table>
Let me know if I helped :)
I don't have opportunity to test this. But I hope this help.
$handle = fopen("images.txt", "r");
if ($handle) {
while (($pathToImg = fgets($handle)) !== false) {
echo "<img src='".$pathToImg."'/>";
}
fclose($handle);
} else {
// error opening the file.
}
can anyone help me to get each file size individually from a local directory ?.
$files = scandir('soft');
foreach($files as $file) {
echo $file . "<br />";
}
From here
$files = scandir('soft');
foreach($files as $file) {
if (!in_array($file,array(".","..")))
{
echo $file . "<br />";
echo filesize('soft/'.$file) . ' bytes';
}
}
Just need to keep in mind that scandir gets only the filenames in that dir, and not the relative path to it. that's why you need to use 'soft/'.$file and not $file
<?
$files = scandir('.');
foreach($files as $file) {
echo filesize($file) . " bytes<br>";
}
?>
use filesize($filename) php function, it will give size in bytes
i am trying to achieve this. I have alot of HTMLs that looks somethinglike this (for example).
<div>
<img src="http://firstsite.com/path/to/img/main.jpg" style="width: 500px; height: 400px;" />
</div>
Now i try to make a php that automatically changes the path of the images to another website, but i also want to download the images and put them into same folder structure. So far i did this:
$input = "c:/wamp/www/primo/input12";
$output = "c:/wamp/www/primo/output12";
$handle = opendir($input);
while (($file = readdir($handle)) !== false) {
if($file != '.' && $file != '..') {
$data = file_get_contents($input . "/" . $file);
$data = str_replace("http://firstsite.com/", "http://secondsite.com", $data);
file_put_contents($output . "/" . $file, $data);
}
}
closedir($handle);
This changes the path but now i need to somehow get into a variable the full path http://firstsite.com/path/to/img/main.jpg in my example in order to download the image.
Is there a way to get the full path of the images while replacing http://firstsite.com/ which is just the begining of the path ?
Thank you in advance, Daniel!
Get only images:
$data = file_get_contents($input . "/" . $file);
preg_match_all('/\<img.*src=\"(.+?)\"/s', $data, $matches);
//go through the match array and download your files
$data = str_replace("http://firstsite.com/", "http://secondsite.com", $data);
file_put_contents($output . "/" . $file, $data);
Get all pathes:
$data = file_get_contents($input . "/" . $file);
preg_match_all('/http\:\/\/firstsite\.com([^\s]+?)/s', $data, $matches);
//go through the match array and download your files
$data = str_replace("http://firstsite.com/", "http://secondsite.com", $data);
file_put_contents($output . "/" . $file, $data);
How about:
preg_match_all('/(http:\/\/firstsite\.com\/[^\s]*)/', $data, $matches);
I want to print all the folder names inside a parent folder. The current issue I am facing is, though I have 400+ folders in a folder only 257 are getting printed. Again, this is not at all issue related with permissions.
Please find my code below:
$newdir = "content/";
$dircnt = 0;
// Open a known directory, and proceed to read its contents
if (is_dir($newdir)) {
if ($dh = opendir($newdir)) {
while (($file = readdir($dh)) !== false) {
$dircnt++;
if(filetype($newdir. $file) == 'dir') {
echo "filename: $file : filetype: " . filetype($newdir. $file) . "dircnt:" .$dircnt. "<br>";
}
}
closedir($dh);
}
}
}
You can use glob() function - returns an array containing the matched files/directories, an empty array if no file matched or FALSE on error.
$filesDirectories = glob($newdir.'*', GLOB_BRACE);
foreach($filesDirectories as $key=>$file) {
echo "$file size " . filesize($file) . "\n";
}
I would use glob:
$newdir = "content/";
$dirs = glob($newdir.'*',GLOB_ONLYDIR);
foreach($dirs as $index=>$dir){
echo "filename ". $dir." filetype ".filetype($newdir.$dir)." dircnt:".($index+1)."<br/>";
}