PDF to print iteration in PHP - php

Can anyone tell me if it's possible to iterate over an array of SimpleXML objects that contain PDF data and have each print to separate PDF files? I've been fighting with this for over a week now. My latest loop code is as follows:
foreach($xml->DocumentPDFs->DocumentPDF->PDFBytes as $PDFBytes => $value) {
$binary = base64_decode($value);
file_put_contents($xml->EnvelopeStatus->EnvelopeID . "/" . $xml->EnvelopeStatus->DocumentStatuses->DocumentStatus->Name . ".pdf", $binary,FILE_APPEND);
}
This prints out the first PDF and then exits the loop.

So it turns out it was syntax issues. Both in the base64_decode call and the file_put_contents call:
foreach($xml->DocumentPDFs->DocumentPDF as $value) {
$binary = base64_decode($value->PDFBytes);
file_put_contents($xml->EnvelopeStatus->EnvelopeID . "/" . $value->Name . ".pdf", $binary);
}
So there you go.

Related

PHP how to delete file on local machine

I am trying to read the PDF files in each child job and pull out the number of pages in each. My code works perfectly for the first file, but not for the second. My thinking is that the file is not being overwritten by the second PDF that's pulled in.
I am downloading the file to read it, so there's no need to keep it afterwards.
I tried putting in a check to unlink the file after use, but I get a permission denied error.
$CI = &get_instance();
$CI->load->library('Awss3', null, 'S3');
$PdfTranscriptInfo = $CI->MJob->getDOCCSPdfTranscript($copy['jobid']);
$filename = $PdfTranscriptInfo['origfilename'];
$PdfFilename = 'uploads/' . $copy['jobid'] . '/' . $filename;
$localfilename = FCPATH . 'tmp/local.pdf';
$fileData = $CI->S3->readfile($PdfFilename, false, 'bucket');
require_once 'application/libraries/fpdi/fpdf.php';
require_once 'application/libraries/fpdi/fpdi.php';
$pdf = new FPDI();
$pageCount = $pdf->setSourceFile($localfilename);
if ($pageCount == $copy['pagecount']) {
echo '<td>' . $copy['pagecount'] . '</td>';
} else {
echo '<td style="background-color:red;">' . $copy['pagecount'] . '/' . $pageCount . '</td>';
}
if (file_exists($localfilename)) {
unlink(FCPATH . 'tmp/local.pdf');
}
I am not getting any error messages, but the page count for the second file ($pageCount) is the same as the first file, so it's obviously not picking up the second PDF.
Thanks in advance for any help.
EDIT: FCPATH is C:\wamp64\www\companyname\

How to save $_GET data to a file PHP

I'm trying to save all key values from the GET parameters, but its not writing anything to the file.
foreach ($_GET as $key => $value) {
$contents = $key . " => " . $value . "<br>";
echo($contents);
file_put_contents("./test.log", $contents, FILE_APPEND);
}
Don't use file_put_contents() inside loop. Put it outside:-
$contents='';
foreach ($_GET as $key => $value) {
$contents .= $key . " => " . $value . "\n"; // or use `"\r\n"`
}
file_put_contents("./test.log", $contents, FILE_APPEND);
Note:- Check that file have write permission (644) + folder in which this file lies have the permission too (777) and path of the file is correct.
Below are the screen-shots of working code at my local end:- http://prntscr.com/e98o04 And http://prntscr.com/e98oco
print_r will give you the same output you are trying to build, and you can solve this using just one line.
file_put_contents("./test.log", print_r($_GET, true), FILE_APPEND);
AS i seen the save data in file is in every iteration an only at iteration current position:
$contents='';
foreach ($_GET as $key => $value) {
$contents.= $key . " => " . $value . "<br>";
}
file_put_contents("./test.log", $contents, FILE_APPEND);
Same path of php code?, not needed "./", could you try to open file, before and put here if there is no error.

Extract a folder and then search for specific file in PHP

I´m building a php programm which uploads a zip file, extracts it and generates a link for a specific file in the extracted folder. Uploading and extracting the folder works fine. Now I´m a bit stuck what to do next. I have to adress the just extracted folder and find the (only) html file that is in it. Then a link to that file has to be generated.
Here is the code I´m using currently:
$zip = new ZipArchive();
if ($zip->open($_FILES['zip_to_upload']['name']) === TRUE)
{
$folderName = trim($zip->getNameIndex(0), '/');
$zip->extractTo(getcwd());
$zip->close();
}
else
{
echo 'Es gab einen Fehler beim Extrahieren der Datei';
}
$dir = getcwd();
$scandir = scandir($dir);
foreach ($scandir as $key => $value)
{
if (!in_array($value,array(".",".."))) //filter . and .. directory on linux-systems
{
if (is_dir($dir . DIRECTORY_SEPARATOR . $value) && $value == $folderName)
{
foreach (glob($value . "/*.html") as $filename) {
$htmlFiles[] = $filename; //this is for later use
echo "<a href='". SK_PICS_SRV . DIRECTORY_SEPARATOR . $filename . "'>" . SK_PICS_SRV . DIRECTORY_SEPARATOR . $filename . "</a>";
}
}
}
}
So this code seems to be working. I just noticed a rather strange problem. The $zip->getNameIndex[0] function behaves differently depending on the program that created the zip file. When I make a zip file with 7zip all seems to work without a problem. $folderName contains the right name of the main folder which I just extracted. For example "folder 01". But when I zip it with the normal windows zip programm the excat same folder (same structure and same containing files) the $zip->getNameIndex[0] contains the wrong value. For example something like "folder 01/images/" or "folder 01/example.html". So it seems to read the zip file differently/ in a wrong way. Do you guys know where that error comes from or how I can avoid it? This really seems strange to me.
Because you specify the extract-path by yourself you can try finding your file with php's function "glob"
have a look at the manual:
Glob
This function will return the name of the file matching the search pattern.
With your extract-path you now have your link to the file.
$dir = "../../suedkurier/werbung/"
$scandir = scandir($dir);
foreach ($scandir as $key => $value)
{
if (!in_array($value,array(".",".."))) //filter . and .. directory on linux-systems
{
if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
{
foreach (glob($dir . DIRECTORY_SEPARATOR . $value . "/*.html") as $filename) {
$files[] = $value . DIRECTORY_SEPARATOR $filename;
}
}
}
}
The matched files will now be saved in the array $files (with the subfolder)
So you get your path like
foreach($files as $file){
echo $dir . DIRECTORY_SEPARATOR . $file;
}
$dir = "the/Directory/You/Extracted/To";
$files1 = scandir($dir);
foreach($files1 as $str)
{
if(strcmp(pathinfo($str, PATHINFO_EXTENSION),"html")===0||strcmp(pathinfo($str, PATHINFO_EXTENSION),"htm")===0)
{
echo $str;
}
}
Get an array of each file in the directory, check the extension of each one for htm/html, then echo the name if true.

PHP - Moving multiple files with different files names to own directory

Hi wonder if you can help,
I'm looking to do this in PHP if someone can help me. I have a number of files that look like this:
"2005532-JoePharnel.pdf"
and
"1205121-HarryCollins.pdf"
Basically I want to create a PHP code that when someone ftp uploads those files to the upload folder that it will 1) Create a directory if it doesn't exist using there name 2) Move the files to the correct directory (E.g. JoePharnel to the JoePharnel Directory ignoring the number at the beginning)
I have looked through alot of code and found this code and have adapted it:
<?php
$attachments = array();
preg_match_all('/([^\[]+)\[([^\]]+)\],?/', $attachments, $matches, PREG_SET_ORDER);
foreach ($matches as $file) {
$attachments[$file[1]] = $file[2];
}
foreach ($attachments as $file => $filename) {
$uploaddir = "upload" . $file;
$casenumdir = "upload/JoePharnel" . $CaseNumber;
$newfiledir = "upload/JoePharnel" . $CaseNumber .'/'. $file;
$each_file = $CaseNumber .'/'. $file;
if(is_dir($casenumdir)==false){
mkdir("$casenumdir", 0700); // Create directory if it does not exist
}
if(file_exists($casenumdir.'/'.$file)==false){
chmod ($uploaddir, 0777);
copy($uploaddir,$newfiledir);
}
$allfiles = $CaseNumber .'/'. $file . "[" . $filename . "]" . ",";
$filelistfinished = preg_replace("/,$/", "", $allfiles);
echo $filelistfinished;
// displays multiple file attachments on the page correctly as: casenumber/testfile1.pdf[testfile1.pdf],casenumber/testfile2.pdf[testfile2.pdf],
],
}
?>
Sorry for the lack of code but best i could find.
Any help is much appreciated.
Thanks.

Trying to load an php array into an echo that prints an image

Hi I am trying to get images to load into a page using the file names from an array,
This is what I have so far
<?php
$i=0;
$img=array("1.png","2.png","3.png","4.png");
while ($i<count($img))
{
echo "<img class='loadin' alt='imgg' src=" . "'http://www/images/" . $img[i] . "'" . "/" . ">" . "<br/>";
$i++;
}
?>
It seems to ignore the file name and just enters:
http://www/images/
as the source and ignores the file name from the array
Any Help would be great Thanks
Mikey
You forgot the dollar sign with your $i variable: $img[$i]
EDIT:
(btw. using a foreach-loop would be easier...)
foreach($img AS $filename) {
echo "<img class='loadin' alt='imgg' src='http://www/images/" . $filename . "'/><br/>";
}

Categories