| Home » Categories » Multiple Categories |
File Upload in PHP - PHP File Upload Script |
|
Article ID: 34 | Rating: 3.5/5 from 2 votes | Last Updated: Fri, Nov 6, 2009 at 6:14 PM
|
| This script will help you lean how to upload files with PHP. PHP is undoubtedly the best programming language when it comes to web programming. It gives us so many features and capabilities, a few of which we’ve discussed already. So continuing with that, today we will see how we can use a few lines of PHP code to create a File Upload Script that would allow file uploading right from the web browser. File upload feature is definitely useful kinds of website but at the same time very much vulnerable to malicious attacks as well. So use it with a lot of precautions!
For this example, we will need a front-end web page (upload.html) that’ll accept the file from the user and a backend PHP file upload script (file-upload.php) to process and store the file. Let us look at the codes of each file.
<html>
<body>
<form action="file-upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
Here we have a HTML form that calls the script on submission. The method of data sending should be “POST” and there should be and enctype as “multipart/form-data” which means we can upload binary form data. The input type “file” opens the File Input Box that’d let the user browse for the file. The submit button “Upload” submits the form as usual.
PHP File Upload Script (file-upload.php)
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error:: " . $_FILES["file"]["error"] . "<br />";
}
else
{
if (file_exists($_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], $_FILES["file"]["name"]);
echo "File <b>" . $_FILES["file"]["name"] . "</b> uploaded successfully!";
}
}
?>
Just like we had POST form data in $_POST[] and GET data in $_GET[] array same way to have files sent to script we use $_FILES[] array.
move_uploaded_file(): As files sent to scripts are just stored temporarily, we have to save it to some place, for this we use this PHP function. It’s first parameter is the temporary file location and second is the place we need to store the file and with what name. We are storing it to the same directory the script is in and with name same as on the users’ computer.
NOTE: This is just meant for example purpose and you shouldn’t have this on your server as it is an open invitation to hackers/spammers. If you need upload facility on your website in all the cases you should have some kind of authentication system that’d only allow registered users to upload anything. you should also accept only certain file types. you wouldn’t like someone uploading/running spamming script off your server. Would you? there may be a few other precautions that you may need to take depending on the purpose you intend to use the script for. |
Attachments
There are no attachments for this article.
|
Comments
There are no comments for this article. Be the first to post a comment.
|
8 Basics of Regular Expressions
Viewed 458 times since Mon, Mar 8, 2010
50 Free And Quality Web Icons
Viewed 871 times since Tue, Jan 5, 2010
Free JavaScript Charts Library - JS Charts
Viewed 827 times since Sun, Dec 13, 2009
PHP Quiz Questions and Answers
Viewed 6050 times since Mon, Jan 11, 2010
PHP Interview Questions And Answers - PHP Test Quiz
Viewed 8452 times since Wed, Feb 17, 2010
Tiny Flash MP3 Audio Player
Viewed 843 times since Wed, Jan 6, 2010
10 Password Strength Meter Scripts to Check Password Strength
Viewed 4634 times since Sun, Jan 3, 2010
Web Usability Testing Tool - Clixpy
Viewed 773 times since Sat, Jan 9, 2010
25 Graph & Chart Solutions For Web Developers
Viewed 12962 times since Mon, Feb 1, 2010
How to control factors that cause common plant diseases
Viewed 557 times since Mon, Jul 4, 2011
|

Add Comment