scan4jobs

How to upload Image in PHP

Answered
0
0

How to upload Image in PHP

 

Image or document uploading is quite simple in Hypertext Preprocessor. By using the use of move_upload_file() we will circulate an Image or document  to your folder. We will copy the uploading image or document to your root folder via this Function. Here is the easy feature for add image in php. In this article we going to upload the document, take a look at the extension of the document  and validate, rename the file, move to folder, save the document name in db, get image to display.

  • You must to post comments
Good Answer
1
0

Create table to save the uploading file or image details:

CREATE TABLE `demo`.`upload` (
`id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`file_name` VARCHAR( 300 ) NOT NULL ,
`post_date` DATETIME NOT NULL 
) ENGINE = InnoDB;

Design form with ‘enc type’:

<h2>Image Upload in php</h2>
<?php if(isset($_GET["result"]) && $_GET["result"] == "error"){ echo "<h3 class=\"error\">Oops! something went wrong. Please upload again</h3>"; } ?>
<?php if(isset($_GET["result"]) && $_GET["result"] == "success"){ echo "<h3 class=\"success\">Yay yay!! Image uploaded successfully</h3>"; } ?>
<br/>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<table class="mytable">
<tr>
	<td><label>Select an image : </label></td>
	<td><input type="file" name="your_file" /></td>
</tr>
<tr>
	<td colspan="2"  align="center"><input type="submit" name="post_file" class="button" value="Upload This Image" /></td>
</tr>
<tr>
	<td colspan="2"  align="center"><input type="button" onClick="window.location='?view_images';" name="view_images" class="button" value="View Images" /></td>
</tr>
</table>
</form>

Add this snippet to your page and save the uploaded file or image into folder:

mysql_connect("localhost", "root", "") or die ("Oops! Server not connected"); // Connect to the host
mysql_select_db("demo") or die ("Oops! DB not connected"); // select the database
if(isset($_POST["post_file"])){
	if($_POST["post_file"] != ""){
		$valid_files = array("png", "jpg", "gif"); // include the valid image file extensions
		$file_name	 = $_FILES["your_file"]["name"]; // get the image name you posting
		$file_name_exp = explode(".", $file_name); // explode the uploaded image to get extension
		$file_extension = $file_name_exp[1]; // get extension
		if(in_array($file_extension, $valid_files)){ // check that image is valid or not
			$change_file_name = strtolower(str_replace(" ", "_", $file_name_exp[0])); // change the image name
			$file_name		  = $change_file_name.".".$file_extension; // join the new image name and its extension
			$folder_name	  = "uploads/"; // get the directory name which you want to upload the image make sure this directory has 0755 permision
			$target			  = $folder_name.$file_name; 
			$temp_name		  = $_FILES["your_file"]["tmp_name"]; // get the temp name of the image
			$move			  = move_uploaded_file($temp_name, $target); // move_uploaded_file function will let us to copy the file from your local drive to your root directory
			if($move){
				mysql_query("INSERT INTO `upload` (file_name, post_date) VALUES ('$file_name', now())");
				echo "<script type=\"text/javascript\">window.location = 'upload.php?result=success';</script>";	
			}else{
				echo "<script type=\"text/javascript\">window.location = 'index.php?result=error';</script>";	
			}
		}else{
			echo "<script type=\"text/javascript\">window.location = 'index.php?result=error';</script>";	
		}
	}else{
		echo "<script type=\"text/javascript\">window.location = 'index.php?result=error';</script>";	
	}
}

View the images and files which you uploaded:

<?php
if(isset($_GET["view_images"])){
?>
<br/>
<table class="mytable">
<tr>
	<td><label>Sno</label></td>
    <td><label>Image Name</label></td>
    <td><label>Image</label></td>
</tr>
<?php
	$select		= mysql_query("SELECT * FROM `upload` ORDER BY id DESC");
	$i = 0;
	while($fetch = mysql_fetch_array($select)){
	$i = $i + 1;
?>
<tr>
	<td><label><?php echo $i; ?></label></td>
    <td><label><?php echo $fetch["file_name"]; ?></label></td>
    <td><label><img src="uploads/<?php echo $fetch["file_name"]; ?>" alt="<?php echo $fetch["file_name"]; ?>" title="<?php echo $fetch["file_name"]; ?>" height="50" width="50" /></label></td>
</tr>
<?php 
	}
?>
</table>
<?php
}
?>
  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website