PHP Search string in multiple files pdf and get name this files - php

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

Related

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
}
}
}
}
}

Require to display limited number of file names from directories -php

For the below code I have multiple directories and files. I can display one filename per directory(Which is good with the "BREAK").
<?php
$dir = "/images/";
$i=0;
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
echo "filename:" . $file . "<br>";
break;
//---- if ($i>=5) { break; }
}
closedir($dh);
}
}
?>
With
if ($i>=5) { break; } I can still display 5 filenames but it reads only one directory.
I want to display at least 5 file names from all directories, how can I do it?
Use the scandir function.
array scandir ( string $directory [, int $sorting_order = SCANDIR_SORT_ASCENDING [, resource $context ]] )
or
If you are using unix you could also do a system call and run the following command.
ls /$dir | head -5
$dir is the directory and -5 is the number filenames in the directory.
Since you said that you have multiple directory's, I rewrote your code a bit:
(Here I first loop through all directory's with array_map() then I get all files from each directory with glob(). After this I just limit the files per directory with array_slice() and at the end I simply print all file names)
<?php
$directorys = ["images/", "xy/"];
$limit = 3;
//get all files
$files = array_map(function($v){
return glob("$v*.*");
}, $directorys);
//limit files per directory
$files = array_map(function($v)use($limit){
return array_slice($v, 0, $limit);
}, $files);
foreach($files as $directory) {
echo "<b>Directory</b><br>";
foreach($directory as $file)
echo "$file<br>";
echo "<br><br>";
}
?>
You don't have to break it, you can just skip it. And in doing so, you have to use continue instead.
$dir = "/images/";
$i=0;
// 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 ($i>=5)
continue;
}
closedir($dh);
}
}
Here is also another scenario. Because you mentioned that you have many directories but you only show one main directory, I am guessing that the directories you've mentioned were inside the /images/ directory.
$dir = "images/";
$i=1;
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
$j=1;
if (is_dir($file)) {
if ($internalDir = opendir($file)) {
while (($internalFile = readdir($internalDir)) !== false) {
echo $file."->filename: ".$internalFile."<br>";
if ($j>=5)
continue;
$j++;
}
closedir(opendir($file));
}
} else {
echo "filename:" . $file . "<br>";
if ($i>=5)
continue;
$i++;
}
}
closedir($dh);
}
}
Read more about continue here: http://php.net/manual/en/control-structures.continue.php

Read only pdf files and get the filename of that pdf files in a directory

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

php directory read and delete existing files

I am trying to make sure that when the user uploads a profile picture, there can only be one image within the dir "profile_pic". I am openning and reading the dir with a while loop but the #unlink is not working. Below is the php code:
$directory = "uploads\\".$id."\images\profile_pic\\";
if (glob($directory . "*.jpg") != false) {
$filecount = count(glob($directory . "*.jpg"));
if ($filecount > 0) {
//delete exiting pics in folder profile_pic
$handle = opendir($directory);
while ($handle && ($file = readdir($handle)) !== false) {
if ( unlink($entry)) {
$msg .= "File Deleted";
}
}
closedir($directory);
}
}
Sorry, where did you define $entry - did you ever assign a value to it? And why do you make exactly the same call to glob() twice instead of catching the result of the first call in a variable? And why bother to opendir() to list files when you have already glob()ed and obtained that list of files? Especially when you don't apply the same *.jpg filter to the files you are deleting...
Just do this:
// Forward slashes work on Windows too (in PHP, at least)
$directory = "uploads/".$id."/images/profile_pic";
if (($existing = glob($directory . "/*.jpg")) !== false) { // Get a list of files...
foreach ($existing as $file) { // ...loop them...
$msg .= (unlink($directory."/".$file)) ? "File $file deleted\n" : "Could not delete $file\n"; // ...and delete them
}
} else $msg .= "Listing directory failed\n";

Looping through an image directory

I need to loop through a .png image directory and insert the file name in a mysql database.
This is what I have so far:
mysql_connect('localhost', 'admin', '');
mysql_select_db('database');
// Each file is formated like this
$file = 'Abe+Froman+SK_HG.png';
$player_name = urldecode(str_replace("_HG.png", "", $file));
//echo $player_name;
mysql_query("INSERT IGNORE INTO signatures SET gamertag = '".$player_name."'");
Another way of doing it is with glob which allows you to select only the png images if other files are also present in the same directory.
foreach (glob("*.txt") as $filename) {
$player_name = urldecode(str_replace("_HG.png", "", $filename));
mysql_query("INSERT IGNORE INTO signatures SET gamertag = '".$player_name."'");
}
Use opendir to open a directory and readdir to loop through it:
<?php
// From the manual entry for opendir
$dir = "/etc/php5/";
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
//Do your sql insert - the filename is in the $file variable
}
closedir($dh);
}
}
?>

Categories