Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Angry 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
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    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

  • Users who have thanked BluePanther for this post:

    EvilSupahFly (08-19-2011)

  • #3
    New Coder
    Join Date
    Aug 2011
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Smile

    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; 08-19-2011 at 05:26 AM. Reason: Notation of Star Clickage for BluePanther

  • #4
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    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.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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

  • #6
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote 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.

  • #7
    New Coder
    Join Date
    Aug 2011
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote 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; 12-22-2011 at 07:49 PM.

  • Users who have thanked Fou-Lu for this post:

    EvilSupahFly (12-24-2011)

  • #9
    New Coder
    Join Date
    Aug 2011
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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; 12-26-2011 at 02:32 AM. Reason: Wrong slashes.

  • #10
    New Coder
    Join Date
    Aug 2011
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •