Hello,

I am developing a site with Codeigniter 2.1.3.

I am creating a pdf file with mpdf and I save the file in a folder outside the root folder.
So my folders look like this:
c:/mysite/uploads/mypdffile.pdf
c:/mysite/public_html

I looked at all kind of tutorials and forum posts about the php header function, but I can't understand how to use it to get the file.

I am using: $mail->AddAttachment($mypdf) but how do I get the file if it is in a folder outside the root folder?

Can someone please help?

Thank you

Recommended Answers

On live server you can use the Document Root to get an idea of the correct path, for example:

echo $_SERVER['DOCUMENT_ROOT'];

returns the server root, if this is something like /var/www/mysite/public_html/ and your upload directory is at the same level of public_html, then you can set:

$pdfFilePath = "/var/www/mysite/uploads/$filename"; …
Jump to Post

All 4 Replies

Sorry, maybe I should add some of my code here:
In my controller I have this code:

$filename = 'mypdf.pdf';
$pdfFilePath = "../uploads/$filename";

if(file_exists($pdfFilePath) == FALSE)
{
    ini_set('memory_limit','128M');
    $html = $this->load->view('email/invoice', $data, true); // render the view into HTML

    $this->load->library('pdf');
    $pdf = $this->pdf->load();
    $pdf->SetFooter($_SERVER['HTTP_HOST'].'|{PAGENO}|'.date(DATE_RFC822)); 
    $pdf->logo = file_get_contents(site_url('assets/img/logo.png'));
    $pdf->WriteHTML($html); // write the HTML into the PDF
    $pdf->Output($pdfFilePath, 'F'); // save to file because we can
}

$subject = 'Your order with Highland Coffee Roastery';
$body = 'email/send_order';
$emailToName = $data['user']->first_name . ' ' . $data['user']->last_name;

$attach = $pdfFilePath;
$emailSent = sendEmail('info@mysite.co.za', 'My Site', 'noreply@mysite.co.za', 'NoReply', $data['user']->email, $emailToName, $subject, $body, $data, $attach);

In my send email helper:

function sendEmail($fromEmail, $fromName, $replyTo, $replyToName, $emailTo, $emailToName, $subject, $body, $bodyData, $attach='')
{
    require_once APPPATH."libraries/phpmailer/class.phpmailer.php";
    $mail = new PHPMailer();
    $CI =& get_instance();

    $body             = $CI->load->view($body, $bodyData, TRUE);    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->SMTPDebug  = 0;// enables SMTP debug information (for testing)
    $mail->IsHTML(true);

    if(ENVIRONMENT == 'development')
    {
        $mail->SMTPAuth   = true;                  // enable SMTP authentication
        $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
        $mail->Host       = 'smtp.gmail.com'; // SMTP server
        $mail->Port       = 465;                    // set the SMTP port for the GMAIL server
        $mail->Username   = "myemail@gmail.com";     // GMAIL username
        $mail->Password   = "password";            // GMAIL password
    }

    $mail->SetFrom($fromEmail, $fromName);
    $mail->AddReplyTo($replyTo, $replyToName);
    $mail->Subject    = $subject;
    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!";     // optional, comment out and test
    $mail->MsgHTML($body);

    $address = $emailToName . ' ' . $emailTo; //"recipient address@someadress.com";
    $mail->AddAddress($emailTo, $emailToName);

    $logo = 'logo_small.png';
    $logoUrl = 'assets/img/logo_small.png';

    $mail->AddEmbeddedImage($logoUrl, 'logo', $logo);
    if($attach != '')
        $mail->AddAttachment($attach);

    if($mail->Send())
        return TRUE;
    else
        dump($mail->ErrorInfo);
}

The file is not being attached as the server can't find it.

Hope you understand what I am trying to ask.... ;)

Set an absolute path, absolute to the system not to the web root, so change this:

$pdfFilePath = "../uploads/$filename";

to:

$pdfFilePath = "c:/mysite/uploads/$filename";

Thanks, but that won't work on the live server?

On live server you can use the Document Root to get an idea of the correct path, for example:

echo $_SERVER['DOCUMENT_ROOT'];

returns the server root, if this is something like /var/www/mysite/public_html/ and your upload directory is at the same level of public_html, then you can set:

$pdfFilePath = "/var/www/mysite/uploads/$filename";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.