Upload Files using PHP

Related Articles

An Article in PHP to upload files to a remote server

1. Create a simple PHP page containing a form to get the
name, address and person photo(picture).

2. Create a simple database in Mysql (Example: Sample_Db) and a table (example: Customer)
with four fields ID, name, Address and File_Name. Let ID be Autonumber

3. Type the code below into Notepad and save it as Demo.php

<?
session_cache_limiter('nocache');
session_start();
$host = "localhost";
$dbusername = "";
$dbpassword = "";
$dbname = "sample";
$link_id=mysql_pconnect($host,$dbusername,$dbpassword);
mysql_select_db($dbname,$link_id);

if($_REQUEST['Mode']=="Save")
{
$Nm=$_REQUEST['Nm'];
$Addr=$_REQUEST['Address'];
if ($Nm !="" && $Addr!="" && $HTTP_POST_FILES['Fle']['name'] != "")
{
if(!(is_dir("Image_upload_Folder")))
{
mkdir("Image_upload_Folder");
}
copy($HTTP_POST_FILES['Fle']['tmp_name'],"Image_upload_Folder/".$HTTP_POST_FILES['Fle']['name']);

$Pict=$HTTP_POST_FILES['Fle']['name'];

$Sql="insert into stud([name],[Address],[File_Name]) values('".$Nm."','".$Addr."','".$Pict."')";
mysql_query($Sql);
}
}
?>

<html>
<head><title>Demo to upload file using PHP</title>
<script language="javascript">
function FnChk()
{
if(frm1.Nm.value=="")
{
alert("Enter your Name");
frm1.Nm.focus();
return false;
}
if(frm1.Address.value=="")
{
alert("Enter your Address");
frm1.Address.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<form name=frm1 method=post action="Demo.php?Mode=Save" enctype="multipart/form-data" onSubmit="return FnChk()">
<table>
<tr><td>Name :</td><td><input type=text name=Nm></td></tr>
<tr><td>Address :</td><td><textarea name=Address cols=20 rows=10></textarea></td></tr>
<tr><td colspan=2 align=center><input type=file name=Fle></td></tr>
<tr><td colspan=2 align=center><input type=submit name=Submit value=Submit></td></tr>
</table>
</form>
</body>
</html>

3. The above given program should be stored with the name Demo.php.

4. The commands given below saves the file onto a folder "Image_upload_Folder"

5. The Function: is_dir("Image_upload_Folder") checks whether a folder exists in the server. If not found then creates a folder with the name "Image_upload_Folder".
It then uploads the file to the server.

if(!(is_dir("Image_upload_Folder"))
{
mkdir("Image_upload_Folder");
}
copy($HTTP_POST_FILES['Fle']['tmp_name'],"Image_upload_Folder/".$HTTP_POST_FILES['Fle']['name']);

6. Thus it helps us to easily upload the customer's details along with their images.

7. The path of the image file is obtained from:

$HTTP_POST_FILES['Fle']['name']

and then stored in a variable, "$Pict".

$Pict=$HTTP_POST_FILES['Fle']['name'];

8. After uploading the image to the server, it then writes the details of the customer onto a table "Customer" in the database: "Sample_Db".


9. Any queries related to the program can be fired to "info@aesasp.com" for
more information kindly write to us at "info@aesasp.com" or visit "aesasp.com"
to post your Query.


Publication Date: Saturday 7th August, 2004
Author: U.swaroop View profile

Related Articles