Upload Images to server using PHP

Simple example to upload images to server with PHP.

When we need to upload images/ files to server file manager(hosted in local server or hosted website) it is easy to create a PHP file using Dreamweaver. 
Using Dreamweaver
1. Go to File > New  and select PHP
2.Then add Following form anywhere between <body></body> tag.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<table bgcolor="#009900" align="center" width="300" border="1" cellspacing="0" cellpadding="10">
 
    <td class="tableheading">Image</td>
    <td><input type="file" name="fileupload" accept="image/jpeg" required/></td>
  </tr>
  <tr height="40" class="regTableTag1">
    <td>&nbsp;</td>
    <td><input type="submit" name="submit" value="PUSH" />
    </td>
  </tr>
 </table>
</form>



2.Then insert following image in order to preview the uploaded image.

1
2
3
4
5
6
7
8
9
 <table class="regTable" align="center" width="300" border="0" cellspacing="0" cellpadding="10">
 <tr height="40">
    <td width="300">
    <img src="photos/wallpaper.jpg" width="280" height="400"/>
    </td>
    
    </tr>
  
 </table>  

3.Finally add the php code on top of the html code. Make sure that there are no lines before php code.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<?php
if(isset($_POST['submit']))
{
 foreach($_FILES as $file_name => $file_array){
  if (is_uploaded_file($file_array['tmp_name'])) 
    {    
     move_uploaded_file($file_array['tmp_name'], "photos/wallpaper.jpg") ;
  $msg = "uploaded";
   }
 else
  {  
  $msg="Error : ".mysql_error();  
  }  
 header("Location:uploadMyPhoto.php?txt=$msg");
   
 }
}
?>

Before run this file make sure that you have created "photos" folder. Also if you run this in a local server put the php file inside default root folder.
To get the details of the button click HTTP method should "POST" in both form and isset() of php code section. PHP code segment will not executed if there is no data submitted by POST.

Complete php file


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
if(isset($_POST['submit']))
{
 foreach($_FILES as $file_name => $file_array){
  if (is_uploaded_file($file_array['tmp_name'])) 
    {    
     move_uploaded_file($file_array['tmp_name'], "photos/wallpaper.jpg") ;
  $msg = "uploaded";
   }
 else
  {  
  $msg="Error : ".mysql_error();  
  }  
 header("Location:uploadMyPhoto.php?txt=$msg");   
 }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body bgcolor="#0066FF">
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<table bgcolor="#009900" align="center" width="300" border="1" cellspacing="0" cellpadding="10">
 
    <td class="tableheading">Image</td>
    <td><input type="file" name="fileupload" accept="image/jpeg" required/></td>
  </tr>
  <tr height="40" class="regTableTag1">
    <td>&nbsp;</td>
    <td><input type="submit" name="submit" value="PUSH" />
    </td>
  </tr>
 </table>
</form>
 <table class="regTable" align="center" width="300" border="0" cellspacing="0" cellpadding="10">
 <tr height="40">
    <td width="300">
    <img src="photos/wallpaper.jpg" width="280" height="400"/>
    </td>   
    </tr>  
 </table>  
</body>
</html>




2 comments:

  1. Titanium Tail Bags - Stainless Steel Sledgehammer - TITAN
    Titanium Tail Bags - Stainless Steel titanium auto sales Sledgehammer - TITAN, Inc. is located in Tbilisi, Georgia gold titanium alloy and is a supplier titanium water bottle of apple watch titanium brass, stainless columbia titanium jacket steel,

    ReplyDelete