Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 3 of 3
  1. #1
    Senior Member
    Join Date
    Apr 2002
    Location
    Cocooned in her thoughts
    Posts
    312
    Member #
    67
    Liked
    3 times
    PHP Uploads...


    General things you need to know:
    * On the HTML form, where the user specifies the file to upload, you will put up a "File" control

    that shows up as a textbox with a Browse button.
    * A form with an option to upload files, needs to have an encoding of multipart/form-data (see

    form declaration below)
    * When uploaded, files get copied to the server's temp directory. It is your problem to copy the

    file from there, within your php script, to whereever you want, and then delete the one in the

    temp folder to free up space.
    * The max size of files that can be uploaded may be specified within the form as well as within

    the PHP configuration file
    * It is possible to upload multiple files in one go.
    ------------

    The Tute:

    Users specify the file to upload by using the "Browse" button on your html form. This button and

    the accompanying textbox are actually a single "File" Control. Here's an example of such a button within a form:

    Code:
    <form enctype="multipart/form-data" action="file_submit.php" method="post">
    <input type="hidden" name="MAX_FILE_SIZE" value="1000">
    Upload: <input name="thefile" type="file"><input type="submit" value="Upload!"> </form>
    Note the form enctype value - keep it that way. The action value (file_submit.php) is the target

    php script that accepts the form submission.
    MAX_FILE_SIZE is an advice to the browser to limit the size of the file being uploaded - note

    that browsers may chose to ignore this limit.
    The "File" control is named "thefile" in this example. Lastly, there's a submit button to submit

    the form, and upload the file.

    When the user specifies a file, and submits the form, the browser starts sending the file over HTTP to your server. The server accepts the file, saves it to the "temp" folder, and starts
    execution of your target php script ("file_submit.php" in our e.g.).

    Your PHP script now needs to copy the file from the temp folder to where you need it. For this, you need the complete path of the uploaded file. This path is available in a variable that php makes for your script, which is:
    $HTTP_POST_FILES['thefile']['tmp_name'] - note that 'thefile' is the name we specified for the "File" control, and 'tmp_name' is a keyword.


    So within file_submit.php, do :
    PHP Code:
    copy ($HTTP_POST_FILES['thefile']['tmp_name'], $new_path); 
    There! You've uploaded a file, and saved it to the required location on your server!




    More:
    * Like the variable mentioned above, php makes other variables available for you to manipulate the upload, which are:
    1. $HTTP_POST_FILES['thefile']['name'] - the original name of the file on the user's machine
    2. $HTTP_POST_FILES['thefile']['type'] - the mime type of the file. For e.g. text/plain, image/gif. Note that it is upto the browser to provide the server this mime information.
    3. $HTTP_POST_FILES['thefile']['size'] - size of the uploaded file in bytes.


    ------------
    FAQ:
    Q: I want to specify a different default folder where files should be uploaded instead of the server "temp" folder -
    A: Use the "upload_tmp_dir" directive within php.ini - set it to your target folder.

    Q: What does the function move_uploaded_file() do?
    A: Handles moving the uploaded file to your custom target location. Only available upwards of PHP ver. 4.0.3.

    Q: I want to find out whether the user uploaded a document or an image!
    A: Use the $HTTP_POST_FILES['thefile']['type'] variable - whether it contains the string "image/" (the top level mime-type for images) or "text/" (plain text files/html files etc) or "application/" (documents like pdf, word...)

    Q: I also want to check whether a file was uploaded at all, within my php script!
    A: Use:
    PHP Code:
        if ($HTTP_POST_FILES['thefile']["size"] > 0)
        {
            
    // file was uploaded
        

    Q: What are the available mime types?
    A: Look here : http://www.isi.edu/in-notes/iana/***...es/media-types

    ---------------

    References:
    http://www.php.net/manual/en/features.file-upload.php
    http://www.php.net/manual/en/function.copy.php
    http://www.php.net/manual/en/functio...oaded-file.php
    http://www.phpbuilder.com/columns/bealers20000904.php3


    And while you are at it, do visit my site: Lyra Computing


    ERRATA
    scoutt on VBF pointed out the following that i overlooked:
    1. $HTTP_POST_FILES is deprecated - instead use the $_FILES array - this variable is a SuperGlobal always - don't use the global keyword with it within functions. Introduced in 4.1.0
    2. Using upload_tmp_dir to set the target folder for uploaded files is deemed a temporary directory by PHP, and will be cleaned by the server during cleanup. So be careful whlie using it. Haven't checked this out myself, but trying to get more information...
    Don't look so frightened
    This is just a passing phase

    One of my bad days...

  2.  

  3. #2
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    Psst, use &> instead of >, etc
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  4. #3
    Senior Member
    Join Date
    Apr 2002
    Location
    Cocooned in her thoughts
    Posts
    312
    Member #
    67
    Liked
    3 times
    psst..done.
    Don't look so frightened
    This is just a passing phase

    One of my bad days...


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 01:20 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com