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 11 of 11
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    PHP File Upload not working

    Ok, so I have a form as follows:
    Code:
    <form action="submit.php" method="post"
    enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file" />
    <br />
    <input type="submit" name="submit" value="Submit" />
    </form>
    The page that 'receives' the file has the following code that handles the uploaded file:
    PHP Code:
    <?php
    if ((($_FILES["file"]["type"] == "image/gif")
    || (
    $_FILES["file"]["type"] == "image/jpeg")
    || (
    $_FILES["file"]["type"] == "image/pjpeg"))
    && (
    $_FILES["file"]["size"] < 200000000))
      {
      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 />";

        if (
    file_exists("uploads/" $_FILES["file"]["name"]))
          {
          echo 
    $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
          
    move_uploaded_file($_FILES["file"]["tmp_name"],
          
    "uploads/" $_FILES["file"]["name"]);
          echo 
    "Stored in: " "uploads/" $_FILES["file"]["name"];
          }
        }
      }
    else
      {
      echo 
    "Invalid file";
      }
    ?>
    So the code says in the browser that it works, but the file itself is not actually showing up on my server. You can try out the code here: northside.mcsdga.net/reveille/staffsubmit.php I've checked the folder name, and it is correct.

  • #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
    You're not checking the return value of your call to move_uploaded_file(). You need to make sure that call worked.

  • #3
    New Coder
    Join Date
    Dec 2010
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fumigator View Post
    You're not checking the return value of your call to move_uploaded_file(). You need to make sure that call worked.
    How would I do that? Sorry, I'm quite new to PHP.

  • #4
    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
    The function returns TRUE or FALSE; you can assign it to a variable and check it:

    PHP Code:
    $moveResult move_uploaded_file();
    if (!
    $moveResult)
    {
        echo 
    "ERROR";

    Or just shortcut and check it directly:

    PHP Code:
    if (!move_uploaded_file())
    {
        echo 
    "ERROR";


  • #5
    New Coder
    Join Date
    Dec 2010
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Ok, so I implemented the code to check my move, and I keep getting the error code showing that it's not successfully moving it. Any idea why? Ask me for specs if you need them.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Chances are its a permission issue.
    Replace the "Error" with a print_r(error_get_last());. One of the offsets will be message, and I'm betting its either going to be a non-existent directory, or a privilege problem (permission denied).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    New Coder
    Join Date
    Dec 2010
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Chances are its a permission issue.
    Replace the "Error" with a print_r(error_get_last());. One of the offsets will be message, and I'm betting its either going to be a non-existent directory, or a privilege problem (permission denied).
    Ok, so I replace the "Error" with the code . . . nothing shows up in the browser. What should I do if it is a permissions problem?

  • #8
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post
    most likely this is a file permissions problem.

    make sure your upload property is set to 0777

    also using your ftp client, chmod your directory to 0777 as well

  • Users who have thanked fommerjackson for this post:

    ditchfieldcaleb (02-23-2011)

  • #9
    New Coder
    Join Date
    Dec 2010
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by fommerjackson View Post
    most likely this is a file permissions problem.

    make sure your upload property is set to 0777

    also using your ftp client, chmod your directory to 0777 as well
    Umm . . . I use Adobe Dreamweaver CS5 to upload my files . . . can you explain to me how to do these things?

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    The absolute easiest thing you can do is use mkdir from PHP. That automatically assigns proper ownership. Otherwise, you may need to login through your webhost and chmod the directory manually using SSH.

    If you are getting a white page, that indicates a fatal error, assuming that there is supposed to be some kind of output. Enable your error reporting by putting this at the top of the page:
    PHP Code:
    ini_set('display_errors'1);
    error_reporting(E_ALL); 
    If the only thing that has changed was the addition of error_get_last, then the error will be a non-existent function. Error_get_last is only post 5.2. To get around that, you would instead track errors with:
    PHP Code:
    ini_set('track_errors'1); // Somewhere usually at the top

    ...
    echo 
    $php_errormsg// After your attempt to move the uploaded file 
    Chances are your error reporting is too low. As soon as you open it up to E_ALL, then you will likely see the error without needing to use error_get_last or $php_errormsg.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

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

    ditchfieldcaleb (02-23-2011)

  • #11
    New Coder
    Join Date
    Dec 2010
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts
    That worked! It is a permissions problem. I'll have my friend help me with the rest. Thanks!


  •  

    Tags for this Thread

    Posting Permissions

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