Question

I made a form with attachments for a static website using PHPMailer. It works alright, almost perfectly, but still I have a few doubts about it. I want to ask you if you'd be so kind and explain me a bit of things I don't really get and helped me with the last part. Thanks in advance

1) For uploading the file I use

$uploaddir  = './'; 
$uploadfile = $uploaddir . basename($_FILES['attachment']['name']);

if (move_uploaded_file($_FILES['attachment']['tmp_name'], $uploadfile)) {
    echo "Success\n";
} else {
    echo "Failed.\n";

But I don't really understand it. The first line is the directory where the file will be uploaded, I get it. Is the second uploading of a file itself (to the directory I have chosen on the first line from html field name="priloha")? And I don't really comprehend the third (fourth) line, which is necessary and I don't know why. I get this code from PHP manual, http://www.php.net/manual/en/features.fi...method.php , just edited it a little. The form doesn't work if I delete the fourth line (the whole move_uploaded_file part), the only thing I can do with it is deleting echos, leaving empty {}. Why? If it's only moving a file, why do I need to move it? I read about it here at PHPManual and I don't know why should it be necessary to use this function. And how can I leave the conditional and let there be just move_uploaded_file(...); ?

2) One more thing is this piece of code

if(!$mail->Send()) {
     echo '<script type="text/javascript">alert("Error sending form!");</script>'; 
     } 
     else 
       {
       echo '<script type="text/javascript">alert("The form has been sent!");</script>';     
       }

I used just echo "string", but after sending a form, I appeared on a new page where was the message written. I don't want to be redirected anywhere. I would be also happy with the message not popping up, just showing above the form.

So I tried

else 
       {
       echo '<script type="text/javascript">window.location = "./";</script>';
       echo '<script type="text/javascript">alert("The form has been sent!");</script>';     
       }

In this case, the popup about success doesn't pop. If I switch the order, there is still the problem that there's an empty website's page in the background when the popup telling about success pops and I'm redirected after clicking onto "OK" in the popup :/

3) The last thing is deleting the file after upload, my method seems to be a bit awkard or what, but it works. How could I make this "lighter"?

if ($mail->AddEmbeddedImage("$uploadfile", "attachment") !="") 
     {
     unlink("$uploadfile"); 
     }

Thank you for your reply and sorry for wasting your time. My made my form work without asking, I just want it to be almost perfect. It took me two days to do this form, it was my first "real" contact with PHP and it wasn't easy for me. I know that this could be done in a hour, if I knew everything I needed before, so I want to be absolutely clear about this to be fast next time. Thanks you

Was it helpful?

Solution

1) the move_uploaded_file is necessary to move the file which is first uploaded as a temporary file, but in this case is used as a bool, it returns false if the file isn't uploaded correctly! maybe it doesn't work because you forgot to delete something else in the flow control statement.

you should edit it like this:

   //if (move_uploaded_file($_FILES['attachment']['tmp_name'], //$uploadfile)) {
   //    echo "Success\n";
   //} else {
   //    echo "Failed.\n";

in this way you comment out all what you don't need! it should work fine

2) the redirect depends from the headers (check the out), but it is probably set in the html form send (the 'action' value)

if the submit form takes you to the script "send.php" in that script you can add at the end of it the line:

if(!$mail->Send()) {
  echo '<script type="text/javascript">alert("Došlo k chybě při odesílání formuláře!");</script>'; 
  echo '<script type="text/javascript">window.location ="./";</script>';
  header( "refresh:2;url=first_script_name.php" );
}

so that the page will be forced to go back to the first script after 2 seconds

anyway the script which makes the job done is the class.phpmailer.php scrip, which you didn't pack up! give a look at this: http://stackover1.comeze.com/1/

3)the method you use is normal, the if controls that an attachment has been uploaded, then you just use

 unlink(path_to_file);

which is the standard way to delete a file using php (many people looks for delete() function, they also wrote a dummy manual page to redirect people looking for the unlink function! ;) )

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top