VERY simple image uploader

Discussion in 'PHP' started by le007, Nov 14, 2011.

  1. #1
    Hi all,

    Just need a very basic jpg uploader - put this together and it is giving no errors but I can't find the picture in the folder? I can't find it anywhere on my file manager?

    <html>
    <body>
    
    <form action="upload_file.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>
    
    </body>
    </html>
    PHP:
    upload_file.php
    <html>
    <body>
    
    <?php
    if ($_FILES["file"]["error"] > 0)
      {
      echo "Error: " . $_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 "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    
    ?>
    
    
    </html>
    PHP:
    I get:
    Any ideas?
    Thank you.
     
    Solved! View solution.
    Last edited: Nov 14, 2011
    le007, Nov 14, 2011 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    sarahk, Nov 14, 2011 IP
  3. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #3
    Hi Sarahk,

    thanks for your reply. I tried the below code and I got permission denied? I created a folder called "upload" but no pix is in it :/

    <?php
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
    && ($_FILES["file"]["size"] < 20000))
      {
      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("upload/" . $_FILES["file"]["name"]))
          {
          echo $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "upload/" . $_FILES["file"]["name"]);
          echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
          }
        }
      }
    else
      {
      echo "Invalid file";
      }
    ?>
    PHP:
     
    le007, Nov 14, 2011 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #4
    "upload/" probably isn't the full path to the directory
    have you set the directory permissions to 777
     
    sarahk, Nov 14, 2011 IP
  5. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #5
    Excellent, sarahk, I put the folder 777 and it works now! Excellent!
    Thanks very much.

    If I wanted to alter the above text to just overwrite the file everytime - could you edit it for that please? :D
     
    le007, Nov 14, 2011 IP
  6. #6
    move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
    PHP:
    becomes
    move_uploaded_file($_FILES["file"]["tmp_name"], "upload/permanent_name.txt");
    PHP:
    replace permanent_name.txt with whatever the file name should be.
     
    sarahk, Nov 14, 2011 IP
  7. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #7
    Excellent, thanks a million :D
     
    le007, Nov 14, 2011 IP
  8. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #8
    Hi Sarahk,

    I've tried the code in a page on my site and it won't work - any suggestions please?

    Link is here:
    http://standoutmodels.com/upload.php
     
    le007, Dec 5, 2011 IP
  9. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #9
    Just focussing on the php section

    <?php
    if (isset($_FILES)) {
        $valid_types = array("image/gif", "image/jpeg", "image/pjpeg");
        if (in_array($_FILES["file"]["type"], $valid_types) && ($_FILES["file"]["size"] < 20000)) {
            if ($_FILES["file"]["error"] > 0) {
                echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
            }
            else {
                echo "Upload: " . $_FILES["file"]["name"] . "<br />
                    Type: " . $_FILES["file"]["type"] . "<br />
                    Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />
                    Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
    
                    $new_file = 'upload/' . $_FILES['file']['name'];
                if (file_exists($new_file))            {
                    echo $new_file . ' already exists. ';
                }
                else {
                    move_uploaded_file($_FILES["file"]["tmp_name"], $new_file);
                    echo "Stored in: " . $new_file;
                }
            }
        }
        else {
            echo "Invalid file";
        }
    }
    else echo "No file selected for upload";
    ?>
    PHP:
    but all I've really done is improve the read-ability and put a check to see if a file had been uploaded in the first place. I have no idea where your upload form is and there are no obvious reasons to be getting errors.

    What isn't actually working for you?
     
    sarahk, Dec 5, 2011 IP
  10. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #10
    le007, Dec 5, 2011 IP
  11. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #11
    Where is your form?

    The link itself takes me to the page but unless I can select an image on my computer and submit that form I can't really test.
     
    sarahk, Dec 6, 2011 IP
  12. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #12
    Just the same as my very first post. I tried a different file size and it worked there.
    http://www.standoutmodels.com/upload_file.php

    It won't overwrite the original file but I didn't implement those changes. Thanks very much Sarahk
     
    le007, Dec 6, 2011 IP