Saving the Uploaded File

Posted by tutor | Posted in PHP Tutorial | Posted on 04-08-2010-05-2008

0

While uploading files by above codes, a temporary copy of the uploaded files is created in the PHP temp folder on the server.

These temporarily copied files disappear, once the uploading is done or when the script ends. To store the uploaded file we need to copy it to some secure and defined location:

    <?php
    if ((($_FILES["file"]["type"] == “image/gif”)
    && ($_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 format or size”;
    }
    ?>

The above script first checks for the availability of the file, if it does not exist, it copies the file to the specified folder.

Note: This script is for learnig purposes only.

Write a comment

Twitter Users
Enter your personal information in the form or sign in with your Twitter account by clicking the button below.