Reputation: 121
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.
Upvotes: 2
Views: 273
Reputation: 12974
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).
Upvotes: 2
Reputation: 6190
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
etc..
Upvotes: 2