Sample Code with Output File Upload : In PHP, we can upload file to a server fileupload.html #menu a #content #italictext <img src="../bookstore.jpg" width="100%" height="40%"></img> <div id="menu" align="center">
<br> <form enctype="multipart/form data" action="add.php" method="post"> Name: <input type="text" name="name"><br> E mail: <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> add.php #menu a #content #italictext
<img src="../bookstore.jpg" width="100%" height="40%"></img> <div id="menu" align="center"> <br> <?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.";?> view.php #menu a #content #italictext <img src="../bookstore.jpg" width="100%" height="40%"></img> <div id="menu" align="center"> <img src="../bookstore.jpg" width="100%"
height="40%"></img> <div id="menu" align="center"> <br> <?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.";?> view.php #menu a #content #italictext <img src="../bookstore.jpg" width="100%
<br> <?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>";?>