Forums

attachements no longer working?

samoht 08 Sep, 2009
Hey guys,

I added a little code to my billing and invoice form to also include billing tickets and now no attachments are being included?

Do you see something wrong with this:

<?php
$f=1;
$fdate = 'invoices/*-invoice_'.date('m-d-Y').'.doc';
//echo $fdate;
foreach (glob("$fdate") as $filename) {
    echo '<input type="hidden" id="att'.$f.'" name="att'.$f.'" value="'.$filename.'"> '. "\n";
	$f++;
}
//gather up the billing tickets and attach them to the email
$btdate = 'billing_tickets/*-a.txt';
foreach (glob("$btdate") as $filename){
	echo '<input type="hidden" id="att'.$f.'" name="att'.$f.'" value="'.$filename.'"> '. "\n";
	rename($filename,substr_replace($filename, "",-6).".txt");
	$f++;
}
?>

the second foreach is what I added.

and then I also added this in the onSubmit Before:
$fdate = 'invoices/*-invoice_'.date('m-d-Y').'.doc';
$attachments = glob($fdate);
$attachments .= glob($btdate); 
foreach (glob("$fdate") as $filename) {
    echo '<input type="hidden" id="att'.$f.'" name="att'.$f.'" value="'.$filename.'"> '. "\n";
	$f++;
}
foreach (glob("$btdate") as $filename){
	echo '<input type="hidden" id="att'.$f.'" name="att'.$f.'" value="'.$filename.'"> '. "\n";
	rename($filename,substr_replace($filename, "",-6).".txt");
	$f++;
}
print_r($attachments, TRUE);
samoht 09 Sep, 2009
Here is what a debug screen contained:

<dd class="message message fade">
	<ul>
		<li>Form passed first SPAM check OK</li>
		<li>Form passed the submissions limit (if enabled) OK</li>
		<li>Form passed the Image verification (if enabled) OK</li>
		<li>Form passed the server side validation (if enabled) OK</li>

		<li>Form passed the plugins step (if enabled) OK</li>
		<li>Emails data loaded OK</li>
		<li>Form passed all before email code evaluation OK</li>
		<li>An email has been SENT successfully from (Andrew Adcock)aadcock@tntmax.com to andynsonya@comcast.net</li>
		<li>Debug End</li>
	</ul>

</dd>
</dl>

						<div id="ja-pathway">
				
			</div>
			
			_POST: Array
(
    [startdate] => 2009-09-01
    [enddate] => 2009-09-09
    [att1] => invoices/Surgimap-com-invoice_09-08-2009.doc
    [att2] => invoices/TimothySykes-com-invoice_09-08-2009.doc
    [att3] => invoices/philstockworld-com-invoice_09-08-2009.doc
    [att4] => billing_tickets/Surgimap.com-billing_ticket_09-08-2009_18-58-28-a.txt
    [att5] => billing_tickets/TimothySykes.com-billing_ticket_09-08-2009_12-59-51-a.txt
    [submit] => Send
    [ebf3ec5903c49797bdca51788c60224a] => 1
)
nml375 09 Sep, 2009
Hi samoth,
To me, it looks like you first add the "old" filename to your $attachments array, then rename the actual files, making the filenames in the $attachments-array point to non-existant files. Since I don't see the actual code responsible for the attachment, I could only imagine it somehow chokes later on, preventing any attachments being made.

/Fredrik
samoht 09 Sep, 2009
Hi Fredrik,

I was renaming the files (billing tickets only) so that I could determine files that need to be attached verses files that have already been sent. Since the billing tickets are created every day but the invoices only once at the end of the billing period. I put this code in the OnSubmit Before - so I didn't think that renaming it here would affect anything.

Hmm, maybe there is a better way of searching my directory for billing tickets that using the glob function?
I would really prefer just using a date range query since I have already got a begin date and an end date from the form fields. Any ideas how I might accomplish that?

Thanks,
nml375 09 Sep, 2009
Hi samoht,
Try putting the code in the "on submit after email" instead, by then the files should've been attached and sent already, and any changes to the filename should'nt be a problem anymore...

/Fredrik
samoht 09 Sep, 2009
Thanks,

I'll give that a try.

but I really don't like my logic - since it will only work once. But for the sake of the attachments I'll just try to get that working first.
nml375 09 Sep, 2009
One approach, I suppose, for the date-range idea is to iterate through a list of files and use the filectime to see when it was last changed. If you then add some archive-mechanism to your monthly invoice-routine, the list to parse would not be that large.

Yet another approach would be to create a database table holding an ID (filename), date, and content/path to file; though this approach would probably call for quite a few changes in your current code.

/Fredrik
samoht 16 Oct, 2010
Hello all,

Something strange happened recently - and I'm not sure what caused it or how to find the issue.
I am now getting this error when I try to run an invoice creating form:

Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\secureforms\components\com_chronocontact\chronocontact.html.php(320) : eval()'d code on line 333

Warning: fclose(): supplied argument is not a valid stream resource in C:\xampp\htdocs\secureforms\components\com_chronocontact\chronocontact.html.php(320) : eval()'d code on line 334



I believe that this is breaking on my FormHTML at this point:
		</body>
	</html>	'."\n";
		fwrite($fp, $str);
		fclose($fp); 

?>


which is at the end of this code:
<?php
global $mainframe;
$database =& JFactory::getDBO();
$database->setQuery( 'SELECT TIMEDIFF(ts_timeout, ts_timein) as totaltime, TIME_TO_SEC(TIMEDIFF(ts_timeout, ts_timein)) as totalsecs, ts_client, ts_dscr, recordtime 
                    FROM jos_chronoforms_timesheet2 
                    WHERE recordtime BETWEEN "' .$_POST['date_0'].'" AND "'.$_POST['date_1'].'"
                    ORDER BY ts_client, recordtime');
$database->query();
$records = $database->loadAssocList();
                                

$c=1;
$t=0;
$prevclient = '';
 
    foreach( $records as $row ) {
        if($row['ts_client'] != $prevclient){
            if($prevclient != '') {
                //this is just to add extra rows for filling out the page!
                for($w=0; $w < 17 - $t; $w++){
                    $str .=  '
                    <tr>
                        <td> </td>
                        <td> </td>
                        <td> </td>
                        <td> </td>
                    </tr>';
                }
                //get the total time in seconds and convert it to a decimal for each client
                $n=$gt/3600;
                //close each client table except for the last
                $str .=  '
                            <tr style="page-break-inside:avoid;height:.2in">
                              <td width=414 valign=top style="width:310.5pt;border:none;padding:2.15pt 5.75pt 2.15pt 5.75pt;  height:.2in">
                              <p class=MsoNormal> </p>
                              </td>
                              <td width=102 style="width:76.5pt;border:none;padding:2.15pt 5.75pt 2.15pt 5.75pt;  height:.2in">
                              <p class=MsoNormal> '.round($n, 1) .' </p>
                              </td>
                              <td width=102 style="width:76.6pt;border:none;border-right:solid windowtext 1.0pt;  padding:2.15pt .15in 2.15pt .15in;height:.2in">
                              <p class=RightAligned>TOTAL</p>
                              </td>
                              <td width=102 style="width:76.5pt;border-top:none;border-left:none;  border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;  padding:2.15pt .15in 2.15pt .15in;height:.2in">
                              <p class=Amount>$'.round($n * 37.75, 2) .'</p>
                              </td>
                            </tr>
                        </table>
                    </div>
                    <p class=MsoNormal> </p>
                    <div align=center>
                        <table class=MsoNormalTable border=0 cellspacing=0 cellpadding=0 width=720 style="width:7.5in;border-collapse:collapse">
                         <tr style="height:109.9pt">
                          <td width=720 valign=bottom style="width:7.5in;padding:5.75pt 5.75pt 5.75pt 5.75pt;  height:109.9pt">
                          <p class=MsoNormal>Make all checks payable to Andrew Adcock</p>
                          <p class=MsoNormal>Total due in 15 days. Overdue accounts subject to a  service charge of 1% per month.</p>
                          <p class=MsoNormal> </p>
                          </td>
                         </tr>
                         <tr style="height:.3in">
                          <td width=720 style="width:7.5in;padding:0in 5.4pt 0in 5.4pt;height:.3in">
                          <p class=Thankyou>Thank you for your business!</p>
                          </td>
                         </tr>
                        </table>
                    </div>
                    <p class=MsoNormal> </p>
                    </body>
                </html>    '."\n";
                fwrite($fp, $str);
                fclose($fp); 
                //set these back to 0 so that the total does not include the previous clients
                $n=0;
                $gt = 0;
                $t=0;
                $fp='';
                $str='';
            }
            // start the new WORD document
            
            $cname = str_replace(".","-",$row['ts_client']);
            $filename = 'invoices/'.$cname.'-invoice_'.date('m-d-Y').'.doc';
            $fp = fopen($filename, 'x+');
            //start writting the file contents.
            //throw in some style 
            $str = '
            <html>
            <style>
            <!--
             /* Font Definitions */
             @font-face
                {font-family:Tahoma;
                panose-1:2 11 6 4 3 5 4 4 2 4;}
             /* Style Definitions */
             p.MsoNormal, li.MsoNormal, div.MsoNormal
                {margin:0in;
                margin-bottom:.0001pt;
                line-height:110%;
                font-size:8.5pt;
                font-family:Tahoma;
                letter-spacing:.2pt;}
            h1
                {margin:0in;
                margin-bottom:.0001pt;
                text-align:right;
                line-height:110%;
                font-size:20.0pt;
                font-family:Tahoma;
                color:gray;
                letter-spacing:.2pt;}
            h2
                {margin:0in;
                margin-bottom:.0001pt;
                line-height:110%;
                font-size:8.0pt;
                font-family:Tahoma;
                text-transform:uppercase;
                letter-spacing:.2pt;}
            h3
                {margin:0in;
                margin-bottom:.0001pt;
                line-height:110%;
                font-size:8.5pt;
                font-family:Tahoma;
                letter-spacing:.2pt;
                font-weight:normal;
                font-style:italic;}
            p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
                {margin:0in;
                margin-bottom:.0001pt;
                line-height:110%;
                font-size:8.0pt;
                font-family:Tahoma;
                letter-spacing:.2pt;}
            p.Companyname, li.Companyname, div.Companyname
                {margin-top:7.0pt;
                margin-right:0in;
                margin-bottom:0in;
                margin-left:0in;
                margin-bottom:.0001pt;
                line-height:110%;
                font-size:12.0pt;
                font-family:Tahoma;
                letter-spacing:.2pt;
                font-weight:bold;}
            p.Columnheading, li.Columnheading, div.Columnheading
                {margin:0in;
                margin-bottom:.0001pt;
                text-align:center;
                line-height:110%;
                font-size:8.0pt;
                font-family:Tahoma;
                letter-spacing:.2pt;
                font-weight:bold;}
            p.RightAligned, li.RightAligned, div.RightAligned
                {margin:0in;
                margin-bottom:.0001pt;
                text-align:right;
                line-height:110%;
                font-size:8.0pt;
                font-family:Tahoma;
                text-transform:uppercase;
                letter-spacing:.2pt;}
            p.Thankyou, li.Thankyou, div.Thankyou
                {margin:0in;
                margin-bottom:.0001pt;
                text-align:center;
                line-height:110%;
                font-size:10.0pt;
                font-family:Tahoma;
                letter-spacing:.2pt;
                font-weight:bold;}
            p.Amount, li.Amount, div.Amount
                {margin:0in;
                margin-bottom:.0001pt;
                text-align:right;
                line-height:110%;
                font-size:8.5pt;
                font-family:Tahoma;
                letter-spacing:.2pt;}
            @page Section1
                {size:8.5in 11.0in;
                margin:.5in .5in 36.7pt .5in;}
            div.Section1
                {page:Section1;}
             /* List Definitions */
             ol
                {margin-bottom:0in;}
            ul
                {margin-bottom:0in;}
            -->
            </style>
            <body>
            '."\n";
            $strc=$row['ts_client'];
            $fl=$strc[0];
            //begin actual page
            $str .= '
            <div align=center>
                <table class=MsoNormalTable border=0 cellspacing=0 cellpadding=0 width=720 style="width:7.5in;border-collapse:collapse">
                 <tr style="height:39.75pt">
                  <td width=361 rowspan=2 valign=top style="width:270.6pt;padding:0in 5.4pt 0in 5.4pt;  height:39.75pt">
                  <p class=Companyname>Andrew T Adcock</p>
                  <h3>Web work</h3>
                  <p class=MsoNormal> </p>
                  <p class=MsoNormal>233 Hamel Ave</p>
                  <p class=MsoNormal>Glenside, PA 19038</p>
                  <p class=MsoNormal>Phone (267)943-1097  </p>
                  </td>
                  <td width=359 valign=top style="width:269.4pt;padding:0in 5.4pt 0in 5.4pt;  height:39.75pt">
                  <h1>INVOICE</h1>
                  </td>
                 </tr>
                 <tr style="height:39.75pt">
                  <td width=359 valign=bottom style="width:269.4pt;padding:0in 5.4pt 0in 5.4pt; height:39.75pt">
                  <p class=RightAligned>Invoice #'.$c.'</p>
                  <p class=RightAligned>Date: '.$_POST['date_0'].' to '.$_POST['date_1'].'</p>
                  </td>
                 </tr>
                </table>
            </div>
            <p class=MsoNormal> </p>
            <div align=center>
                <table class=MsoNormalTable border=0 cellspacing=0 cellpadding=0 width=720 style="width:7.5in;border-collapse:collapse">
                 <tr style="height:1.0in">
                  <td width=360 valign=top style="width:3.75in;padding:0in 5.4pt 0in 5.4pt;  height:1.0in">
                  <h2>To:</h2>
                  <p class=MsoNormal>Irene Galvan </p>
                  <p class=MsoNormal>TNTmax, LLC</p>
                  <p class=MsoNormal>491 Sicomac Avenue</p>
                  <p class=MsoNormal>Wyckoff, NJ 07481-1136</p>
                  <p class=MsoNormal></p>
                  </td>
                  <td width=360 valign=top style="width:3.75in;padding:0in 5.4pt 0in 5.4pt;  height:1.0in">
                  <h2>For:</h2>
                  <p class=MsoNormal>'.$row['ts_client'].'</p>
                  <p class=MsoNormal>#'.$fl.date('mdy').rand(31,69).'</p>
                  </td>
                 </tr>
                </table>
            </div>
            <p class=MsoNormal> </p>
            <p class=MsoNormal> </p>';
            $str .=  '
            <div align=center>    
                <table class=MsoNormalTable border=1 cellspacing=0 cellpadding=0 width=720 style="width:7.5in;border-collapse:collapse;border:none">
                    <tr height=20 class="headrow">
                        <td width="66%"><p class=Columnheading>Description:</p></td>
                        <td width="12%"><p class=Columnheading>Hours:</p></td>
                        <td width="12%"><p class=Columnheading>Rate:</p></td>
                        <td width="12%"><p class=Columnheading>Amount:</p></td>
                    </tr>';
            $prevclient = $row['ts_client'];
        }
        $ts=$row['totalsecs']/3600;
        $str .=  '
            <tr>
                <td><p class=MsoNormal>'.$row['ts_dscr'].'</p></td>
                <td><p class=MsoNormal>'.round($ts,1) .'</p></td>
                <td><p class=Amount>$37.75</p></td>
                <td><p class=Amount>'.round($ts * 37.75, 2) .'</p></td>
            </tr>';
        $gt += $row['totalsecs'];
        $gtt += $row['totalsecs'];
        $c++;
        $t++;
    }
    for($w=0; $w < 17 - $t; $w++){
        $str .=  '
        <tr>
            <td> </td>
            <td> </td>
            <td> </td>
            <td> </td>
        </tr>';
    }
    $n=$gt/3600;
    $str .=  '
                    <tr style="page-break-inside:avoid;height:.2in">
                      <td width=414 valign=top style="width:310.5pt;border:none;padding:2.15pt 5.75pt 2.15pt 5.75pt;  height:.2in">
                      <p class=MsoNormal> </p>
                      </td>
                      <td width=102 style="width:76.5pt;border:none;padding:2.15pt 5.75pt 2.15pt 5.75pt;  height:.2in">
                      <p class=MsoNormal> '.round($n, 1) .' </p>
                      </td>
                      <td width=102 style="width:76.5pt;border:none;border-right:solid windowtext 1.0pt;  padding:2.15pt .15in 2.15pt .15in;height:.2in">
                      <p class=RightAligned>TOTAL</p>
                      </td>
                      <td width=102 style="width:76.5pt;border-top:none;border-left:none;  border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;  padding:2.15pt .15in 2.15pt .15in;height:.2in">
                      <p class=Amount>$'.round($n * 37.75, 2) .'</p>
                      </td>
                    </tr>
                </table>
            </div>
            <p class=MsoNormal> </p>
            <div align=center>
                <table class=MsoNormalTable border=0 cellspacing=0 cellpadding=0 width=720 style="width:7.5in;border-collapse:collapse">
                 <tr style="height:109.9pt">
                  <td width=720 valign=bottom style="width:7.5in;padding:5.75pt 5.75pt 5.75pt 5.75pt;  height:109.9pt">
                  <p class=MsoNormal>Make all checks payable to Andrew Adcock</p>
                  <p class=MsoNormal>Total due in 15 days. Overdue accounts subject to a  service charge of 1% per month.</p>
                  <p class=MsoNormal> </p>
                  </td>
                 </tr>
                 <tr style="height:.3in">
                  <td width=720 style="width:7.5in;padding:0in 5.4pt 0in 5.4pt;height:.3in">
                  <p class=Thankyou>Thank you for your business!</p>
                  </td>
                 </tr>
                </table>
            </div>
            <p class=MsoNormal> </p>
        </body>
    </html>    '."\n";
        fwrite($fp, $str);
        fclose($fp); 

?>


Now this code has been working fine for at least a year - so I'm not thinking the problem is here, but where else could it be??

Thanks for any help.
GreyHead 16 Oct, 2010
Hi samoht,

The fopen() earlier on in the code isn't working for some reason. Maybe some permissions changed?

Bob
samoht 16 Oct, 2010
Thanks Bob,

I found the problem. The date range I picked was off by a day and did not have any info returned - that is why the fopen worked but not the fwrite etc. I thought it was a permissions thing too - but when I tested with a previous date range it worked and I realized my problem. However, it brought up an issue - how should write the code to check for zero records and send a message back stating such?

thanks
GreyHead 17 Oct, 2010
Hi samoht,

Something like
$database->query();
$records = $database->loadAssocList();
// add these lines
if ( ! count($records ) {
  $mainframe->redirect('some_url', 'Sorry, no records were found', 'error');
  return false;
}
. . .


Bob
This topic is locked and no more replies can be posted.