CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: [RESOLVED] mobile upload script

  1. #1

    [RESOLVED] mobile upload script

    so i have the following code and i was wondering if someone could help me. what i want is to be able to check an email address and have any pics/vids that are emailed to post onto a webpage that ppl can view.. i cannot seem to figure this out. couldl i get some help thansk alot in advance

    PHP Code:
    <?php
    $cfg 
    = array();
    ######## CONFIG #######
    $cfg['upload_dir'] = 'demo'//what directory to upload images to $cfg['mail_server'] = 'localhost'; //email server $cfg['mail_port'] = '110'; //email server port

    /* email server services as described on http://php.net/imap_open */ 
    $cfg['mail_services'] = '/pop3/notls';

    $cfg['mail_username'] = 'mobile'//username for the email address to check $cfg['mail_password'] = 'mobile1'; //password for above user $cfg['debug'] = false; //true or false for debugging

    /* Accepted File Types: if less than four characters, use a dot as the first char. */ 
    $cfg['accepted'] = array('.3g2','.png','.jpg','.jpe','jpeg','.gif');
    ######## END CONFIG #########

    $pid=2;

    $type = array('text''multipart''message''application''audio''image''video''other'); $encoding = array('7bit''8bit''binary''base64''quoted-printable''other');

    if(!
    is_dir($cfg['upload_dir'])) {
      @
    mkdir($cfg['upload_dir']) or die('Cannot create directory:
    '
    .$cfg['upload_dir'].'! Make sure the parent folder has write permissions'); }

    // open POP connection
    $inbox = @imap_open
    ('{'.$cfg['mail_server'].':'.$cfg['mail_port'].$cfg['mail_services'].'}',
    $cfg['mail_username'], $cfg['mail_password']) or die('Connection to server failed.');

    // parse message body
    function parse($structure) {
    global 
    $type;
    global 
    $encoding;

    // create an array to hold message sections $ret = array();

    // split structure into parts
    $parts $structure->parts;

     for(
    $x=0$x<sizeof($parts); $x++) {
      
    $ret[$x]['pid'] = ($x+1);
      
    $that $parts[$x];

    // default to text
      
    if ($that->type == '') {
       
    $that->type 0;
      }
      
    $ret[$x]['type'] = $type[$that->type] . '/' strtolower($that->subtype);

    // default to 7bit
      
    if ($that->encoding == '') {
       
    $that->encoding 0;
      }

      
    $ret[$x]['encoding'] = $encoding[$that->encoding];
      
    $ret[$x]['size'] = strtolower($that->bytes);
      
    $ret[$x]['disposition'] = strtolower($that->disposition);

      if (
    $that->ifparameters == 1) {
       
    $params $that->parameters;

       foreach (
    $params as $p) {
        if(
    $p->attribute == 'NAME') {
         
    $ret[$x]['name'] = $p->value;
         break;
        }
       }
      }
     }
     return 
    $ret;
    }

    function 
    get_attachments($arr) {
     for(
    $x=0$x sizeof($arr); $x++) {
      if(
    $arr[$x]['disposition'] == 'attachment') {
       
    $ret[] = $arr[$x];
      }
     }
     return 
    $ret;
    }

    $count = @imap_num_msg($inbox) or die('No Messages in mailbox!'); //echo 'count: ' . $count;

    // get message headers and structure
    for ($c 1$c <= $count$c++) {
     
    $id $c;
     
    $headers imap_header($inbox$id);
     
    $structure imap_fetchstructure($inbox$id);

    // if multipart, parse
     
    if(sizeof($structure->parts) > 1) {
      
    $sections parse($structure);
      
    $attachments get_attachments($sections);  }

    if (
    $cfg['debug']) {
     echo 
    'Structure of message: ' $id '<BR><pre>';  print_r($structure);  echo '</pre><BR>';  echo 'Sections of message: ' $id '<BR><pre>';  print_r($sections);  echo '</pre><BR>'; }

    // look for specified part
     
    for($x=0$x<sizeof($sections); $x++) {
      if(
    $sections[$x]['pid'] == $pid) {
        
    $dtype $sections[$x]['type'];
        
    $dencoding $sections[$x]['encoding'];
        
    $filename $sections[$x]['name'];
      }
     }

    if (
    $cfg['debug']) {
     echo 
    ' type: ' $dtype '<BR>';
     echo 
    'encoding: ' $dencoding '<BR>';  echo 'filename: ' $filename '<BR>';  echo ' id: ' $id '<BR><BR>'; }

    $attachment imap_fetchbody($inbox$id$pid);

    if (!
    $cfg['debug']) {

     
    //if (in_array(substr($filename, -4), $cfg['accepted'])) {
      
    if ($dencoding == 'base64') {
      
    // Decode and save attachment to a file
       
    list($usec$sec) = explode(' 'microtime());
       
    $usec substr($usec,2,3);
       
    $name date('Ymd.His');
       
    $fp=fopen($cfg['upload_dir'].'/'.$name.'_'.$filename,'w');
       
    fwrite($fp,imap_base64($attachment));
       
    fclose($fp);
      }
     
    //}
    }

    }

    if (!
    $cfg['debug']) {
    // delete all emails
    for ($i 1$i <= $count$i++) {
    imap_delete($inbox$i);
    }
    imap_expunge($inbox);
    }

    imap_close($inbox);

    if (!
    $cfg['debug']) {
    header('Location: '.$cfg['upload_dir']); } ?>
    Last edited by PeejAvery; December 7th, 2007 at 07:44 AM. Reason: Changed code tags to PHP tags.
    New to PHP and MySql. Started looking at scripts about a week ago. So far have very little understanding of it. So please be easy with me if I ask a stupid question.

    www.ethans-space.com

  2. #2
    Join Date
    Aug 2002
    Posts
    879

    Re: mobile upload script

    As far as I get it you want to
    - check an email account for mails with attached pics/vids
    - upload this attached pics/vids on a website

    So what is the problem with the posted code in particular?
    - mail checking is not working
    - extracting the attached pic/vid is not working
    - upload dosn't work
    - ........

    Please post some more information.

  3. #3

    Re: mobile upload script

    Actually i got it to work thanks for your reply though. appreciate it.
    New to PHP and MySql. Started looking at scripts about a week ago. So far have very little understanding of it. So please be easy with me if I ask a stupid question.

    www.ethans-space.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)