Uploading Files to Your Web Site

Creating the Form:
Start out by creating a one-field form. You can create a form to upload as many files as you like after you get this sequence to work with one file.
1. Open a new file in your text editor and type the following HTML:
<HTML>
<HEAD>
<TITLE>Upload a File</TITLE>
</HEAD>
<BODY>
<H1>Upload a File</H1>
<FORM METHOD=”POST” ACTION=” do_upload.php” ENCTYPE=”multipart/form-data”>
<p><strong>File to Upload:</strong><br>
<INPUT TYPE=”file” NAME=”img1″ SIZE=”30″></P>
<P><INPUT TYPE=”submit” NAME=”submit” VALUE=”Upload File”></P>
</FORM>
</BODY>
</HTML>
2. save this file as upload_form.html and upload it to webserver.

Create the Upload Script:
Take a moment to commit the following list to memory—it contains the variables that are automatically placed in the $_FILES superglobal after a successful file upload. The base of img1 comes from the name of the input field in the original form.
  • $_FILES[img1][tmp_name]. The value refers to the temporary file on the Web server. 
  • $_FILES[img1][name]. The value is the actual name of the file that was uploaded. For example, if the name of the file was me.jpg, the value of $_FILES[img1][name] is me.jpg. 
  • $_FILES[img1][size]. The size of the uploaded file in bytes.
  • $_FILES[img1][type]. The MIME type of the uploaded file, such as image/jpg.
1. Open a new file in your text editor and start with PHP block:
<?
if ($_FILES[img1] != “”) {
copy($_FILES[img1][tmp_name],”C:/xampp/htdocs/practice/upload file/upload/”.$_FILES[img1][name]) or die(“Couldn’t copy the file.”);
} else {
die(“No input file specified”);
}
?>
<HTML>
<HEAD>
<TITLE>Successful File Upload</TITLE>
</HEAD>
<BODY>
<H1>Success!</H1>
<P>You sent: <? echo $_FILES[img1][name]; ?>,
a <? echo $_FILES[img1][size]; ?> byte file with
<? echo $_FILES[img1][type]; ?>.</P>
</BODY>
</HTML>
2. save as do_upload.php and upload it to webserver.

About the author

Being the CEO and Founder of ClecoTech International, Mr. Ashish Prajapati is dedicated towards his aim of mentoring young startups into a full-fledged businesses. He is helping startups from America, Europe, India, and various other countries through proper guidance and the use of latest technologies to develop their innovation and ideas into definite realities.

Leave a Reply

Your email address will not be published.