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
}
}
}
}
}
Related
I have write script to the search string in pdf files. I used script from this link: link.
I have problem, because my script not search in all files pdf, only search in first file (sorted by name)
My script:
foreach (glob("storage/cvs/*.pdf") as $file) {
// $files = 'storage/cvs/d08540965a_2017-12-06.pdf';
$this->pdf2text->setFilename($file);
$this->pdf2text->decodePDF();
$convert2Txt = $this->pdf2text->output();
if (stripos($convert2Txt, $query) !== false) {
echo $resultQuery = "tak ".$file."</br>";
} else {
echo $resultQuery = "nie znaleziono ".$file."</br>";
}
}
And I don't know how I can get name files where found string.
This is how I do it:
$dir = "/path/to/your/directory/"; // Set your directory path here
if (is_dir($dir)){ // Check its a directory
if ($dh = opendir($dir)){ // Open the directory
while (($file = readdir($dh)) !== false){ // Read the files
if (substr($file, -3)=='pdf') { // Check filename ends 'pdf'
// Your code here
$file will be set as the filename
please i am beginner in php and i want to read images in a folder
I have pictures with names like:
image_1_0_0_0.png
image_1_1_0_0.png
image_1_1_1_0.png
.........
.....
image_2_0_0_0.png
image_2_1_0_0.png
image_3_0_0_0.png
I want to assign a color (imagefilter) to the images that starts with image_1 _..... and another color for image_2 _... and another color to images_3 ....
then I want to read only the last image and retrieve only the number 3, the first number left
I want to know how to do this please.
To read the content of a directory:
$dir = "/images/";
$color = "#000000";
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
echo "Filename: ".$file."<br>";
if($file[7]=='1') $color="#FF0000";
elseif ($file[7]=='2') $color="#00FF00";
elseif ($file[7]=='3') $color="#0000FF";
else $color="#000000";
// do something with the image
}
closedir($dh);
}
}
I don't understand your last question.
I have 800+ text files in directory called documents. All have txt extension. from 100 to 500kb each file with text and html tags. Now i want import all files to my database. It's possible to do this with php? Also want to delete file.
I know only how to get filenames from dir but how to take content and insert into database?
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo "filename: .".$file."<br />";
}
closedir($dh);
}
}
You should be able to load the contents of the file into a string and then write the string to sql
This will give you the contents of the file:
$fileContents= file_get_contents($file);
<?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;
I need to read only pdf files in a directory and then read the filename of every files then I will use the filename to rename some txt files. I have tried using only eregi function. but it seems cannot read all I need. how to read them well?
here's my code :
$savePath ='D:/dir/';
$dir = opendir($savePath);
$filename = array();
while ($filename = readdir($dir)) {
if (eregi("\.pdf",$filename)){
$read = strtok ($filename,"."); //get the filenames
//to rename some txt files using the filenames that I get before
//$testfile is text files that I've read before
$testfile = "$read.txt";
$file = fopen($testfile,"r") or die ('cannot open file');
if (filesize($testfile)==0){}
else{
$text = fread($file,55024);
fclose($file);
echo "</br>"; echo "</br>";
}
}
More elegant:
foreach (glob("D:/dir/*.pdf") as $filename) {
// do something with $filename
}
To get the filename only:
foreach (glob("D:/dir/*.pdf") as $filename) {
$filename = basename($filename);
// do something with $filename
}
You can do this by filter file type.. following is sample code.
<?php
// directory path can be either absolute or relative
$dirPath = '.';
// open the specified directory and check if it's opened successfully
if ($handle = opendir($dirPath)) {
// keep reading the directory entries 'til the end
$i=0;
while (false !== ($file = readdir($handle))) {
$i++;
// just skip the reference to current and parent directory
if (eregi("\.jpg",$file) || eregi("\.gif",$file) || eregi("\.png",$file)){
if (is_dir("$dirPath/$file")) {
// found a directory, do something with it?
echo " [$file]<br>";
} else {
// found an ordinary file
echo $i."- $file<br>";
}
}
}
// ALWAYS remember to close what you opened
closedir($handle);
}
?>
Above is demonstrating for file type related to images you can do the same for .PDF files.
Better explained here