Something like this:
$path = '/home/suomedia/public_html/user_images/'; // the path to upload images (must be writable)
$filepath = $path . basename( $_FILES['Picture']['name']) ;
$len = strlen($_FILES['Picture']['name']) - 4;
$extension = substr($_FILES['Picture']['name'],$len); // get the file extension
$permitted = array('.jpg', '.JPG', '.PNG', '.png', '.GIF', '.gif'); // only alow these file extensions
$success = false;
if (in_array($extension, $permitted)) {
if(move_uploaded_file($_FILES['Picture']['tmp_name'], $filepath)) {
$success = true;
$fileatt_type = 'image/' . $extension;
$picture_name = $_FILES['Picture']['name'];
$file = fopen( $filepath, 'rb' );
$data = fread( $file, filesize( $filepath ) );
fclose( $file );
$data = chunk_split( base64_encode( $data ) );
$rand = md5( time() );
$mime_boundary = '==Multipart_Boundary_x{$rand}x';
} else {
$success = false;
}
} else {
$success = false;
}
// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$picture_name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$picture_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
$path must be a path on your server to a writable directory.
You also need to sanitize all user input from your form and decide what to do if $success = false.
Matti Ressler
Suomedia
Last edited by Suomedia : Mar 25th, 2008 at 12:29 pm.