7 Replies Latest reply on Jul 28, 2008 7:28 AM by Newsgroup_User

    PHP Script Help?

    TGC80104 Level 1
      Problem with the following php script. I want the name of the uploaded file to appear in the subject line and the body of the email it generates. It seems as though the script is supposed to write the filename in the confirmation (see first if/echo statement), but that doesn't happen. Also, is there an easy way to redirect it to a custom confirmation page?
      Any help would be greatly appreciated!
      Thanks
        • 1. Re: PHP Script Help?
          Level 7
          The first if/echo is not writing that information into anything other than
          the current page. Depending on what else is happening there, you may never
          see it. The uploaded file's name is given by basename(
          $_FILES['uploaded']['name']) , so if you say -

          $subject = 'New File Upload' . basename( $_FILES['uploaded']['name']) ;

          You should see that filename appear in the subject line. You can take a
          similar approach for the $message variable.

          > Also, is there an easy way
          > to redirect it to a custom confirmation page?

          You mean after the email is sent?

          add this -

          header("Location: custom-confirmation.php");
          exit();

          immediately after the mail($to, $subject, $message, $headers); line.

          --
          Murray --- ICQ 71997575
          Adobe Community Expert
          (If you *MUST* email me, don't LAUGH when you do so!)
          ==================
          http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
          http://www.dwfaq.com - DW FAQs, Tutorials & Resources
          ==================


          "tomcomber@comcast.net" <webforumsuser@macromedia.com> wrote in message
          news:g689k6$css$1@forums.macromedia.com...
          > Problem with the following php script. I want the name of the uploaded
          > file to
          > appear in the subject line and the body of the email it generates. It
          > seems as
          > though the script is supposed to write the filename in the confirmation
          > (see
          > first if/echo statement), but that doesn't happen. Also, is there an easy
          > way
          > to redirect it to a custom confirmation page?
          > Any help would be greatly appreciated!
          > Thanks
          >
          > <?php
          > $target = "upload/";
          > $target = $target . basename( $_FILES['uploaded']['name']) ;
          > $ok=1;
          > $to = 'enter email address';
          > $subject = 'New File Upload';
          > $message = 'A new file has been uploaded to DIRECTORY NAME';
          > $headers = 'From: enter email address' . "\r\n" .
          > 'Reply-To: enter reply-to address' . "\r\n" .
          > 'X-Mailer: PHP/' . phpversion();
          >
          > if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
          > {
          > echo "The file ". basename( $_FILES['uploaded']['name']). " has been
          > uploaded";
          > mail($to, $subject, $message, $headers);
          > }
          > else {
          > echo "Sorry, there was a problem uploading your file.";
          > }
          > if ($uploaded_type =="text/php")
          > {
          > echo "No PHP files<br>";
          > $ok=0;
          > }
          > if ($uploaded_type =="text/html")
          > {
          > echo "No HTML files<br>";
          > $ok=0;
          > }
          > ?>
          >

          • 2. PHP Script Help?
            TGC80104 Level 1
            I tried your suggestions, but no go. I'm sure I did something wrong. The revised script is below. Should the $subject and $message variables include the $target variable as well? The script contains two $target variables. Seems like one designates the directory the which contains the uploaded files, while the next designates the directory AND the filename. Example, from the scripts first two lines:

            $target = "upload/";
            $target = $target . basename( $_FILES['uploaded']['name']) ;

            So, maybe...

            $subject = 'New File Upload' $target . basename( $_FILES['uploaded']['name']);

            Or not?
            Forgive my ignorance, but I'm a complete PHP newbie.
            Thanks for your help!

            <?php
            $target = "upload/";
            $target = $target . basename( $_FILES['uploaded']['name']) ;
            $ok=1;
            $to = 'enter email address';
            $subject = 'New File Upload'. basename( $_FILES['uploaded']['name']);
            $message = 'A new file' . basename( $_FILES['uploaded']['name']) 'has been uploaded to the UPLOAD folder';
            $headers = 'From: enter email address' . "\r\n" .
            'Reply-To: enter repl-to address' . "\r\n" .
            'X-Mailer: PHP/' . phpversion();


            if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
            {
            echo "The file ". basename( $_FILES['uploaded']['name']). " has been uploaded";
            mail($to, $subject, $message, $headers);
            header("Location: custom-confirmation.php");
            exit();
            }
            else {
            echo "Sorry, there was a problem uploading your file.";
            }
            if ($uploaded_type =="text/php")
            {
            echo "No PHP files<br>";
            $ok=0;
            }
            if ($uploaded_type =="text/html")
            {
            echo "No HTML files<br>";
            $ok=0;
            }
            ?>
            • 3. Re: PHP Script Help?
              TGC80104 Level 1
              Oh, I should mention that I AM getting the emails, but the message body and subject line contain no filenames, only the "New File Upload" and "A new file has been uploaded to DIRECTORY NAME", respectively.

              The redirect doesn't work, either. Just shows me a blank white page with "The file has been uploaded" as it originally did.

              The files themselves DO get uploaded.
              • 4. Re: PHP Script Help?
                Level 7
                The $target variable contains the path and filename for the uploaded file.
                The filename itself is given by the basename() function, e.g.,

                <?php
                $path = "/home/httpd/html/index.php";
                $file = basename($path); // $file is set to "index.php"
                $file = basename($path, ".php"); // $file is set to "index"
                ?>

                So - if you want that filename to appear in the subject of the email, you
                would do it like this -

                $subject = 'New File Upload' . ' ' . basename( $_FILES['uploaded']['name']);

                In other words, concatenate the text 'New File Upload' with a space, and
                then with the filename, as returned by that function.

                > The script contains two $target variables. Seems like
                > one designates the directory the which contains the uploaded files, while
                > the
                > next designates the directory AND the filename.

                Actually the first instance sets $target to the directory, and the second
                instance appends the filename to that variable's value (note the "." to
                indicate the concatenation). The variable $target now contains just the
                path to the file.

                To add this filename to the $message variable, you would use this -

                $message = 'A new file' . ' - ' . basename( $_FILES['uploaded']['name']) .
                'has been ....

                Finally, if the email is sent, then the redirection MUST work, assuming that
                this is the correct redirection pagename -

                custom-confirmation.php

                --
                Murray --- ICQ 71997575
                Adobe Community Expert
                (If you *MUST* email me, don't LAUGH when you do so!)
                ==================
                http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
                http://www.dwfaq.com - DW FAQs, Tutorials & Resources
                ==================


                "TGC80104" <webforumsuser@macromedia.com> wrote in message
                news:g68v2n$4k8$1@forums.macromedia.com...
                >I tried your suggestions, but no go. I'm sure I did something wrong. The
                > revised script is below. Should the $subject and $message variables
                > include the
                > $target variable as well? The script contains two $target variables. Seems
                > like
                > one designates the directory the which contains the uploaded files, while
                > the
                > next designates the directory AND the filename. Example, from the scripts
                > first
                > two lines:
                >
                > $target = "upload/";
                > $target = $target . basename( $_FILES['uploaded']['name']) ;
                >
                > So, maybe...
                >
                > $subject = 'New File Upload' $target . basename(
                > $_FILES['uploaded']['name']);
                >
                > Or not?
                > Forgive my ignorance, but I'm a complete PHP newbie.
                > Thanks for your help!
                >
                > <?php
                > $target = "upload/";
                > $target = $target . basename( $_FILES['uploaded']['name']) ;
                > $ok=1;
                > $to = 'enter email address';
                > $subject = 'New File Upload'. basename( $_FILES['uploaded']['name']);
                > $message = 'A new file' . basename( $_FILES['uploaded']['name']) 'has been
                > uploaded to the UPLOAD folder on puremash.com';
                > $headers = 'From: enter email address' . "\r\n" .
                > 'Reply-To: enter repl-to address' . "\r\n" .
                > 'X-Mailer: PHP/' . phpversion();
                >
                >
                > if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
                > {
                > echo "The file ". basename( $_FILES['uploaded']['name']). " has been
                > uploaded";
                > mail($to, $subject, $message, $headers);
                > header("Location: custom-confirmation.php");
                > exit();
                > }
                > else {
                > echo "Sorry, there was a problem uploading your file.";
                > }
                > if ($uploaded_type =="text/php")
                > {
                > echo "No PHP files<br>";
                > $ok=0;
                > }
                > if ($uploaded_type =="text/html")
                > {
                > echo "No HTML files<br>";
                > $ok=0;
                > }
                > ?>
                >
                >

                • 5. Re: PHP Script Help?
                  TGC80104 Level 1
                  Okay, the script is now writing the filename into the subject line and message of email, and the emails are going through. THANKS!

                  However, regarding the custom-confirmation.php redirect, I get the following error: Warning: Cannot modify header information - headers already sent (output started at upload.php: 15)

                  After adding
                  header("Location: custom-confirmation.php");
                  exit();

                  Immediately after the mail($to, $subject, $message, $headers); line, as instructed

                  So that part of the code reads:
                  mail($to, $subject, $message, $headers);
                  header("Location: custom-confirmation.php");
                  exit();

                  FYI, Line 15 of the script contains:
                  mail($to, $subject, $message, $headers);



                  • 6. Re: PHP Script Help?
                    TGC80104 Level 1
                    Thanks for all your help!
                    FWIW, I got the redirect to work by switching the order of:

                    mail($to, $subject, $message, $headers);
                    header("Location: custom-confirmation.php");

                    To:
                    header("Location: custom-confirmation.php");
                    mail($to, $subject, $message, $headers);

                    Thanks again!

                    • 7. Re: PHP Script Help?
                      Level 7
                      .oO(TGC80104)

                      >Thanks for all your help!
                      > FWIW, I got the redirect to work by switching the order of:
                      >
                      > mail($to, $subject, $message, $headers);
                      > header("Location: custom-confirmation.php");
                      >
                      > To:
                      > header("Location: custom-confirmation.php");
                      > mail($to, $subject, $message, $headers);

                      This shouldn't cause any different result. But in the first version
                      there are two errors:

                      1) The Location header requires an absolute URI ( http://...)
                      2) You should call exit; after the header() call to stop the script
                      execution at this point.

                      Micha