I am patching together a script to attach one or more files to an email. Which files get attached depends on a form with checkboxes.

I've got an array with the filenames (based upon the form values):

  // see which boxes are checked
foreach($_POST['check'] as $value) {
$files = array($value);
}

...but I need to get the array to look like this for the script I'm using:

// array with filenames to be sent as attachment
$files = array("file_1.ext","file_2.ext","file_3.ext",......);

$just=count($files);
for($x=0;$x<count($files);$x++){
$file = fopen($files[$x],'rb');
$data = fread($file,filesize($files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .=Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
if($x==($just-1))
{
$message .= "–{$mime_boundary}-\n";
} else
{
$message .= "–{$mime_boundary}\n";
}

}

Unless of course there is another way to use the array w/o quotes/commas.

Thanks...

    The first piece of code doesn't really make an array of values, it just puts the last value in the array, but maybe it's a typo and not your real code.

    The second piece of code doesn't really have anything to do with making the array look like anything.

    But you can go through an array of strings and add quotes to them using the usual method for concatenating strings, and you can use [man]join[/man] to join them together into one comma-separated string. In fact, "'".join("','", $array)."'" would do both at the same time.

      I now realize $_POST['check'] is an array...

        Write a Reply...