360haven works best with JavaScript enabled
PHP File Upload
Loading
Register
Results 1 to 4 of 4

Thread: PHP File Upload

  1. #1
    DEADBEEF

    JizzaBeez is offline
    Join Date : Nov 2010
    Posts : 777
    Array

    PHP File Upload

    With PHP, it is possible to upload files to the server.


    Create an Upload-File Form

    To allow users to upload files from a form can be very useful.

    Look at the following HTML form for uploading files:
    Code:
    <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>
    Notice the following about the HTML form above:

    • The enctype attribute of the <form> tag specifies which content-type to use when submitting the form. "multipart/form-data" is used when a form. requires binary data, like the contents of a file, to be uploaded.
    • The type="file" attribute of the <input> tag specifies that the input should be processed as a file. For example, when viewed in a browser, there will be a browse-button next to the input field.

    Note: Allowing users to upload files is a big security risk. Only permit trusted users to perform file uploads.


    Create The Upload Script

    The "upload_file.php" file contains the code for uploading a file:
    Code:
    <?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: " . $_FILES["file"]["tmp_name"];
      }
    ?>
    By using the global PHP $_FILES array you can upload files from a client computer to the remote server.

    The first parameter is the form's input name and the second index can be either "name", "type", "size", "tmp_name" or "error".

    Like this:

    • $_FILES["file"]["name"] - the name of the uploaded file.
    • $_FILES["file"]["type"] - the type of the uploaded file.
    • $_FILES["file"]["size"] - the size in bytes of the uploaded file.
    • $_FILES["file"]["tmp_name"] - the name of the temporary copy of the file stored on the server.
    • $_FILES["file"]["error"] - the error code resulting from the file upload.

    This is a very simple way of uploading files. For security reasons, you should add restrictions on what the user is allowed to upload.


    Restrictions on Upload

    In this script we add some restrictions to the file upload. The user may only upload .gif or .jpeg files and the file size must be under 20 kb:
    Code:
    <?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 "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: " . $_FILES["file"]["tmp_name"];
        }
      }
    else
      {
      echo "Invalid file";
      }
    ?>
    Note: For IE to recognize jpg files the type must be pjpeg, for FireFox it must be jpeg.




    Saving the Uploaded File

    The examples above create a temporary copy of the uploaded files in the PHP temp folder on the server.

    The temporary copied files disappears when the script ends. To store the uploaded file we need to copy it to a different location:
    Code:
    <?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";
      }
    ?>
    The script above checks if the file already exists, if it does not, it copies the file to the specified folder.

    Note: This example saves the file to a new folder called "upload"

  2. The Following 3 Users Say Thank You to JizzaBeez For This Useful Post:


  3. #2
    Senior Member
    ohnoyekoms is offline
    Join Date : Apr 2012
    Posts : 103
    Array

    Re: PHP File Upload

    cant upload files larger then whats within the php.ini to, default is 20M i believe?

  4. #3
    DEADBEEF

    JizzaBeez is offline
    Join Date : Nov 2010
    Posts : 777
    Array

    Re: PHP File Upload

    Quote Originally Posted by ohnoyekoms View Post
    cant upload files larger then whats within the php.ini to, default is 20M i believe?
    You can change the settings in the php.ini file to whatever you want.

    Defaults:

    Maximum size of POST data that PHP will accept:
    post_max_size = 8M

    Maximum allowed size for uploaded files:
    upload_max_filesize = 2M

  5. The Following User Says Thank You to JizzaBeez For This Useful Post:


  6. #4
    Senior Member
    ohnoyekoms is offline
    Join Date : Apr 2012
    Posts : 103
    Array

    Re: PHP File Upload

    lol, i set mine to 200M which i believe is the max for safe use. i did that for my file hosting site. incase someone wanted to share a huge gamesave set
    Downloads : 14 || Uploads : 0 || Rep Power : 2178 || Posts : 103 || Thanks : 20 || Thanked 40 Times in 16 Posts


    Need file hosting? go here


 

 

Similar Threads

  1. More Upload viewing in more places with Xbox.com/Upload and Upload on Xbox 360
    By THUMBS in forum Xbox 360 News, Updates, and Rumors
    Replies: 0
    Last Post: 11-04-2014, 08:47 PM
  2. Replies: 4
    Last Post: 02-07-2012, 09:51 PM
  3. PHP File
    By JizzaBeez in forum Tutorials and Resources
    Replies: 0
    Last Post: 03-03-2011, 11:38 AM

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
  •  
This website uses cookies
We use cookies to store session information to facilitate remembering your login information, to allow you to save website preferences, to personalise content and ads, to provide social media features and to analyse our traffic. We also share information about your use of our site with our social media, advertising and analytics partners.
     

About 360haven

    360haven is an Forum Devoted To Game modding Fans from all over the world.

    An Awesome Community of Xbox 360 Gamers, Modders and Developers who Create & Share Tutorials, Applications, Gfx, Trainers and Gamesaves.

    A haven for the l33t.
    A scarce paradise for modders.

Like this page

Google+