ballouta Posted June 18, 2008 Share Posted June 18, 2008 Hello I am using IMAP functions to manage some emails. I sent an email with a zip file of 29KB. The function detects the attachment and copy it to an exisiting directory. The problem is that this file becomes 1KB, i downloaded it to test it, it says it is damaged! WHY? parts of the code: $SaveFileDirectory = "/home/gsclteam/public_html/stk/"; ...... foreach ($attachment as $attach) { WriteToFile($SaveFileDirectory . $attach['filename'], imap_base64($attach['filedata'])); echo "Attachment found: " . $attach['filename'] . "<br>"; $myf=$attach['filename']; } Please please Help. Link to comment https://forums.phpfreaks.com/topic/110806-file-problem/ Share on other sites More sharing options...
ballouta Posted June 18, 2008 Author Share Posted June 18, 2008 please help Link to comment https://forums.phpfreaks.com/topic/110806-file-problem/#findComment-568585 Share on other sites More sharing options...
hitman6003 Posted June 19, 2008 Share Posted June 19, 2008 All of your file writing is done in the function "WriteToFile", which you haven't provided code for. We can not help you without the relevant code. Link to comment https://forums.phpfreaks.com/topic/110806-file-problem/#findComment-568674 Share on other sites More sharing options...
ballouta Posted June 19, 2008 Author Share Posted June 19, 2008 Here's my code, I know it is alittle bit long. but easy to trace after the code reads emails in my inbox (only one message here), it copies the attachment to a directory, extraxt it, loop over the directory to process some DB issues accodrdingly. <?php include('CMS/global.inc.php'); $user= $_GET['user']; $SaveFileDirectory = "/home/gsclteam/public_html/"; function WriteToFile($file,$contents) { // IF the contents is empty lets just make it one space if ($contents=='' || empty($contents)) $contents = " "; // $file is the full path to the file $fh = fopen($file, "w") or die("Could not open file!"); fwrite($fh, $contents) or die("Could not write to file"); fclose($fh); } //check for new messages $mailbox = imap_open("{localhost:143/notls}INBOX", "EMAIL", "PASSWORD") or die("Can't connect: " . imap_last_error());; // Check messages $check = imap_check($mailbox); // viewing number of messages in inbox $chec = imap_mailboxmsginfo($mailbox); $headers = @imap_headers($mailbox) or die("Couldn't get emails"); $numEmails = sizeof($headers); echo "You have $numEmails messages in your mailbox <br><br><br>"; // SHowing headers //for($j = 1; $j >=1; $j--) //{ $j=1; $header = imap_header($mailbox, $j); $from = $header->from; foreach ($from as $id => $object) { $fromname = $object->personal; echo "<b>From Name:</b> $fromname<br>"; $fromaddress = $object->mailbox . "@" . $object->host; echo "<b>From Address:</b> $fromaddress<br>"; } $subject = strip_tags($header->subject); echo "<b>Subject:</b>$subject </br>"; $date = $header->date; echo "<b>Date:</b>$date</br> "; if ($check->Nmsgs=='0') { echo "There are no messages that need to be downloaded"; } else { $emailIndex = $j; /* Lets find the email body */ $struct = imap_fetchstructure($mailbox, $emailIndex); $parts = $struct->parts; $i = 0; if (!$parts) { /* Simple message, only 1 piece */ $attachment = array(); /* No attachments */ $Email_Comments = imap_body($mailbox, $emailIndex); } else { /* Complicated message, multiple parts */ $endwhile = false; $stack = array(); /* Stack while parsing message */ $Email_Comments = ""; /* Content of message */ $attachment = array(); /* Attachments */ while (!$endwhile) { if (!$parts[$i]) { if (count($stack) > 0) { $parts = $stack[count($stack)-1]["p"]; $i = $stack[count($stack)-1]["i"] + 1; array_pop($stack); } else { $endwhile = true; } } if (!$endwhile) { /* Create message part first (example '1.2.3') */ $partstring = ""; foreach ($stack as $s) { $partstring .= ($s["i"]+1) . "."; } $partstring .= ($i+1); if (strtoupper($parts[$i]->disposition) == "ATTACHMENT") { /* Attachment */ $attachment[] = array("filename" => $parts[$i]->parameters[0]->value, "filedata" => @imap_fetchbody($mailbox, $emailIndex, $partstring)); } elseif (strtoupper($parts[$i]->subtype) == "PLAIN") { /* Message */ $Email_Comments .= imap_fetchbody($mailbox, $emailIndex, $partstring); echo "<b>Email Body:</b> $Email_Comments <br/><br/>"; //$daouk=$Email_Comments; // my code //$emailIndex +=1; } } if ($parts[$i]->parts) { $stack[] = array("p" => $parts, "i" => $i); $parts = $parts[$i]->parts; $i = 0; } else { $i++; } } /* while */ } /* complicated message */ foreach ($attachment as $attach) { WriteToFile($SaveFileDirectory . $attach['filename'], imap_base64($attach['filedata'])); echo "Attachment found: " . $attach['filename'] . "<br>"; $myf=$attach['filename']; } } // end else //Step 1mkdir("stk/temp", 0700); //Step 2 if (!copy("/home/gsclteam/public_html/$myf", "/home/gsclteam/public_html/stk/$myf")) { echo "failed to copy $file...\n"; } //Extracting the archive require_once('pclzip.lib.php'); $archive = new PclZip("$myf"); if ($archive->extract(PCLZIP_OPT_PATH, "stk/temp/", PCLZIP_OPT_REMOVE_PATH, 'install/release') == 0) { die("Error : ".$archive->errorInfo(true)); } else echo "working! </br>"; // I should delete the zip file in www after uncompressing it //Open directory anf check for .ask file if ($handle = opendir('stk/temp')) { while (false != ($file=readdir($handle))) { if ( ($file != ".") && ($file != "..") ) { $array = explode( '.' , $file ); if ($array[1] = 'ask') { // copy the extracted files to the user's file. @copy("stk/temp/$array[0].$array[1]", "stk/$user/$array[0].$array[1]"); @copy("stk/temp/$array[0]nss.rpl", "stk/$user/$array[0]nss.rpl"); @copy("stk/temp/$array[0].rpl", "stk/$user/$array[0].rpl"); $query = "UPDATE `membersfiles` SET `recfile` = '$array[0]nss.rpl' WHERE `user` = '$user' AND `sentfile` = '$file' AND `fla` = 'M' "; $result = mysql_query($query); $query = "SELECT * FROM `uploaded` WHERE `sentfile` = '$file' AND `received` = 'N' "; $result = mysql_query($query); $row = mysql_fetch_array($result); if (mysql_num_rows($result) > 0) { //echo "Data found"; $date = date("y-m-d"); $query = " UPDATE `gsclteam_gsteam`.`uploaded` SET `received` = 'Y', `date` = '$date' WHERE `uploaded`.`user` = '$user' AND `sentfile` = '$file'"; $result = mysql_query($query); $query = "UPDATE `gsclteam_gsteam`.`membersfiles` SET `recfile` = '$array[0].rpl', `date` = '$date' WHERE `membersfiles`.`user` = '$user' AND `sentfile` = '$file' AND `fla` = 'D' "; $result = mysql_query($query); echo "User before copying is: $user"; } else { //echo "No data found for the User $ user"; } }//end if we found the .ask file in the uploaded table echo "<br><br><br>"; } } } // end open Directory imap_delete($mailbox, $j); imap_expunge($mailbox); //} // end for ?> Please HELP Link to comment https://forums.phpfreaks.com/topic/110806-file-problem/#findComment-568787 Share on other sites More sharing options...
hitman6003 Posted June 19, 2008 Share Posted June 19, 2008 Have an error condition created if the "contents" of the file is empty...this will let you know that the problem is before the writing of the file... // IF the contents is empty lets just make it one space if ($contents=='' || empty($contents)) die("File " . $file . " was sent to be written, but it's empty"); If that occurs, then you the problem is probably in the retrieving of the attachment from the mail server. Link to comment https://forums.phpfreaks.com/topic/110806-file-problem/#findComment-569024 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.