User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 329,052 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,589 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting

Serious problem with attchments-please help

Join Date: Mar 2008
Posts: 151
Reputation: Suomedia is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 19
Suomedia Suomedia is offline Offline
Junior Poster

Re: Serious problem with attchments-please help

  #15  
Mar 25th, 2008
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.
If you want your dreams to come true, the first thing you must do is to wake up....
Suomedia - Dynamic Content Management
Reply With Quote  
All times are GMT -4. The time now is 5:46 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC