Page 1 of 1

php mail: insert uploaded filenames into field

Posted: Tue Aug 16, 2011 12:09 pm
by cjkeane
hi everyone.

i have a form which allows multiple attachments to be sent with an email. i can insert a single filename into a db like so:

Code: Select all

<?php
$target_path = $target_path . basename($filename = $_FILES['resume']['name']);
$query = "INSERT INTO `attachments` VALUES ('$date', '$time', '$filename')";
$result = mysql_query($query) or die();
?>
however, i allow multiple attachments with the following code:

Code: Select all

<?php
foreach($_FILES as $userfile) {
        	$tmp_name = $userfile['tmp_name'];
         	$type = $userfile['type'];
         	$name = basename($userfile['name']);
         	$size = $userfile['size'];
		
		
        if (file_exists($tmp_name)) {
         // check to make sure that it is an uploaded file and not a system file
            if(is_uploaded_file($tmp_name)) {
	        	$file = fopen($tmp_name,'rb');
    			$data = fread($file,filesize($tmp_name));
				$uploaddir = 'upload/';
				// generate a set of 6 random characters and append it to the filename incase a file with the same
				// name already exists.
				$seed_array = array_merge(range('A','Z'),range(0,9));
				$rand_ary = array_rand($seed_array,6);
				$rand_str = '';
				foreach($rand_ary as $rk)
				$rand_str .= $seed_array[$rk];
				$fileName = $rand_str . '-' . $name; 
				move_uploaded_file($tmp_name, $uploaddir.str_replace(' ', '_', $fileName));
				fclose($file);
                $data = chunk_split(base64_encode($data));
            }
			
		}
?>
i need to insert filenames into mysql into a single field like so:
filename1.ext, filename2.ext, filename3.ext, etc

i'm unsure how to do that. does anyone have any ideas or links which may assist me? Thanks.

Re: php mail: insert uploaded filenames into field

Posted: Tue Aug 16, 2011 12:20 pm
by phphelpme
You could use a hidden field and build its value onSubmit, but if I were you, I'd simply use the array notation in the name attribute, and implode the array with '-' as glue : like this:

Code: Select all

<select name="date[year]"> ... <select name="date[month]"> ... <select name="date[day]"> ... 
then add to one value:

Code: Select all

$date = implode('-', $_POST['date']) //validate date format here 
Take a quick look here for examples: http://stackoverflow.com/questions/3102 ... submission

Best wishes

Re: php mail: insert uploaded filenames into field

Posted: Tue Aug 16, 2011 12:29 pm
by cjkeane
if i understand you correctly, i could do something like:

Code: Select all

<?php
<select name="filename['one']"> ... <select name="filename['two']"> ... <select name="filename['three']"> ... 
$filenames = implode(',', $_POST['filename'])
?>
correct?

Re: php mail: insert uploaded filenames into field

Posted: Tue Aug 16, 2011 12:39 pm
by phphelpme
Yes that would seem correct from what you have written. Give it a go and see what the results are.

Best wishes