www.webdeveloper.com
Results 1 to 9 of 9

Thread: Upload form

  1. #1
    Join Date
    May 2003
    Posts
    14

    Upload form

    I have a site and i want users to have the option to upload files to my server. You know, like a text field, a Browse button and a Submit button. Now I know this must be done in the server side, but HOW exactly do i do this ??

  2. #2
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    Try something like this:

    PHP Code:
    <html>
    <head>
    <title>Upload a File</title>
    </head>
    <body>



    <?PHP

    $uploaddir 
    "uploads"# must be chmoded to 777

    if ($_POST['submit']) 
    {
        
    $name $_FILES['File']['name'];
        
    $tmpname $_FILES['File']['tmp_name'];
        
    $size $_FILES['File']['size'];

        if (
    copy ($tmpname"$uploaddir/$name"))
        {
            print (
    "File Name: $name<br/><br/>\n");
            print (
    "File Size: $size<br/><br/>\n");
            print (
    "Your file was successfully uploaded!<br/><br/>\n");
        }
        else
        {
            print (
    "Your file could not be uploaded.");
        }
        
    unlink ($tmpname);
    }

    ?>

    Upload a file to the server

    <form action="upload.php" method="post" enctype="multipart/form-data">
    File: <input type="file" name="File" size="20"><br/>
    <input type="submit" name="submit" value="Upload File"></form>

    </body>
    </html>

    Personal website http://www.ryanbrill.com/
    Business website: http://www.infinitywebdesign.com/
    TypeSpace http://www.typespace.org/

    I reject your reality and substitute it with my own!

  3. #3
    Join Date
    Dec 2002
    Location
    WI, United States
    Posts
    1,372
    In order for that code to work, make sure you name the file upload.php
    if (succeed != true)
    function askAdam();

    Check out my site!

  4. #4
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    Yes, and you'll also have to be running PHP 4.1.0 (due to the autoglobal $_FILES), so you might as well write it like this. (named upload.php, again... )

    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Upload a File</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>

    <?PHP

    $uploaddir 
    "uploads/"# must be chmoded to 777

    if (isset($_POST['submit'])) {
        
    $name $_FILES['File']['name'];
        
    $tmpname $_FILES['File']['tmp_name'];
        
    $size $_FILES['File']['size'];

        if (
    move_uploaded_file($tmpname$uploaddir $name)) {
            print (
    "<p>File Name: $name</p>\n");
            print (
    "<p>File Size: $size</p>\n");
            print (
    "<p>Your file was successfully uploaded!</p>\n");
        }
        else {
            print (
    "<p>Your file could not be uploaded.</p>");
        }
    }

    ?>

    <p>Upload a file to the server</p>

    <form action="upload.php" method="post" enctype="multipart/form-data">
    <p>File: <input type="file" name="File" size="20"><br/>
    <input type="submit" name="submit" value="Upload File"></p></form>

    </body>
    </html>
    This can be re-written to work in eariler versions, if necessary...

    Personal website http://www.ryanbrill.com/
    Business website: http://www.infinitywebdesign.com/
    TypeSpace http://www.typespace.org/

    I reject your reality and substitute it with my own!

  5. #5
    Join Date
    Apr 2007
    Posts
    24

    Lightbulb

    But can you make a code that allow only *.jpg files,
    and files that are less than 9000kb?

  6. #6
    Join Date
    Mar 2007
    Location
    Nagpur (India)
    Posts
    107

    Thumbs up

    Code for Image.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Upload a File</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>

    PHP Code:
    <?PHP

    $uploaddir 
    "uploads/"# must be chmoded to 777

    if (isset($_POST['submit'])) {
        
    $name $_FILES['File']['name'];
        
    $tmpname $_FILES['File']['tmp_name'];
        
    $size $_FILES['File']['size'];

        
    // If the user uploads not a valid image, then display an error msg.
        
    $imgSize = @getImageSize($tmpname);
        if (empty(
    $imgSize)) {
          print (
    "<p>Please upload a valid image.</p>\n");
        } else {
          
    /*** Get the mime type of the image and check, manager entered the valid image or not. ***/
          
    $imageType explode("/",$imgSize['mime']);
          
    $imageType trim(strtolower($imageType[1]));
          
    $validImageTypes = array("jpg""jpeg""png""gif"); // you can also use the same code for checking other type of files like, PDF.
          
    if (!in_array($imageType$validImageTypes)) {
            print(
    "<p>Please upload a valid image for cover.</p>\n");
          } else {
            if (
    move_uploaded_file($tmpname$uploaddir $name)) {
              print (
    "<p>File Name: $name</p>\n");
              print (
    "<p>File Size: $size</p>\n");
              print (
    "<p>Your file was successfully uploaded!</p>\n");
            } else {
              print (
    "<p>Your file could not be uploaded.</p>");
            }
          }
        }
    }

    ?>
    <p>Upload a file to the server</p>

    <form action="upload.php" method="post" enctype="multipart/form-data">
    <p>File: <input type="file" name="File" size="20"><br/>
    <input type="submit" name="submit" value="Upload File"></p></form>

    </body>
    </html>
    Suhas Dhoke
    --------------------
    Zeon Solutions Pvt. Ltd. Nagpur.

  7. #7
    Join Date
    May 2008
    Posts
    2

    Upload form

    These forms above works perfectly, I dont know anything about php but I was looking for something like this for some time. The only problem is that this form only works well whit files that has less then 3 MB and I needed one that can upload until 9 GB. Can you help me whit that?
    I tank you in advance for the time dispended in reading this message, and hoppe to ear about you guys soon.

    (obs): I'm Portuguese so I'm sorry about my rusty Inglish writing.

  8. #8
    Join Date
    Feb 2005
    Location
    Tauranga
    Posts
    2,062
    9 gig ? you will have to change options in your PHP.INI to allow for files that big.

  9. #9
    Join Date
    May 2008
    Posts
    2

    php.ini

    Quote Originally Posted by Sheldon
    9 gig ? you will have to change options in your PHP.INI to allow for files that big.
    Like I said previosly, I dont know how to program PHP, I just copy the form above and created an upload directory and it work, but only with files that has less than 2MB. Can you please help me with the ini.php?
    Tank you, and sorry for being anoyng.

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
  •  
HTML5 Development Center



Recent Articles