Results 1 to 13 of 13

Thread: How to make your first PHP file uploader

  1. #1
    Join Date
    Aug 2007
    Location
    in a random little world
    Posts
    5,778
    Mentioned
    0 Post(s)
    Quoted
    7 Post(s)

    Default How to make your first PHP file uploader

    In this tutorial i will show you how to create you very first File Uploader in PHP

    now this is a very simple one but at least it will get some people started

    so first you will want something to run the files on, if you have php and apache installed then your fine otherwise you will need to find a host which has php and apache installed

    now your going to create 2 files
    the first one will be to select the file and click upload and the other will tell you where the file is and give you a link to where it is

    so in your first file put this code and call it index.html
    PHP Code:
    <html>
    <
    body>

      <
    form action="upload.php" method="post" enctype="multipart/form-data">
        <
    label for="file">Filename:</label>
        <
    input type="file" name="file" id="file" />
        <
    input type="submit" name="submit" value="Submit" />
      </
    form>

    </
    body>
    </
    html
    now i will explain what each line does one by one
    the first two
    PHP Code:
    <html>
    <
    body
    just declare that it is a html file and that it is the start of the body of the page
    the next line
    PHP Code:
    <form action="upload.php" method="post" enctype="multipart/form-data"
    tells the browser that you are starting a form that should take you to a file called upload.php when you click the submit button
    after that we have
    PHP Code:
    <label for="file">Filename:</label
    now this is just prints out a bit of text nothing else
    then we have
    PHP Code:
    <input type="file" name="file" id="file" /> 
    now this tells the browser to make a box with a button beside it saying browse so you can choose a file
    is also calls it file
    then after that we have
    PHP Code:
    <input type="submit" name="submit" value="Submit" /> 
    and that is the submit button and tells it to take us to upload.php when it is clicked
    then the last few lines are
    PHP Code:
    </form>

    </
    body>
    </
    html
    which tells the browser that we are at the end of the form and the body and the html file

    now with that done it should look like this
    http://oneunderuk.com/upload/

    now on to the next file
    make a file called upload.php in in there put
    PHP Code:
    <html>
    <body>
      <?php
        
    if ($_FILES["file"]["error"] > 0)
        {
          echo 
    "Error: " $_FILES["file"]["error"] . "<br />";
        }
        else
        {
          echo 
    "Uploaded : " $_FILES["file"]["name"] . "<br />";
          
    move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" $_FILES["file"]["name"]);
          echo 
    "You can download it at<br/>" "http://oneunderuk.com/upload/upload/" $_FILES["file"]["name"]."<br/>";
        }
      
    ?> 
    </body>
    </html>
    once again you have the two tags at the top telling the browser that your inside the body
    now the next line is new and something we havent come across before
    it is
    PHP Code:
    <?php
    that baisicaly says that everything in there is php code until you get to
    PHP Code:
    ?> 
    the next line
    PHP Code:
      if ($_FILES["file"]["error"] > 0
    checks whether there was a problen uploading the file
    if there was then it will write a messge on the screen saying Error: then the error
    PHP Code:
        echo "Error: " $_FILES["file"]["error"] . "<br />"
    and if there wasnt a error then it will call whatever is in the block after it
    PHP Code:
    $_FILES["file"]["name"
    is what the file that you uploaded is called
    so the line that says
    PHP Code:
        echo "Uploaded : " $_FILES["file"]["name"] . "<br />"
    prints out on the screen Uploaded : then the file name

    now when you upload the file it makes a temporary file of it which then gets deleted once the script has finished
    and that is what the next line is for
    you want to move it before the script is finished so it can be downoaded at a later date
    so
    PHP Code:
    move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" $_FILES["file"]["name"]); 
    moved the file from the temporary folder to a folder called upload so it wont get deleted when the script finishes *phew*
    but now you want to download the file dont you?
    and thats what the next line is for
    PHP Code:
    echo "You can download it at<br/>" "http://oneunderuk.com/upload/upload/" $_FILES["file"]["name"]."<br/>"
    that prints out on the screen where the file is so you can copy that link and paste it in the the bar at the top and you can download it
    then you have the
    PHP Code:
    ?> 
    which says that your at the end of the php script
    then there is the
    PHP Code:
    </body>
    </
    html
    which says that your at the end of the file

    now $_FILES["file"]["name"] tells you the name of the file
    but there are other stuff that you can use as well
    PHP Code:
    $_FILES["file"]["name"//returns the file name
    $_FILES["file"]["type"//returns the file type
    $_FILES["file"]["error"//returns if there was a error while uploading the file
    $_FILES["file"]["tmp_name"//returns where the temp file is located
    $_FILES["file"]["size"//returns the files size in bytes 
    now if someone uploads a file with the same name then it will overwrite the old one oh noes...
    but using the name of the file means that we can see if it has already been taken
    so if you replace upload.php with
    PHP Code:
    <html>
    <body>
      <?php
        
    if ($_FILES["file"]["error"] > 0)
        {
          echo 
    "Error: " $_FILES["file"]["error"] . "<br />";
        }
        else
        {
          if (
    file_exists("upload/" $_FILES["file"]["name"]))
          {
            echo 
    $_FILES["file"]["name"] . " already exists. Please rename it and try again";
          }
          else
          {
            echo 
    "Uploaded : " $_FILES["file"]["name"] . "<br />";
            
    move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" $_FILES["file"]["name"]);
            echo 
    "You can download it at<br/>" "http://oneunderuk.com/upload/upload/" $_FILES["file"]["name"]."<br/>";
          }
        }
      
    ?> 
    </body>
    </html>
    i added a check to see if the file already exists
    if it does then it wont overwrite the old one but it will tell you that you need to rename it and try again
    if it doesnt exist then it will move it so it can be downloaded
    that way it will stop any files from eing over written

    also you can use this methood of setting a upload limit or a certain upload type
    this will stop anyone from uploading anything above 200 meg
    PHP Code:
    <html>
    <body>
      <?php
        
    if ($_FILES["file"]["size"] < 200*1024*1024)
        {
          if (
    $_FILES["file"]["error"] > 0)
          {
            echo 
    "Error: " $_FILES["file"]["error"] . "<br />";
          }
          else
          {
            if (
    file_exists("upload/" $_FILES["file"]["name"]))
            {
              echo 
    $_FILES["file"]["name"] . " already exists. Please rename it and try again";
            }
            else
            {
              echo 
    "Uploaded : " $_FILES["file"]["name"] . "<br />";
              
    move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" $_FILES["file"]["name"]);
              echo 
    "You can download it at<br/>" "http://oneunderuk.com/upload/upload/" $_FILES["file"]["name"]."<br/>";
            }
          }
        }
        else
        {
          echo 
    "The file is too large";
        }
      
    ?> 
    </body>
    </html>
    you can also use the same tecnique for checking the file type
    PHP Code:
    <html>
    <body>
      <?php
        
    if ($_FILES["file"]["type"] = "application/octet-stream")
        {
          if (
    $_FILES["file"]["error"] > 0)
          {
            echo 
    "Error: " $_FILES["file"]["error"] . "<br />";
          }
          else
          {
            if (
    file_exists("upload/" $_FILES["file"]["name"]))
            {
              echo 
    $_FILES["file"]["name"] . " already exists. Please rename it and try again";
            }
            else
            {
              echo 
    "Uploaded : " $_FILES["file"]["name"] . "<br />";
              
    move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" $_FILES["file"]["name"]);
              echo 
    "You can download it at<br/>" "http://oneunderuk.com/upload/upload/" $_FILES["file"]["name"]."<br/>";
            }
          }
        }
        else
        {
          echo 
    "Invalid file type";
        }
      
    ?> 
    </body>
    </html>
    that will stop anyone from uploading any bat files


    well i hope this has helped anyone
    it may be a bit hard to understand but i didnt plan anyof this out and i just wrote it from my head as i went along

    any questions or comments dont hesitate to post

    for a working copy go to http://oneunderuk.com/upload

    ~shut
    Last edited by Shuttleu; 07-04-2009 at 03:38 AM.

  2. #2
    Join Date
    Mar 2007
    Posts
    3,116
    Mentioned
    0 Post(s)
    Quoted
    2 Post(s)

    Default

    *brags*
    http://www.pspsquare.com/upload.php

    ~MylesMadness

  3. #3
    Join Date
    Aug 2007
    Location
    in a random little world
    Posts
    5,778
    Mentioned
    0 Post(s)
    Quoted
    7 Post(s)

  4. #4
    Join Date
    Mar 2007
    Posts
    3,116
    Mentioned
    0 Post(s)
    Quoted
    2 Post(s)

    Default

    Well, maybe this is useful, rep++

    ~Myles

  5. #5
    Join Date
    Aug 2007
    Location
    in a random little world
    Posts
    5,778
    Mentioned
    0 Post(s)
    Quoted
    7 Post(s)

  6. #6
    Join Date
    Dec 2008
    Location
    In a galaxy far, far away...
    Posts
    584
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Where was this tut when I needed it 2 years ago.. lol

    Anyway Nice work You've done alot of explaining in your tut, you deserve rep++



    ~NS

  7. #7
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Nadeem surely doesn't need it now...he's php master .

  8. #8
    Join Date
    Feb 2007
    Location
    Access Violation at 0x00000000
    Posts
    2,865
    Mentioned
    3 Post(s)
    Quoted
    18 Post(s)

    Default

    Good tut for starters
    PHP also has standards... Your code burns my eyes
    PHP Code:
    <html>
    <body>
        <?php
            
    if ($_FILES["file"]["type"] = "application/octet-stream")
            {
                if (
    $_FILES["file"]["error"] > 0)
                {
                    echo 
    "Error: " $_FILES["file"]["error"] . "<br />";
                }
                else
                {
                    if (
    file_exists("upload/" $_FILES["file"]["name"]))
                    {
                        echo 
    $_FILES["file"]["name"] . " already exists. Please rename it and try again";
                    }
                }    
                else
                {
                    echo 
    "Uploaded : " $_FILES["file"]["name"] . "<br />";
                    
    move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" $_FILES["file"]["name"]);
                    echo 
    "You can download it at<br/>" "http://oneunderuk.com/upload/upload/" $_FILES["file"]["name"]."<br/>";
                }
            }
            else
            {
                echo 
    "Invalid file type";
            }
        
    ?> 
    </body>
    </html>
    Ce ne sont que des gueux


  9. #9
    Join Date
    Aug 2007
    Location
    in a random little world
    Posts
    5,778
    Mentioned
    0 Post(s)
    Quoted
    7 Post(s)

  10. #10
    Join Date
    Feb 2007
    Location
    Access Violation at 0x00000000
    Posts
    2,865
    Mentioned
    3 Post(s)
    Quoted
    18 Post(s)

    Default

    Added in my posts of sort of notepadded ish work im soo tired atm lol.
    Ce ne sont que des gueux


  11. #11
    Join Date
    May 2007
    Location
    Sydney, Australia (Faggot Region)
    Posts
    1,465
    Mentioned
    0 Post(s)
    Quoted
    11 Post(s)

    Default

    I paided ss23 to make me a uploader + adding them to a table and make it look nice ill upload it here in 48hrs


  12. #12
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    How much did you pay him O_o?

  13. #13
    Join Date
    May 2007
    Location
    Sydney, Australia (Faggot Region)
    Posts
    1,465
    Mentioned
    0 Post(s)
    Quoted
    11 Post(s)

    Default

    $10

    Everything is there



Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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