Tuesday, 15 January 2013

Upload multiple files with PHP -



Upload multiple files with PHP -

i'm trying utilize code upload multiple files php 5.5 , attach email. if take brackets file[] (so it's file) on name attribute of input field, i'm able upload 1 file; if set brackets on, page gives warning (for line phone call file_get_contents()) , attaches file email called array.dat not selected file. doing wrong?

warning: file_get_contents() expects parameter 1 valid path, array given in c:\wamp\www\myapp\submit.php on line 188

html:

<input id="file" name="file[]" type="file" multiple="true">

php:

$fullmessage = "this multi-part fullname in mime format. --_1_$boundary content-type: multipart/alternative; boundary=\"_2_$boundary\" --_2_$boundary content-type: text/plain; charset=\"iso-8859-1\" content-transfer-encoding: quoted-printable $msg_quoted --_2_$boundary--\r\n"; $msg_b64 = base64_encode($msg); $fullmessage .= "--_1_$boundary content-type: application/octet-stream; name=\"".$date->format('u')."-".$company."-".$custname."-".$email.".txt\" content-transfer-encoding: base64 content-disposition: attachment $msg_b64\r\n"; foreach($_files $f){ if($f['size'] > 0){ $attachment = chunk_split(base64_encode(file_get_contents($f['tmp_name']))); $name = $f['name']; $fullmessage .= "--_1_$boundary content-type: application/octet-stream; name=\"$name\" content-transfer-encoding: base64 content-disposition: attachment $attachment\r\n"; } } $fullmessage .= "--_1_$boundary--"; $mail_options = ''; $mail_options .= 'o deliverymode=b'; //tells sendmail run asynchronously mail(email_recipients, $subject, $fullmessage, $headers, $mail_options);

update: fixed it, @i'l'i:

// process uploaded files. $num_files = count($_files['file']['tmp_name']); for($i=0; $i < $num_files; $i++){ if($_files['file']['size'][$i] > 0){ $attachment = chunk_split(base64_encode(file_get_contents($_files['file']['tmp_name'][$i]))); $name = $_files['file']['name'][$i]; $fullmessage .= "--_1_$boundary content-type: application/octet-stream; name=\"$name\" content-transfer-encoding: base64 content-disposition: attachment $attachment\r\n"; } }

i think problem more matter of error indicates:

warning: file_get_contents() expects parameter 1 valid path, array given in c:\wamp\www\myapp\submit.php on line 188.

file_get_contents expects string, not array.

so there's couple of things can prepare this:

either utilize right string:

$attachment = chunk_split(base64_encode(file_get_contents($f)));

or utilize key of $_files array

for ($x=0; $x<sizeof($_files); $x++) { $attachment = chunk_split(base64_encode(file_get_contents($_files[$x]))); }

php

No comments:

Post a Comment