PHP File Attachment Size

I am using imap_fetchbody to get the attachments out of an e-mail.... but if the attachment is too large, I get an "Internal Server Error"... I am sure this is because of a timeout or something...

so 2 questions,

1.) How do i set the timeouts so this does not happen....
2.) How can I get the attachment file size before I grab it with the "imap_fetchbody" function?
LVL 1
JoshWegenerAsked:
Who is Participating?
 
ahoffmannCommented:
> PHP is part of the server, ..
no, it just may be a module inside the server
you can set the limit for that module (or CGI) but not the limit for server calling the module (or CGI) from within php (a child cannot change it's parent, obviously:)
Things might be different if you web server is PHP itself ...
0
 
OscurochuCommented:
set_time_limit(0) disables timeouts. (http://us.php.net/manual/en/function.set-time-limit.php)
0
 
ygouthamCommented:
in the php.ini file there is always a restriction on fileupload sizes

there are also restrictions on individual scripts using certain amount of memory and execution time

probably you need to look at all this and then restart the httpd process so that the new php.ini takes effect

if the file were to be so large that it exceeds the maximum execution time or the default browser timeout then there is not much that you can do.  
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
ahoffmannCommented:
timeout has to be set on the server, I doubt that the application (PHP here) can change this value

for the IMAP mail sizes: you have to issue an IMAP command (if your IMAP server supports it) like:
   b42 uid fetch 1:* (flags rfc822.size)

don't know how this is done with PHP, but I guess that there is a function for it (somewhere, beside the infinite other functions:)
0
 
OscurochuCommented:
ahoffmann, PHP does have a function that changes (or disables) timeouts. PHP is part of the server, hence I don't doubt that php would be able to change the value.
0
 
hernst42Commented:
1) maybe you don't get a timeout, more a memory-limit execeeced. Check memory_limit on your php-server
2) To get the size of a message see http://www.php.net/manual/en/function.imap-fetchstructure.php
0
 
JoshWegenerAuthor Commented:
1.) How do i set the timeouts so this does not happen....
     A:) Change the PHP.ini file

2.) How can I get the attachment file size before I grab it with the "imap_fetchbody" function?
     A:) ????????
0
 
JoshWegenerAuthor Commented:
After much work.. I found it....

$struckture->parts[$key]->bytes


----------------------------------------------------------------
      function GetAttech($mid,$path,$id) // Get Atteced File from Mail
      {
            $struckture = imap_fetchstructure($this->marubox,$mid);
            $ar="";
            foreach($struckture->parts as $key => $value)
            {
                  $enc=$struckture->parts[$key]->encoding;
                  if($struckture->parts[$key]->ifdparameters)
                  {
                        $name=$struckture->parts[$key]->dparameters[0]->value;
                        $message = imap_fetchbody($this->marubox,$mid,$key+1);
                        if ($enc == 0)
                              $message = imap_8bit ($message);
                        if ($enc == 1)
                              $message = imap_8bit ($message);
                        if ($enc == 2)
                              $message = imap_binary ($message);
                        if ($enc == 3)
                              $message = imap_base64 ($message);
                        if ($enc == 4)
                              $message = quoted_printable_decode($message);
                        if ($enc == 5)
                              $message = $message;
                        if ( ($struckture->parts[$key]->bytes / 1024 / 1024) > 2 ) {
                              $name = 'Upload Too Large.jpg';
                        } else {
                              if ( strlen($name) >= 200 )
                                    $name = substr($name, -200);
                              $name = '1' . $id . rand(5,1000) . '3_' . $name;
                              $fp=fopen($path.$name,"w");
                              fwrite($fp,$message);
                              fclose($fp);
                        }
                        $ar=$ar.$name.",";
                  }
            }
            $ar=substr($ar,0,(strlen($ar)-1));
            return $ar;
      }
----------------------------------------------------------------
0
 
JoshWegenerAuthor Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.