Upload files using PHP


Consider the html form:

<html>
<body>
<form action=”upload_file.php” method=”post” enctype=”multipart/form-data”>
<label for=”file”>Filename:</label>
<input type=”file” name=”file” id=”file” size=”20″ /><br />
<input type=”submit” name=”submit” value=”Submit” />
</form>
</body>
</html>

Script for saving the uploaded file://upload_file.php
<?php
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”];
}
?>

The script above checks if the file already exists, if it does not, it copies the file to the specified folder.This example saves the file to a new folder called “upload”

 

 

  1. November 22nd, 2009
    Trackback from : Trackback

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: