阅读邮件附件(xls和xlsx)

I am reading the mail from php, the mails are having xls or xlsx file so once I download the attatchment it is downloaded but when I try to read that xls or xlsx files so getting

File Not redable error.

Plese help how I can resolve this issue. Below is my code

$email_number = $emails[count($emails)-$k];
/* get information specific to this email */
$overview = imap_fetch_overview($connection,$email_number,0);   
$message = imap_fetchbody($connection,$email_number,2); 
/* get mail structure */
$structure = imap_fetchstructure($connection, $email_number);
$attachments = array();
/* if any attachments found... */
if(isset($structure->parts) && count($structure->parts)) 
{
    for($i = 0; $i < count($structure->parts); $i++) 
    {
        $attachments[$i] = array(
            'is_attachment' => false,
            'filename' => '',
            'name' => '',
            'attachment' => '',
            'typeval'   =>  $structure->parts[$i]->type
        );

        if($structure->parts[$i]->ifdparameters) 
        {                       
            foreach($structure->parts[$i]->dparameters as $object) 
            {
                if(strtolower($object->attribute) == 'filename') 
                {
                    $attachments[$i]['is_attachment'] = true;
                    $attachments[$i]['filename'] = $object->value;
                }
            }
        }

        if($structure->parts[$i]->ifparameters) 
        {
            foreach($structure->parts[$i]->parameters as $object) 
            {
                if(strtolower($object->attribute) == 'name') 
                {
                    $attachments[$i]['is_attachment'] = true;
                    $attachments[$i]['name'] = $object->value;
                }
            }
        }

        if($attachments[$i]['is_attachment']) 
        {
            $attachments[$i]['attachment'] = imap_fetchbody($connection, $email_number, $i+1);

            /* 3 = BASE64 encoding */
            if($structure->parts[$i]->encoding == 3) 
            { 
                $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
            }
            /* 4 = QUOTED-PRINTABLE encoding */
            elseif($structure->parts[$i]->encoding == 4) 
            { 
                $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
            }
        }
    }
}
/* iterate through each attachment and save it */
$j=0;
foreach($attachments as $attachment)
{
    if($attachment['is_attachment'] == 1)
    {
        $filename='';
        if(trim($attachment['filename'])!="")
            $filename=trim($attachment['filename']);
        elseif(trim($attachment['name'])!="")
            $filename=trim($attachment['name']);

        $ext = strtolower(substr(trim($filename), strrpos(trim($filename), '.') + 1));

        //$filename = $attachment['filename'];
        $filename = str_replace(" ","-",strtolower($companyArray[$excelSheet])).$j.$k.'.'.$ext;
        if(empty($filename)) $filename = $attachment['filename'];

        if(empty($filename)) $filename = time() . ".dat";
        $folder = "download";                               
        if(!is_dir($folder))
        {
             mkdir($folder);
        }
        $fp = fopen("./". $folder ."/". $filename, "w+");
        $data = strtr($attachment['attachment'], array('-' => '+', '_' => '/'));
        fwrite($fp, $data);
        fclose($fp);
        chmod("./". $folder ."/". $filename, 0644);
        $j++;
    }
}