Link to home
Start Free TrialLog in
Avatar of Kin Fat SZE
Kin Fat SZEFlag for Hong Kong

asked on

Why is becoming very slow when I using php move_upload function

Why is becoming very slow when I using php move_upload function
mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]] )!?
some solution is solve it!?
http://www.kennam.com/english/contact.php


<?
$clientemail = $_POST['email'] ;
$myemail = "test@kennam.com";

$message     = $_POST['message'] ;
$companyname = $_POST['companyname'] ;
$personname  = $_POST['personname'] ;
$phone       = $_POST['phone'] ;
$fax         = $_POST['fax'] ;
$addr1       = $_POST['addr1'] ;
$addr2       = $_POST['addr2'] ;
$country     = $_POST['country'] ;
$htmlmessage = ereg_replace("\n", "<br/>", $message);
?>

<!------------it doesn't matter------------------>

<!-----------------------------------upload file  work fine--------------------------------------->

<?
if (empty($_FILES["uploadfile"])) {
//            echo 'the file is empty <br/>' ;
} else{
//            echo 'the file is not emptry<br/>';
}

 if(!empty($_FILES["uploadfile"])){
    $uploaddir = "/var/www/html/uploads/";
 // $_FILES['uploadfile']['name'];
      $source = $_FILES["uploadfile"]["tmp_name"];
   $destination =  $uploaddir . $_FILES["uploadfile"]["name"];

    if(move_uploaded_file($source, $destination)){
//            echo "Updated!";    
       }else{
            echo "There was a problem when uploding the new file";
            print_r($_FILES);
          }
  }
?>

<!-----------------------------------it's work----------------------------------------->


<!------------become slow------------------>
<!-----------------------------------TO CLIENT EMAIL----------------------------------------->
<?
$mailSubject = 'To ' . $companyname ;
   
$mailHeaders = "From : $myemail \r\n";
$mailHeaders.= "Content-Type: text/html; charset=big5 ";
$mailHeaders .= "MIME-Version: 1.0 ";

$mailBody  = "Dear $personname <br/><br/>";
$mailBody .= "Thanks for your enquiry <br/>";
$mailBody .= "We will reply this email as soon as possible <br/><br/><br/>";
$mailBody .= "Our Email : <a href='mailto:info@kennam.com'>info@kennam.com</a>, <a href='mailto:manager@kennam.com'>manager@kennam.com</a> <br/>";
$mailBody .= "Our Web site : <a href='http://www.kennam.com/'>http://www.kennam.com/</a> <br/>";
$mailBody .= "KEN NAM INDUSTRIAL LIMITED <br/><br/>"  ;
$mailBody .= "Best Regards, <br/><br/>";
$mailBody .= "C.K.SZE <br/>";

mail($clientemail, $mailSubject, $mailBody, $mailHeaders);
?>

<!----------------------------END OF TO CLIENT EMAIL (success)------------------------------->


<!------------become slow------------------>
<!-----------------------------------TO manager@kennam.com EMAIL----------------------------------------->

<?
$mailHeaders = "From: " . $clientemail . "\r\n";
$mailHeaders.= "Content-Type: text/html; charset=big5 ";
$mailHeaders .= "MIME-Version: 1.0 ";

$mailSubject = 'From :' . $companyname;

$mailBody  = "From : $companyname  <br/>";
$mailBody .= "Mr/Mrs/Ms $personname   <br/>";
$mailBody .= "Tel : $phone <br/>";
$mailBody .= "Fax : $fax   <br/>";
$mailBody .= "Address : $addr1 <br/>  $addr2  <br/>";

if (!($country == '?')) {
      $mailBody .= "Country : $country <br/>";       
}

$mailBody .= "Message : <br/><br/> $htmlmessage <br/>";

mail($myemail, $mailSubject, $mailBody, $mailHeaders);


//$attachments=Array(Array("file"=>$uploaddir . $_FILES["uploadfile"]["name"], "content_type"=>"multipart/alternative"));
//send_mail($myemail, $clientemail, $mailSubject . 'test' , $mailBody, $attachments);
?>

<!----------------------------END OF TO manager@kennam.com EMAIL (success)------------------------------->
Avatar of hernst42
hernst42
Flag of Germany image

are you running on a linux or windows server? Guess the mail command takes long. You can measure that part like:

$start = microtime(true);
mail($clientemail, $mailSubject, $mailBody, $mailHeaders);
echo "Sending mail took " . (microtime(true)-$start) . "s";

In this case ther is a problem on the server with the mail configuration.
Also, if the file is a BIG file or an archive, you may have AV scanning taking place. On an archive, this may be noticeable.

How do you know it has gone slow?
Avatar of Kin Fat SZE

ASKER

I have tried, it doesn't  matter of file uploading
Sending mail took 121.25874900818s
linux fedora,
any help!?
I want to solve it as soon as possible
Could you try adding this code ...

if (DEBUG) { file_put_contents($s_DegugFilename, __FILE__ . ':' . __LINE__ . ':' . microtime(True), FILE_APPEND); }

at every line.

At the top of the script ...

// If you are using sessions, then ignore the next line.
session_start();
define('DEBUG', True); // Set to false to NOT debug.
if (DEBUG) { $s_DebugFilename = './' . session_id . '.log'; }

This will quickly show which lines are slowing you down. Just so you can be absolutely sure.
when I run mail function
mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]] )

It token a long times, but when I visit hotmail, yahoo, there is fast.

I got
upload file took 3.504753112793E-05s
Sending to client took 61.135215997696s
Sending info@kennam.com took 60.316831111908s

by statements
$start = microtime(true);
mail($clientemail, $mailSubject, $mailBody, $mailHeaders);
echo "Sending mail took " . (microtime(true)-$start) . "s";
There is a problem with the mail configuration. The installed mailsystem takes too long to accept and queue the mail. The php-mail command open the process defined in sendmail_path and give the content to that process. So this process takes up to 60s (guess the process waits for a certain information via network like resolving a hostname). Ask your system administrator to check for  that.

 This is not a php issue its a problem with you mail-system configuration on that server.
So, can you give me some more details how to configure mail server!?
The Web site and The server fedora 6 was build by myself. I havn't configure the mail system right now.
 I would like to configure the mail server also If possible.
anybody help!?
I'm on Windows and there we don't use sendmail. I use a combination of htmlmimemail5 from phpguru.org and my replacement for the non-existent getmxrr() function (not on windows - on *ix fine!).

With this, I ini_set('SMTP', 'aaa.bbb.ccc.ddd') to the IP address of the recipients SMTP server.

This SHOULD be what sendmail does (I think).


The idea is that you use getmxrr() to find the list of Mail eXchange servers responsible for the recipients domain.

You then set the SMTP to one of the addresses that have MX records.

With that, you are sending the mail directly to the SMTP server responsible, or at least the publicly advertised SMTP server. What happens after that is not your concern/responsibility, but it is also normally the last place you have any influence.

For this to operate, get the domain of the email address you are sending to.
Put it into getmxrr() like ...

$b_Found = getmxrr ( $s_Domain, $a_MXHosts, $a_Weights);

I don't know if the weights should be lowest to highest, but, choose an order and sort the MX Hosts in the same order - array_multisort should do that for you ...

array_multisort($a_MXHosts, $a_Weights);

Now you can iterate the hosts to see send the email. If you recorded the amount of time it takes along with the MXHosts and Weights, you COULD build a profile system.

If you are sending hundreds of email an hour, then this sort of detail could be useful. Find and ignore the slow/dead hosts, recheck once every hundred emails or something.

You have one major advantage - you know when the mail has been sent.

You have one major disadvantage - you do not have a built in queue mechanism like you would have if you used a local SMTP relay.

The choice is yours.


Dear RQuadling

I am using linux fedora 6.
any other ideas?

Thanks
How to configure your mailserver should be asked here:
https://www.experts-exchange.com/OS/Linux/Distributions/Red_Hat/
You could try using a mail class and tell it to use SMTP rather than sendmail.

The only trick then is to set the ini_set('SMTP', ....); bit.

As PHP's built in mail() will use sendmail by default (on *IX), you have no choice if that's what you intend to use.

The htmlmimemail5 class at http://www.phpguru.org/ will allow you to NOT use that and say you want to use the SMTP  protocol directly - it does all the socket stuff for you - effectively bypassing sendmail and talking directly to the recipient's SMTP server.

It is the only other thing I can think of within PHP.

Did you try running the "profiler" to see what is slow?
over all, you means using SMTP RATHER than mail function of php, right?
what's the statement of smtp of sending mail?
I means the statement "mail($clientemail, $mailSubject, $mailBody, $mailHeaders);" like this.

I do not understand what mean of "profiler" .
I am using statments
$start = microtime(true);
mail($clientemail, $mailSubject, $mailBody, $mailHeaders);
echo "Sending mail took " . (microtime(true)-$start) . "s";
to test the time token
ASKER CERTIFIED SOLUTION
Avatar of Richard Quadling
Richard Quadling
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You means using getmxrr function will find out client's SMTP server and
using $obj_Mail->send(); to send the mail out

what is value $s_Domain!?
what is the meaning of
$a_MXHosts = array();
$a_Weights = array();

More or less - yes.

Read the getmxrr() function in the PHP manual for the full info (http://www.php.net/manual/en/function.getmxrr.php), the above code is also in a user note there.

But in summary.

getmxrr() returns a list of SMTP servers assigned to a particular domain (MX - Mail eXchange records). Each server is given a weight (I don't know which way the weights work).

By setting the PHP SMTP setting (via ini_set) to one of the servers dealing with that domain, you get your post directly to the required server.