Sample Code with Output
Code : fileupload.html <html> <head> <title>opensofttraining.com</title> <style type="text/css"> #menu a { color:red; } #content { textalign:left; color:black; } #italictext { fontstyle:italic; } </style> </head> <body background="bodybg.gif"> <img src="../BookStore.jpg" width="100%" height="40%"></img> <div id="menu" align="center"> <a href="#">Home</a> |File Upload :
<a href="#">Engineering</a> | <a href="#">Medicine</a> | <a href="#">Law</a> | <a href="#">Management</a> | <a href="#">Contact</a> | <a href="#">About Us</a> </div> <br> <div id="content"><b> <form enctype="multipart/formdata" action="add.php" method="POST"> Name: <input type="text" name="name"><br> Email: <input type="text" name = "email"><br> Phone: <input type="text" name = "phone"><br> Photo: <input type="file" name="photo"><br> <input type="submit" value="Add"> </form> </div> </body> </html> add.php <html> <head> <title>opensofttraining.com</title> <style type="text/css"> #menu a { color:red; } #content { textalign:left; color:black; } #italictext { fontstyle:italic; } </style> </head> <body background="bodybg.gif">
Code : <img src="../BookStore.jpg" width="100%" height="40%"></img> <div id="menu" align="center"> <a href="#">Home</a> | <a href="#">Engineering</a> | <a href="#">Medicine</a> | <a href="#">Law</a> | <a href="#">Management</a> | <a href="#">Contact</a> | <a href="#">About Us</a> </div> <br> <div id="content"><b> <?php //This is the directory where images will be saved $target = "images/"; $target = $target . basename( $_FILES['photo']['name']); //This gets all the other information from the form $name=$_POST['name']; $email=$_POST['email']; $phone=$_POST['phone']; $pic=($_FILES['photo']['name']); // Connects to your Database mysql_connect("localhost", "root", "chennai123") or die(mysql_error()) ; mysql_select_db("test") or die(mysql_error()) ; //Writes the information to the database mysql_query("INSERT INTO employees(name,eamil,phone,path) VALUES ('$name', '$email', '$phone', '$pic')") ; //Writes the photo to the server if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { //Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } ?> </div> </body> </html> view.php <html> <head> <title>opensofttraining.com</title> <style type="text/css"> #menu a { color:red; } #content { textalign:left; color:black; } #italictext { fontstyle:italic; } </style> </head> <body background="bodybg.gif"> <img src="../BookStore.jpg" width="100%" height="40%"></img> <div id="menu" align="center"> <img src="../BookStore.jpg" width="100%"
Code : height="40%"></img> <div id="menu" align="center"> <a href="#">Home</a> | <a href="#">Engineering</a> | <a href="#">Medicine</a> | <a href="#">Law</a> | <a href="#">Management</a> | <a href="#">Contact</a> | <a href="#">About Us</a> </div> <br> <div id="content"><b> <?php //This is the directory where images will be saved $target = "images/"; $target = $target . basename( $_FILES['photo']['name']); //This gets all the other information from the form $name=$_POST['name']; $email=$_POST['email']; $phone=$_POST['phone']; $pic=($_FILES['photo']['name']); // Connects to your Database mysql_connect("localhost", "root", "chennai123") or die(mysql_error()) ; mysql_select_db("test") or die(mysql_error()) ; //Writes the information to the database mysql_query("INSERT INTO employees(name,eamil,phone,path) VALUES ('$name', '$email', '$phone', '$pic')") ; //Writes the photo to the server if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { //Tells you if its all ok
Code : echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } ?> </div> </body> </html> view.php <html> <head> <title>opensofttraining.com</title> <style type="text/css"> #menu a { color:red; } #content { textalign:left; color:black; } #italictext { fontstyle:italic; } </style> </head> <body background="bodybg.gif"> <img src="../BookStore.jpg" width="100% <a href="#">Home</a> |
Code : <a href="#">Engineering</a> | <a href="#">Medicine</a> | <a href="#">Law</a> | <a href="#">Management</a> | <a href="#">Contact</a> | <a href="#">About Us</a> </div> <br> <div id="content"><b> <?php // Connects to your Database mysql_connect("localhost", "root", "") or die(mysql_error()) ; mysql_select_db("test") or die(mysql_error()) ; //Retrieves data from MySQL $data = mysql_query("SELECT * FROM employees") or die(mysql_error()); //Puts it into an array while($info = mysql_fetch_array( $data )) { //Outputs the image and other data echo "<img src=http://localhost/fileupload/images/".$info['photo'] ."> <br>"; echo "<b>Name:</b> ".$info['name'] . "<br> "; echo "<b>Email:</b> ".$info['email'] . " <br>"; echo "<b>Phone:</b> ".$info['phone'] . " <hr>"; } ?> </div> </body> </html>