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:
<TITLE>Upload a File</TITLE>
<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>
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");
<TITLE>Successful File Upload</TITLE>
<P>You sent: <? echo $_FILES[img1][name]; ?>,
a <? echo $_FILES[img1][size]; ?> byte file with
<? echo $_FILES[img1][type]; ?>.</P>
2. save as do_upload.php and upload it to webserver.

No comments:

Post a Comment