-1

I have a directory, having more than two files. i need to get all files for attaching to PHP mail function. So i need a php function to get all files from the directory. anybody could you please help me to give an idea to get a result.

thank you.

Gireesh T
  • 57
  • 1
  • 9

2 Answers2

0

Use glob() function, you will get all folders and files in current directory

foreach(glob('*') as $files){
    echo "<br />".$files;
}

for more info, please visit http://php.net/manual/en/function.glob.php

for specific file extension e.g. .php, you can use glob('*.php')

Bhaskar Jain
  • 1,651
  • 1
  • 12
  • 20
0

You can use a scandir() function to list the files. So you can loop to add the files

<?php
$directory = '/path/to/my/directory';
// Eliminate folder "." and ".."
$files = array_diff(scandir($directory), array('..', '.'));

?>

I recommend you to use PHPMailer for send emails.

<?php
$email = new PHPMailer();
$email->From      = 'you@example.com';
$email->FromName  = 'Your Name';
$email->Subject   = 'Message Subject';
$email->Body      = $bodytext;
$email->AddAddress( 'destinationaddress@example.com' );
foreach($files as $file):
    $email->AddAttachment( $file );
endforeach;
return $email->Send();
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Jefferson Costa
  • 188
  • 1
  • 9