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.
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
I have a folder on my server that will receive monthly drops of newsletter files. These drops will occur automatically and I've been asked to write something in PHP to display the list of files as downloadable links while changing the display named based on the name of the file.
The folder I'm looking to is "/var/newsletters" and I'm including code on the index.php page at the root directory. The code I have so far is this:
<?php
$dir = "var/newsletters/";
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
echo "filename:" . $file . "<br>";
}
closedir($dh);
}
}
?>
This does display the list of files, but it's only the first step of the process. They are not linked and they are not renamed. These are monthly newsletter files and are named nmmyyyy.pdf (For example, September would be n092017.pdf). What I need to do is convert n092017.pdf to "September 2017" and then create the link, so something like n092017.pdf and n102017.pdf in the directory becomes
<ul>
<li>September 2017</li>
<li>October 2017</li>
</ul>
I've looked at a few links here:How to list files and folder in a dir (PHP)
and List all files in one directory PHP, but found that the code I showed above worked best. What I need help with is displaying the list as links and converting the names. Thank you!
EDIT:
I was able to get teh link to work with this code:
<?php
$dir = "var/newsletters/";
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
echo " <a href=var/newsletters/$file>Click here</a><br>";
closedir($dh);
}
}
?>
I'm now working on changing the file name and that is not displaying properly. I'm currently working with it as:
<?php
$dir = "var/newsletters/";
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
echo "<a href=var/newsletters/$file>
function getDateFromFileName($filename){
$dateObj = DateTime::createFromFormat('m-
Y',substr($filename,1,2).'-'.substr($filename,3,6));
return $dateObj->format('F Y');
}
</a><br>";
}
closedir($dh);
}
}
?>
EDIT 2
<?php
$dir = "var/newsletters/";
function getDateFromFileName($filename){
$dateObj = DateTime::createFromFormat('m-Y',substr($filename,1,2).'-'.substr($filename,3,6));
return $dateObj->format('F Y');
}
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
//echo $file . "<br>"; OR //echo "<a href=var/newsletters/$file>Click here</a><br>"; WORKING
$formatted_date = getDateFromFileName($file);
echo "<a href=var/newsletters/$file>{$formatted_date}</a><br>";
}
closedir($dh);
}
}
?>
You can add this function to help with getting the dates from the filenames
function getDateFromFileName($filename){
$dateObj = DateTime::createFromFormat('m-Y',substr($filename,1,2).'-'.substr($filename,3,6));
return $dateObj->format('F Y');
}
Calling getDateFromFileName("n102017.pdf") prints October 2017
EDIT:
To change the name in your scenario do
<?php
$dir = "var/newsletters/";
function getDateFromFileName($filename){
$dateObj = DateTime::createFromFormat('m-Y',substr($filename,1,2).'-'.substr($filename,3,6));
return $dateObj->format('F Y');
}
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
if($file == "." || $file ==".."){
continue;
}
$formatted_date = getDateFromFileName($file);
echo " <a href=var/newsletters/$file>{$formatted_date}</a><br>";
closedir($dh);
}
}
?>
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);
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
}
}
}
}
}
I have the following code which reads all filnames from each directory, but I want it to read one filename "only" and skip to the next directory.
<?php
$dir = "/images/";
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
echo "filename:" . $file . "<br>";
}
closedir($dh);
}
}
?>
How can I read only one filename, then skip to the next directory to read "only" first filename and so on. Please let me know if you require any more information.
if it doesn't matter which file you read then just put a break; in your while loop or don't even go in a loop just take the $file = readdir($dh);
PS: In linux OS be aware of . and .. Also look up scabdir() function
You need to maintain counter here to do the same.
Do like this:
$dir = "/images/";
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
echo "filename:" . $file . "<br>";
break;
}
closedir($dh);
}
}
Let me know for more help!