PHP - using a loop with fwrite - php

I have a dynamic form that generates a xml file. The user can add add as many as they want. When the user clicks submit to generate the file, all entries should be written to the xml file.
Unfortunately right now, only the last entry is getting written. I know i need a loop statement but I've having issues.
Here's the form.php --> https://jsfiddle.net/jdarville/mbfjmd02/9/
And here's the save.php(i only included the important parts):
<?php
if(isset($_POST['submit'])){
$message0 = '<?xml version="1.0" encoding="UTF-8"?>';
foreach($_POST['howmany'] as $item_number){
$item_number = $item_number;
}
for($x=1;$x<=$item_number;$x++){
//echo $_POST["templateid".$x];
$message2 = '<Template tid="'. $_POST["templateid".$x].'" gid="000">
<OriginalKey>Queues/Scan</OriginalKey>
<Params>
}
for($x=1;$x<=$item_number;$x++){
$message2 = '</FileFormatInformation>
<StorePath>'. $_POST["uncpath".$x].'</StorePath>
<UserName>'.$_POST["username".$x].'</UserName>
<Password>'.$_POST["password".$x].'</Password>
</SMBStoreParameter>
<caption2>'.$_POST["scantoname".$x].'</caption2>
<userName />
}
}
$file = fopen("test.xml","w");
echo fwrite($file,"$message0 $message1 $message2 ");
fclose($file);
?>

Looks like $message2 = needs to be $message2 .= (that's dot equals)

<?php
if (isset($_POST['submit']))
{
$message0 = '<?xml version="1.0" encoding="UTF-8"?>';
$item_number = intval($_POST['howmany']);
for ($x = 1; $x <= $item_number; $x++)
{
$message1.= '<Template tid="' . $_POST["templateid" . $x] . '" gid="000"> <OriginalKey>Queues/Scan</OriginalKey> <Params>';
}
for ($x = 1; $x <= $item_number; $x++)
{
$message2.= '</FileFormatInformation> <StorePath>' . $_POST["uncpath" . $x] . '</StorePath> <UserName>' . $_POST["username" . $x] . '</UserName> <Password>' . $_POST["password" . $x] . '</Password></SMBStoreParameter><caption2>' . $_POST["scantoname" . $x] . '</caption2><userName />';
}
$file = fopen("test.xml", "w");
echo fwrite($file, "$message0 $message1 $message2 ");
fclose($file);
}
?>
Your xml is not valid because some tags have not been closed and some have not been opened below are some invalid xml tags.
<Params> has not been closed.
</FileFormatInformation> has not been opened.

Related

How do I convert a base64 string to a png image in PHP?

I am trying to create a PHP form that outputs a PDF (using mPDF) of all the values in the form after hitting submit. I have included jSignature which takes in an e-signature drawn by the user. The signature is then converted to an image and outputs the data URI string in a text area.
<div id="signature"></div><br/>
<input type='button' id='click' value='click'>
<textarea id='output' name="signature"></textarea><br/>
<!-- Preview image -->
<img src='' id='sign_prev' style='display: none;' />
<!-- Script -->
<script>
$(document).ready(function() {
// Initialize jSignature
var $sigdiv = $("#signature").jSignature({'UndoButton':true});
$('#click').click(function(){
// Get response of type image
var data = $sigdiv.jSignature('getData', 'image');
// Storing in textarea
$('#output').val(data);
// Alter image source
$('#sign_prev').attr('src',"data:"+data);
$('#sign_prev').show();
});
});
</script>
<button type="submit" class="btn btn-success btn-lg">Submit</button>
I have another file that uses the name attribute from the input areas to display the values from each input field to show on the PDF. Since the signature comes in as a data URI string I have to convert it back into a png image before creating the PDF. What I am sure of at the moment is that I am able to retrieve the data URI string and I am able to replace the "image/png;base64" string that comes with it so all I am left with is the data itself. But I am struggling to decode the URI back to an image and I am not sure about how I should display it back as an image.
<?php
require_once __DIR__ . '/vendor/autoload.php';
//grab variables
$inspection = $_POST['inspection'];
$ccOfficer = $_POST['cco-name'];
$inspector = $_POST['inspector'];
$tower = $_POST['tower'];
$strata = $_POST['strata'];
$suite = $_POST['suite'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$signature = $_POST['signature'];
//Create new PDf instance
$mpdf = new \Mpdf\Mpdf();
$mpdf->debug = true;
$signature = str_replace('image/png;base64,','', $signature);
$signatureDecode = base64_decode($signature);
$im = imagecreatefromstring($signatureDecode);
if(!$im) {
die('Base64 value is not a valid image');
};
// $img_file = '/wti-report-form/images/signature.png';
// imagepng($im, $img_file, 0);
//$file = fopen($output_file, "wb");
//fwrite($file, $signatureDecode);
//fclose($file);
//Create our PDF
$data = '';
$data .= '<h1>ARC - NEW HOME WALK-THROUGH CHECKLIST</h1>';
//add data
$data .= '<strong>Inspection Date</strong> ' . $inspection . '<br/>';
$data .= '<strong>Customer Care Officer</strong> ' . $ccOfficer . '<br/>';
$data .= '<strong>This Suite was inspected by</strong> ' . $inspector . '<br/>';
$data .= '<strong>Tower #</strong> ' . $tower . '<br/>';
$data .= '<strong>Strata Lot #</strong> ' . $strata . '<br/>';
$data .= '<strong>Suite #</strong> ' . $suite . '<br/>';
$data .= '<strong>Email</strong> ' . $phone . '<br/>';
$data .= '<strong>Phone</strong> ' . $email . '<br/>';
//$data .= "<img src='" . $file . "'/>";
//$data .= '<p>' . $signature . '</p>';
//Write PDF
$mpdf->WriteHTML($data);
//output to the browser
$mpdf->Output('myfile.pdf', 'I');
?>
Any help will be appreciated. Thanks! :)
As far as I know You can use base64 data string in img tag and mpdf will render PDF correctly, did You try this solution ?
$data.='<img src="data:image/png;base64,'.$signature.'" />';

Extract only text from ePub

I'm trying to do some text analysis on ebooks, so I need to extract the plain text from epub files. Below is example code from php.net which unzips the epub and then echos all its contents.
My problem is that it also tries to echo images so I get lots of this: ��̹,{ϥ㓦,�[k�رO?��� being echoed. Ideally It would just give me super-basic plain text. Any ideas on how to skip echoing a $zip_entry which is an image (or anything non-text)?
Thanks!
$zip = zip_open("book.epub");
if ($zip) {
while ($zip_entry = zip_read($zip)) {
echo "Name: " . zip_entry_name($zip_entry) . "\n";
echo "Actual Filesize: " . zip_entry_filesize($zip_entry) . "\n";
echo "Compressed Size: " . zip_entry_compressedsize($zip_entry) . "\n";
echo "Compression Method: " . zip_entry_compressionmethod($zip_entry) . "\n";
if (zip_entry_open($zip, $zip_entry, "r")) {
echo "File Contents:\n";
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
echo "$buf\n";
zip_entry_close($zip_entry);
}
echo "\n";
}
zip_close($zip);
}
Is there a
content.opf
file in the root of unzipped epub? If so examine it's content. You should see something like:
<item id="chapter19" href="zzzzzzz.xhtml" media-type="application/xhtml+xml" />
<item id="image1" href="images/yyyyy.jpg" media-type="image/jpeg" />
this should give you good idea how to avoid images.

Not getting desire output

What I want to do is,
I am initializing $message variable for mail body.
And I has to iterate through loop if multiple files are there.
So how can i iterate loop and store values in a variable.
Like,
$message = "Dear " . $fname . " <br/>
<b>Manuscript and Other Documents :</b> <br/>
Source File : " .$file1 . " <br/>
Source PDF File : ".$file2 . " <br/>
Cover Letter : " . $file3 . "<br/>
Supplementary Files : " . while($row=mysql_fetch_array($supplementary)){ echo $row[0] } . ";
So how can i do this if i have multiple supplementary files?
To do this, simply append the data to the message variable within the while loop using the string concatenation operator.
For example:
$message = "Dear " . $fname . " <br/><b>Manuscript and Other Documents :</b><br/>
Source File : " .$file1 . "<br/>Source PDF File : ".$file2 . " <br/>
Cover Letter : " . $file3 . "<br/>Supplementary Files : ";
while($row = mysql_fetch_array($supplementary)) {
$message .= echo $row[0] . '<br />';
}
First, do this:
$suplArray = array();
while($row=mysql_fetch_array($supplementary)){
$suplArray[] = $row[0];
}
Now, you have the strings in an array. I don't know what format you want, but you can do something like
$suplStr = implode(',', $suplArray);
Then in your string, you can have:
... "Supplementary Files : " . $suplStr;

Trying to get php to output xml with full url instead of relative path

I have this working fine in browser but air for ios need the full address.
<?php $path_to_image_dir = 'Games/game_images/'.$username;
$xml_string = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?><data> <LoaderMax name=\"gallery\">";
if ( $handle = opendir( $path_to_image_dir ) ) {
$i = 1;
while (false !== ($file = readdir($handle))) {
if ( is_file($path_to_image_dir.'/'.$file) ) {
$xml_string .= "<ImageLoader url=\"" . $path_to_image_dir."/".$file . "\" />";
}
}
closedir($handle);
}
$xml_string .= "</LoaderMax></data>";
$ignore = array('.','..','cgi-bin','.DS_Store');
$file = fopen($username.'_imagesfile.xml','w');
fwrite($file, $xml_string);
fclose($file);
?>
this is the xml result:
<?xml version="1.0" encoding="iso-8859-1"?><data> <LoaderMax name="gallery"><ImageLoader url="Games/game_images/mrpeepers/1365535674.jpg" /><ImageLoader url="Games/game_images/mrpeepers/1365535527.jpg" /></LoaderMax></data>
works on ios if address is added by hand http://wwww.mysite.ccom/Games/game_images/mrpeeper/1365535674.jpg
Add $_SERVER['SERVER_NAME'] to your directory name like so:
$xml_string .= "<ImageLoader url=\"" . $_SERVER['SERVER_NAME'] . "/" . $path_to_image_dir."/".$file . "\" />";
http://php.net/manual/en/reserved.variables.server.php

can't assign value to variable inside recursive function

I'm trying to create a XML file dynamically trough php, reading DIRS and FILES on a specific directory on my ftp.
So far so god, here's the code:
<?php
$path = ".";
$dir_handle = #opendir($path) or die("Unable to open $path");
function list_dir($dir_handle,$path)
{
while (false !== ($file = readdir($dir_handle))) {
$dir =$path.'/'.$file;
$link =$path.'/';
if(is_dir($dir) && $file != '.' && $file !='..' )
{
$handle = #opendir($dir) or die("unable to open file $file");
$xmlString .= '<' . $file . '>';
list_dir($handle, $dir);
$xmlString .= '</' . $file . '>';
}
elseif($file != '.' && $file !='..' && $file !='ftpteste.php')
{
$xmlString .= '<IMAGE>';
$xmlString .= '<PHOTO>' . $link . '' . $file . '</PHOTO>';
$xmlString .= '</IMAGE>';
}
}
closedir($dir_handle);
}
$xmlString ='<XML>';
list_dir($dir_handle,$path);
$xmlString .="</XML>";
$strXMLhead = '<?xml version="1.0" encoding="UTF-8" ?>';
$xmlString = $strXMLhead . "\n" . $xmlString;
$xmlLoc = "../../interiores/xml/content.xml";
$fileXML = fopen($xmlLoc, "w+") or die("Can't open XML file");
if(!fwrite($fileXML, pack("CCC",0xef,0xbb,0xbf)))
{
print "Error Saving File";
}
else
{
fwrite($fileXML, $xmlString);
print "XML file saved";
}
fclose($fileXML);
?>
The problem is that i'm getting no output on $XmlString running inside the function. If i use Print instead joining the strings it's fine, it does the job. But i need it to be in a variable in order to save to file.
Saving to file it's ok.
It should output something like:
<XML>
<$DIR NAME>
<IMAGE>
<PHOTO>$FILE_NAME</PHOTO>
</IMAGE>
</$DIR NAME>
</XML>
Can anyone help me with this?
thanks in advance,
artur
I think you should take a look at the DomDocument object. It provides an easy way to create a document with nodes (like an xml document). You will be able to avoid your problem with that.
Return the XML you're building from list_dir().
function list_dir($dir_handle,$path)
{
...
$xmlString .= list_dir($handle, $dir);
...
return $xmlString;
}
$xmlString = '<XML>' . list_dir($dir_handle,$path) . '</XML>';

Categories