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 8 of 8
  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Posts
    317
    Thanks
    24
    Thanked 3 Times in 3 Posts

    Upload file Script Doesn't Work

    Hi guys, I was hoping you could tell me why this upload file script doesn't work.

    Code:
    <form enctype="multipart/form-data" action="upload-photo.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
    Select the photo to upload<br /><br />
    <input name="uploadedfile" type="file" /><br /><br />
    <input type="submit" value="Upload File" />
    </form>
    Code:
    <?php
    $target_path = "members/photos/";
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
    
    if ((($_FILES["file"]["type"] == "image/jpg")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
    && ($_FILES["file"]["size"] < 100000))
      {
      if ($_FILES["file"]["error"] > 0){
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        } else {
        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
    	move_uploaded_file($_FILES["file"]["tmp_name"],
          "members/photos/" . $_FILES["file"]["name"]);
          echo "Stored in: " . "members/photos/" . $_FILES["file"]["name"];
        }
    } else {
    echo "Invalid file";
    }
    ?>
    also if i wanted to change the file name how would i do that before the file gets saved?

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Look into move_uploaded_file() and the tutorial on handling file uploads for info on how to get your script working.

    I'm not going to bother attempting to debug your code that I a) can't run myself, and b) wasn't given any error information about.

  • #3
    Regular Coder
    Join Date
    Apr 2007
    Posts
    317
    Thanks
    24
    Thanked 3 Times in 3 Posts
    well the problem is that it always goes to the false statement and reads invalid file eventhough the file being uploaded meets all statement checks.

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by macleodjb View Post
    well the problem is that it always goes to the false statement and reads invalid file eventhough the file being uploaded meets all statement checks.
    use:

    PHP Code:
      if ($_FILES["file"]["error"] !== UPLOAD_ERR_OK){ 
    instead of:

    PHP Code:
      if ($_FILES["file"]["error"] > 0){ 
    PS: $HTTP_POST_FILES is a replacement for $_FILES

    Edit:
    wrong PS.
    correct is $_FILES is a replacement for $HTTP_POST_FILES


    best regards
    Last edited by oesxyl; 02-23-2008 at 09:00 PM.

  • #5
    Regular Coder
    Join Date
    Apr 2007
    Posts
    317
    Thanks
    24
    Thanked 3 Times in 3 Posts
    I used this directly from php.net, and it still doesn't work. I tried a few different files and none of them work. Could this be a problem with my server?

    Code:
    $uploaddir = '/members/photos';
    $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
    
    echo '<pre>';
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        echo "File is valid, and was successfully uploaded.\n";
    } else {
        echo "Possible file upload attack!\n";
    }
    
    echo 'Here is some more debugging info:';
    print_r($_FILES);
    
    print "</pre>";
    What happens is it evaluates saying that this is a possible file upload attack and then below it, it gives all the information for the file i clicked on. All the information is present it just doesn't seem to want to work.

  • #6
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,010
    Thanks
    2
    Thanked 312 Times in 304 Posts
    I wonder where people keep finding that upload code (in the first post). The ['error'] element is only checked after the ['type'] and ['size'] are checked, but an upload error will prevent the the type and size from having any value, so no true error information will ever be reported. Code must check for errors first before accessing any of the data values.

    $HTTP_POST_FILES is depreciated as of php4.1 (was replaced by $_FILES), turned off by default in php5, and eliminated in php6.

    The main reason why your code is not working is because the name="..." of your form field does not match the name you are using in your form processing code.
    Last edited by CFMaBiSmAd; 02-23-2008 at 08:29 PM. Reason: fixed wording
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • Users who have thanked CFMaBiSmAd for this post:

    oesxyl (02-23-2008)

  • #7
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    thank you for correction, CFMaBiSmAd

    wrong understanding of:

    Use $HTTP_POST_FILES instead if using an earlier version
    As CFMaBiSmAd said, you have 'uploadedfile' in form
    Code:
    <form enctype="multipart/form-data" action="upload-photo.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
    Select the photo to upload<br /><br />
    <input name="uploadedfile" type="file" /><br /><br />
    <input type="submit" value="Upload File" />
    </form>
    instead of 'file' must be 'uploadedfile':

    PHP Code:
    if ((($_FILES["uploadedfile"]["type"] == "image/jpg")
    || (
    $_FILES["file"]["type"] == "image/jpeg")
    || (
    $_FILES["file"]["type"] == "image/pjpeg"))
    && (
    $_FILES["file"]["size"] < 100000)) 
    best regards

  • Users who have thanked oesxyl for this post:

    macleodjb (02-23-2008)

  • #8
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Quote Originally Posted by macleodjb View Post
    PHP Code:
    $uploaddir '/members/photos'
    That path is not likely correct because when you preceed the path with a forwardslash like that it starts at the directory root, and you don't have a trailing slash. So, you more than likely want to use:
    PHP Code:
    $uploaddir './members/photos/'

  • Users who have thanked Inigoesdr for this post:

    macleodjb (02-23-2008)


  •  

    Posting Permissions

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