2

I am not able to upload files in a standalone machine.

I would like to know how to upload files with PHP/HTML. Provide me a solution with example - I am new to PHP.

2
  • have you read the php manual ? (php.net/manual/en/features.file-upload.php) Commented Dec 29, 2011 at 8:10
  • 1
    Normally I would agree with the others, research it. However, feeling generous, so I answered this one. : ) Commented Dec 29, 2011 at 8:23

3 Answers 3

2

This should help http://www.tizag.com/phpT/fileupload.php

Sign up to request clarification or add additional context in comments.

3 Comments

yes,i have gone through that link but i have not understand that code
any specific questions/problems?
@Ram can you be more specific? I can help you to understand.
2

I can recommend you this site.

http://www.w3schools.com/php/php_file_upload.asp

Cheers!

EDIT:

File upload is depend on several things in your php configuration

  • File size
  • File Write Permissions
  • Temp Directory

etc..

1 Comment

crystal ball's a little hazy today...what error? unless you provide a lot more detail, we can't help
2

Here's an example with error checking:

the html:

<!DOCTYPE html>
<html lang="en">
    <body>
        <form action="upload.php" method="post" enctype="multipart/form-data">
            <span>Filename: </span>
            <input type="file" name="file" />
            <br />
            <input type="submit" name="submit" value="Upload" />
        </form>
    </body>
</html> 

the php:

<?php
if (($_FILES["file"]["type"] == "image/gif") && ($_FILES["file"]["size"] < 20000))
{
    if ($_FILES["file"]["error"] > 0)
    {
        echo "Error Code: " . $_FILES["file"]["error"];
        echo "<br />";
    }
    else
    {
        echo "Uploaded File Name: " . $_FILES["file"]["name"];
        echo "<br />";
        echo "File Type: " . $_FILES["file"]["type"];
        echo "<br />";
        echo "File Size: " . ($_FILES["file"]["size"])." bytes";
        echo "<br />";
        echo "Temp file name: " . $_FILES["file"]["tmp_name"];
        echo "<br />";

        if (file_exists("uploadDir/" . $_FILES["file"]["name"]))
        {
            echo "A file with the name " . $_FILES["file"]["name"] . " already exists. ";
        }
        else
        {
            move_uploaded_file($_FILES["file"]["tmp_name"], "uploadDir/" . $_FILES["file"]["name"]);
            echo "The file was stored in: " . "uploadDir/" . $_FILES["file"]["name"];
        }
    }
}
else
{
    echo "Invalid file";
}
?>

The first if limits the type of file (in this case to gif) and the file size (in this case to 20000 bytes or less).

The second if checks if there was an error during the file transfer.

The third if checks if a file with that name already exists before copying the temporary file to its final location (in this case in the uploadDir).

6 Comments

i tried about about still i am getting error saying that invalid file
If the code above gives "Invalid file" error then the file you tried to upload is either not a gif or the file is larger than 20000 bytes. To use other types of files or files larger than 20000 bytes the code in the first if (in php) needs to be adjusted.
Thanks it was working fine now i am able to upload gif file suppose if we want to upload any type of file how to do that and where i have to make changes in code.
Wikipedia gives a list of mime types here: en.wikipedia.org/wiki/… You can replace image/gif in the first if with whichever you need. Or if you don't wish to restrict the file type (not recommended) that part of the if can be removed.
If you give me more details about what you are uploading I can give more details!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.