Take files from directory and insert content into SQL - php

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);

Related

PHP readdir(): sort folder first and then files

How to sort the folders first, then the files with the readdir?
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
$file . "\n";
}
closedir($dh);
}
need
..
dir/
dir/
file
file
file
The folders can contain more than 50K files and sub-folders, I don't want to store the results in an array (scandir, etc.) for performance reasons.
Thanks

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

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

Read the content of directory in php

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.

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

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

Categories