PHP: connect X.pdf to X.png in a chosen directory - php

I am looking for a PHP script that connects *.PDF to *.PNG files in a directory. Due to lack of PHP knowledge, I only know how to do this manually in HTML. Could PHP do this for me automatically by connecting A.pdf with A.png, B.pdf with B.png, C.pdf with C.png and so on? Then all I need to be able doing is to change the folder name in PHP.
<div><img src="FOLDER/A.png"></div>
<div><img src="FOLDER/B.png"></div>
<div><img src="FOLDER/C.png"></div>
etcetera...

I'm not sure if I understood this correctly, but the code below will get all .pdf files from a directory and return the HTML for each .pdf file. This will also check to see if the .png exists, but will not echo anything if it does not exist.
// get all .pdf files from directory
$directory = "directory/";
$files = glob($directory."*.{[pP][dD][fF]}", GLOB_BRACE);
foreach ($files as $item) {
// get pathinfo to get the filename
$fileInfo = pathinfo($item);
$fileName = $fileInfo['filename'];
// check if the .png exists
if (file_exists($directory.$fileName.".png")) {
// echo
echo "<div><a href='".$directory.$fileName.".pdf'><img src='".$directory.$fileName.".png'></a></div>";
}
}

Related

how to use original uploaded file without using its .tmp file in Laravel

I want to extract text from docx and doc file. I am using this class The one in the answer.
Everything works fine when i use them in native php and docx file in the same directory of the php files. It extracts pretty well. This is not the case when i upload them through <input type="file">. You can see in the link that this class accepts only docx,doc,pptx and xlsx. I know when you upload file in php it renames and move to temp to avoid name clashing and overwriting. So i came with something like getting the tmp file and removing its extension and adding docx or doc to it.
Here is my code
$file = $request->file('resume');
echo $file."<br>";
$withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file);
$echo $withoutExt."<br>";
$original_file = $withoutExt.".docx"."<br>";
$echo $original_file."<br>";
$doc_file = new DocxConversion($original_file);
$echo $docText= $doc_file->convertToText();
The above code gives me the output as i expected till converting the .tmp to .docx but finally says File Not exists Here is the output
C:\xampp\tmp\phpCB7E.tmp
C:\xampp\tmp\phpCB7E
C:\xampp\tmp\phpCB7E.docx
File Not exists
I have also tried to put a docx file in the controllers directory and tried to execute like this
public function index1(){
echo "hello";
$docObj = new DocxConversion("hello.docx");
var_dump($docText= $docObj->convertToText());
}
The above approach also says File not exists. Am i doing anything wrong here? It works perfectly with the same file in native code where my php files and docx files are in same directory but not when i use it in my controller.
Assuming your $file is a UploadedFile you can use the getRealPath method to get the path to the filename,
$file = $request->file('resume');
$doc_file = new DocxConversion($file->getRealPath());
echo $doc_file->convertToText();

Get directory on a file that path has stored into database

I have a path of file that I stored into a table database like this :
D:/XAMPP/htdocs/develop_tsurumaru/assets/iwwi_file/DO_FOLDER/Damage_Report/0080//thumbs/thumb_52.jpg
You know, this path is a string type now.
How to get its directory, which is :
D:/XAMPP/htdocs/develop_tsurumaru/assets/iwwi_file/DO_FOLDER/Damage_Report/0080//thumbs/
Thanks
If you want to get a list of all the files and folders under thumbs folder, then follow the below code.
<?php
$dir = 'D:/XAMPP/htdocs/develop_tsurumaru/assets/iwwi_file/DO_FOLDER/Damage_Report/0080//thumbs';
$files = scandir($dir);
print_r($files); // This will print an array with all the files and folders
?>

Find full file name using wildcards using glob

I would like to search for the full name of the .jpg image using only the numbers.
I have a folder called "Pictures" and inside all of the pictures are named something like this:
100-Lisa_Person1.jpg
150-BillJohnson.jpg
160-BlakeSmith(1).jpg
and so on....
THE CODE:
$contact_lastname = 150;
$files = glob("C:\Pictures\\" . $contact_lastname . "*.jpg"); // Will find my .jpg
// Process through each file in the list
// and output its extension
if (count($files) > 0)
foreach ($files as $file)
{
$info = pathinfo($file);
echo "File found: extension ".$info["extension"]."<br>";
}
else
echo "No file name exists called $compartment. Regardless of extension."
So this is what I tired but it's saying: File found: extension jpg but I need to know the full file name of the image it found.
Well the full name of the file would be
$info["basename"]
This is because the pathinfo() function returns an array with these indexes:
dirname - the path of the files parent folder.
basename - the full name of the file, including extension.
extension - the extension of the file (this is what you used and is why you only got the extension).
filename - the name of the file without the extension.

Search for file in folder using php

I am storing user uploaded files like pdf images and txt files in separate folders using my php script i want to retrieve the file names from the folder upload and give the pdf and txt in a group and also way to search for specific file.
I also need to rename the file before to $ja variable
$ja
$da = date("dmY");
$ja = $uid.$da;
move_uploaded_file($mi, $uploadpath)
also used this code which i found in stack
Example 01:
<?php
// read all files inside the given directory
// limited to a specific file extension
$files = glob("./ABC/*.txt");
?>
Example 02:
<?php
// perform actions for each file found
foreach (glob("./ABC/*.txt") as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}
?>
the scandir(); function will help you
<?php
$dir = '/tmp';
$files1 = scandir($dir);
print_r($files1);
?>
http://www.php.net/manual/en/function.scandir.php
now you have array of all files in a location you specified you can use array functions to get your work done
you can try listing the directory with scandir, and then filter as you want in the php array of filenames you will get

How we can read zip file and get information of files or folders contains without unzipping in PHP?

What I actually wanted to do is read zip file and then if it does contain folder then refuse it with some message.
I want user should upload zip file with files only without any directory structure.
So I want to read zip file contains and check file structure.
I am trying with following code snippet.
$zip = zip_open('/path/to/zipfile');
while($zip_entry = zip_read($zip)){
$filename = zip_entry_name($zip_entry);
//#todo check whether file or folder.
}
I have sorted out.
I am now checking filename as strings wherever I am getting string ending with "/" that am treating as directory else as file.
can't you parse path of $filename? something like $dirName = pathinfo($filename, PATHINFO_DIRNAME)

Categories