Google search

Basic and advanced computer skills like Excel with macros, How to speed up your PC, C, CPP, Java programming, HTML, JavaScript, PHP, Wordpress, all web tools, Android tutorials, MySQL Tutorials, WAMP server installation. etc.

Upload images to web server using php

The following example shows that how to upload and download images / files [.jpeg/.png/.bmp/ .txt/.doc etc] to web server [localhost or remote server] 

1. Create a HTML web page as following screen with code.
<html>
<head>
<title>Image/File Uploading Form</title>
</head>
<body background=bg.jpg>
<h2>File Upload:</h2>
Select a file to upload: <form action="file_uploader.php" method="post"  enctype="multipart/form-data">
<input type="file" name="file" size="50" /><br>
<input type="submit" value="Upload File" />
</form>
</body>
</html>
















2. Next, create a  PHP file called "file_uploader.php" and add following code to in it.
<html>
<body background=bg.jpg>
<?php
$uploaddir = '/wamp/www/Demo/uploaded/';
$uploadfile = $uploaddir. basename($_FILES['file']['name']);

if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Error in uploading files..!\n";
}
?>
<br><br>
<hr>
<a href="download.php">View Uploaded  files  </a>
<hr>
<br><br><br> <br><br><br><br><a href="home.php"> <img src="home-button-icon.png"> </a>
</body>
</html>

3. Click on "Choose File" button to browse and select file/image then click on upload.

4. Before running this page, create a folder called "uploaded" in the following location.
 /wamp/www/Demo/uploaded/ 

5. Now, Click on "Choose File" button to browse and select file/image then click on upload then it will show as following out put.

6. Now, to download the files from server / create a following PHP page for the same task.

7. Create File called  "download.php" and add following code to it.

<html>
<body background=bg.jpg>
<h1><font color=teal> Download files here </h1>
<hr>
<?php

$directory = 'uploaded'; 

echo "<table border=1 style='border:6px solid teal' cellpadding=5 cellspacing=6 align=center>";

foreach(glob($directory.'/*.*') as $file) 
{
echo "<tr>";
echo "<td>". $file . '-' . date ("m/d/Y", filemtime($file)). "</td>";
echo "<td>". '<a href="'.$file.'">'.$file.' - Size:'.filesize($file).'</a>'. "</td>";

echo "</tr>";
}
echo "</table>";
?>
<br><br><br> <br><br><br><br><a href="home.php"> <img src="home-button-icon.png"> </a>
</body>
</html>

8. Now run the above page to see below output and one can download any files from server.















That's all for now! 
keep browsing here for more on PHP image searching. 

Searching Images using PHP

To search images using PHP from MySQL Database, follow the examples.

First of all create a table in MySQL database with following fields.









Now, insert some images by clicking on "insert" tab in MySQL.






Now, create a HTML page as following tags.
<html>
<body>
<h1> Image searching.........</h1>
<form name=f1 action="image.php" method="GET">
Enter image Id: <input type=text name=t1>
<input type=submit value="Display"> 
</form>
</body>
</html>

After creating above HTML page, now create PHP config file with following code in it.[save as "config.php"] this file contains database name, user, password etc. 
<?php
      $host="localhost";
      $username="root";
      $password="";
      $dbname="mars_db";
      $con=mysql_connect("$host","$username","$password");
      mysql_select_db("$dbname",$con);
?>

Now create PHP file to search stored images in database.
Copy the following code and save as "image.php".
<?php
$key=$_GET['t1'];
include './config.php';
@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());

@mysql_select_db($dbname) or die("Can not select the database: ".mysql_error());

$query = mysql_query("SELECT * FROM photo WHERE pid=' ".$key."'");

$row = mysql_fetch_assoc($query);

$contents = $row['content'];

header("Content-type: image/jpg");
echo $contents;
?> 

Now, run HTML page as following.








Finally the output looks like this.













That's all for now!, Thank you for reading!
you can also refer other PHP examples here.