Web Analytics Made Easy -
StatCounter Server Side Upload script fails on Apache 2.2 with PHP5, Win32 - XP Pro - CodingForum

Announcement

Collapse
No announcement yet.

Server Side Upload script fails on Apache 2.2 with PHP5, Win32 - XP Pro

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Server Side Upload script fails on Apache 2.2 with PHP5, Win32 - XP Pro

    I'm running Apache 2.2 on WinXP, 32 bit, and I have installed PHP5. I need to enable file uploads to the server, and have the following "submit" HTML:

    Code:
    <form method="POST" action="uploader_v3.php" enctype="multipart/form-data">
    Choose a file to upload:
    <input type="file" name="uploaded_file">
    <input type="submit" value="Upload File">
    </form>
    The problem is that once the "Submit" button is clicked, the uploader script returns a blank page. I'm not sure if this is a PHP issue or an Apache issue because Apache's .CONF is good to go and PHP.INI has file uploads enabled, and syntactically, there's nothing wrong with the uploader_v3.php script. For those fluent in PHP, here's what "uploader_v3.php" looks like:

    Code:
    <?PHP
    
    $SafeFile = $HTTP_POST_FILES['uploaded_file']['name'];
    
    $uploaddir = "/uploads/";
    $path = $uploaddir.$SafeFile;
    $upload_err = $HTTP_POST_FILES['uploaded_file']['error']
    
    if($uploaded_file != none){ //AS LONG AS A FILE WAS SELECTED...
    
        if(copy($HTTP_POST_FILES['uploaded_file']['tmp_name'], $path)){ //IF IT HAS BEEN COPIED...
    
            //GET FILE NAME
            $theFileName = $HTTP_POST_FILES['uploaded_file']['name'];
    
            //GET FILE SIZE
            $theFileSize = $HTTP_POST_FILES['uploaded_file']['size'];
    
            if ($theFileSize>999999){ //IF GREATER THAN 999KB, DISPLAY AS MB
                $theDiv = $theFileSize / 1000000;
                $theFileSize = round($theDiv, 1)." MB"; //round($WhatToRound, $DecimalPlaces)
            } else { //OTHERWISE DISPLAY AS KB
                $theDiv = $theFileSize / 1000;
                $theFileSize = round($theDiv, 1)." KB"; //round($WhatToRound, $DecimalPlaces)
            }
    
    echo <<<UPLS
    <table cellpadding="5" width="300">
    <tr>
        <td align="Center" colspan="2"><font color="#C80000"><b>Upload Successful</b></font></td>
    </tr>
    <tr>
        <td align="right"><b>File Name: </b></td>
        <td align="left">$theFileName</td>
    </tr>
    <tr>
        <td align="right"><b>File Size: </b></td>
        <td align="left">$theFileSize</td>
    </tr>
    <tr>
        <td align="right"><b>Directory: </b></td>
        <td align="left">$uploaddir</td>
    </tr>
    </table>
    
    UPLS;
    
        } else {
    
    //PRINT AN ERROR IF THE FILE COULD NOT BE COPIED
    echo <<<UPLF
    <table cellpadding="5" width="80%">
    <tr>
    <td align="Center" colspan="2"><font color="#00C800"><b>File "$SafeFile" could not be uploaded:<br>Return Code: $upload_err <br /></b></font></td>
    </tr>
    
    </table>
    
    UPLF;
        }
    }
    ?>
    Can somebody help me? This is EXTREMELY annoying! I've posted this in both Apache and PHP because I'm not sure where the problem is.

  • #2
    If you had error reporting on, you would have spotted this straight away.

    Your 4th line of php code is missing a ; at the end
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

    Comment


    • #3
      Wow... I have error reporting on, but it didn't flag anything! All this trouble for a stupid little piece of punctuation!

      Thanks a ton! I believe this calls for a +1 for you, my friend!
      Last edited by EvilSupahFly; Aug 19, 2011, 12:26 AM. Reason: Notation of Star Clickage for BluePanther

      Comment


      • #4
        Haha, it's annoying. To think one little character can ruin everything .

        Just a little tip though, PHP logs every error into a log file so if you ever encounter a problem, check there too :P.
        Useful function to retrieve difference in times
        The best PHP resource
        A good PHP FAQ
        PLEASE remember to wrap your code in [PHP] tags.
        PHP Code:
        // Replace this
        if(isset($_POST['submitButton']))
        // With this
        if(!empty($_POST))
        // Then check for values/forms. Some IE versions don't send the submit button 
        Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

        Comment


        • #5
          You're error reporting cannot be on. This is a parse error, its guaranteed to be fatal.
          Also, dump whatever article you're using. This is old school code that hasn't been used in about 10 years. $HTTP_POST_FILES is long gone (currently still usable, but can be disabled via register_long_arrays directive); use $_FILES instead. Aside from this, the first branch condition compares it to an undefined constant 'none'. That should be either treated as a string or have a constant declared for it.

          Edit:
          I should clarify the ER as well. Make sure error reporting is set to something other than 0. See here for the levels available: http://php.ca/manual/en/errorfunc.constants.php
          PHP Code:
          header('HTTP/1.1 420 Enhance Your Calm'); 
          Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

          Comment


          • #6
            Originally posted by Fou-Lu View Post
            You're error reporting cannot be on. This is a parse error, its guaranteed to be fatal.
            Also, dump whatever article you're using. This is old school code that hasn't been used in about 10 years. $HTTP_POST_FILES is long gone (currently still usable, but can be disabled via register_long_arrays directive); use $_FILES instead. Aside from this, the first branch condition compares it to an undefined constant 'none'. That should be either treated as a string or have a constant declared for it.

            Edit:
            I should clarify the ER as well. Make sure error reporting is set to something other than 0. See here for the levels available: http://php.ca/manual/en/errorfunc.constants.php
            Oh wow, I never actually noticed the code - only the missing ;. I managed to spot that, without reading the code...I think my brain is broken.
            Useful function to retrieve difference in times
            The best PHP resource
            A good PHP FAQ
            PLEASE remember to wrap your code in [PHP] tags.
            PHP Code:
            // Replace this
            if(isset($_POST['submitButton']))
            // With this
            if(!empty($_POST))
            // Then check for values/forms. Some IE versions don't send the submit button 
            Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

            Comment


            • #7
              Now I have a new problem. I upgraded to Windows 7 (was running on XP previously) and when I try and upload, it says "NONE" is not defined in the line
              if($uploaded_file != none){ //AS LONG AS A FILE WAS SELECTED... but returns error 0 which isn't helpful. In consulting the server logs, it's looking for temp.html but there's no other info provided in the log.

              I dropped the archaic coding at Fou-Lu's recommendation and added the semi-colon. Other than that, no changes, so the script is still the same as I posted above (but with the corrections implemented). I'm still using the same version of Apache and PHP with the same config as on XP.

              What gets me is that this worked fine after I fixed it until last week when I changed platforms. Ideas or thoughts? The W7 User Migration tool isn't supposed to change anything as it basically just zips it all up and unzips it later, so I'm really at a loss here as to why it's suddenly broken.

              Comment


              • #8
                Originally posted by EvilSupahFly View Post
                Now I have a new problem. I upgraded to Windows 7 (was running on XP previously) and when I try and upload, it says "NONE" is not defined in the line
                if($uploaded_file != none){ //AS LONG AS A FILE WAS SELECTED... but returns error 0 which isn't helpful. In consulting the server logs, it's looking for temp.html but there's no other info provided in the log.
                I mentioned this; your none is a constant, not a string. PHP by default though will resolve a constant as a string if it cannot find a constant under that name. Use if (strcasecmp($uploaded_file, 'none') <> 0) as your comparison instead.

                As for changes; these have a tremendous impact on working code. I'm guessing if you moved environments, you are probably using a clean install. Even the same version may have a different configuration ini versus the original. The PHP.ini's come in three brands in PHP: development, recommended, and default (or along those lines). Development is more open for error reporting, so use it for writing with. Recommended has a lot more things tightened down, so use it for testing. Default is the default configurations provided by ini, and will be fairly common over servers. Make sure it works on all three versions, and if you use something in specific like fopen on a remote script, you should first check if allow_url_fopen is enabled for example.

                Edit:
                Sorry, what am I thinking. You can't check for a file type using none :P
                Check the error instead: if ($_FILES['uploaded_file']['error'] != UPLOAD_ERR_OK). Anything else indicates a problem with the file including no file provided. You can manufacture a custom error output by using these constants for the error codes: http://php.ca/manual/en/features.file-upload.errors.php
                Last edited by Fou-Lu; Dec 22, 2011, 02:49 PM.
                PHP Code:
                header('HTTP/1.1 420 Enhance Your Calm'); 
                Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

                Comment


                • #9
                  I understand about declaring "none" but I discovered that my problem was with the "upload" path.

                  The Apache server and PHP engine I'm using are the same as what I had on XP. Literally the same install. Just zipped on XP and unzipped on 7.

                  I had to change $uploaddir = "/uploads/"; to read $uploaddir = "Z:\\HTTPROOT\\uploads\\"; instead and now it works fine again, still leaving "none" as undeclared.

                  I have however, since fixing the script, upgraded PHP and Apache and encountered no further issues, though with the new versions, performance on Windows 7 has improved nicely (about 35%).
                  Last edited by EvilSupahFly; Dec 25, 2011, 09:32 PM. Reason: Wrong slashes.

                  Comment


                  • #10
                    In looking forward, I have opted to take your advice and change if($uploaded_file != none) to your recommended if ($_FILES['uploaded_file']['error'] != UPLOAD_ERR_OK) in order to avoid problems down the road should the default behaviour of PHP change and cause my script to break again.

                    Thanks, Fou-Lu!

                    Comment

                    Working...
                    X
                    😀
                    🥰
                    🤢
                    😎
                    😡
                    👍
                    👎