Zabs
Zabs

Reputation: 14142

Send email with an attachment in Codeigniter

I am using the Codeigniter library to send an email with an attachment, the file I want to attach is in the following directory

/uploads/pdf/yourfile.pdf

How would I attach this in relation to its fullpath? E.g How can I find the correct path (not URL) for this file.

 $this->email->attach('/path/uploads/pdf/yourfile.pdf');

How can I determine what should replace 'path' in the line above

Upvotes: 1

Views: 17351

Answers (2)

Zabs
Zabs

Reputation: 14142

Managed to use CodeIgniters functionality to do the following..

 $path = set_realpath('uploads/pdf/');
 $this->email->attach($path . 'yourfile.pdf');

Hope this helps someone else. Thanks again

note : Don't forget to load the path helper before, otherwise the set_realpath() will not exists.

Upvotes: 3

YAAK
YAAK

Reputation: 328

you can have the directory where your script is running by __DIR__ magic constant or dirname(__FILE__) in older versions of PHP and then determine your path from this point. Then you can use realpath() to have a resolved path to your file. for example:

$path_to_the_file = realpath(__DIR__.'/../uploads/pdf/yourfile.pdf/');

first try to echo __DIR__ to find your current location and then add a relative path from your current location to __DIR__ and feed it to realpath()

Also in Codeigniter, the constant APPPATH points to the application folder, so this is another solution:

$path_to_the_file = realpath(APPPATH.'relative/path/to/the/file/from/application/folder');

Upvotes: 4

Related Questions