Tuesday, 16 April 2013

Reading extension of uploaded file in php | PHP Tutorials for Beginners

In this tutorial of php code for beginners we will show you how to fetch or read the extension of currently uploaded file.
There are two simple way to read the extension of file, one is by using the php string function and another is by user of fileinfo() function. Following example will show you the both way to read the extension of file.
1) $file_extension = strtolower(substr(strrchr($FILES['image']['name'],"."),1)); 

2) $file_extension = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
By use of any of above code you will be able to read or get the extention any file.

Hope this php tutorial is useful for you. Keep following Php Tutorials for Beginners for more help.

File Download Script in Php | PHP Tutorials for Beginners

In this tutorial of php code for beginners we will show you how to download any files from web server to local machine. When you want to download any file you need to send the file name to this application.

Step 1:-
Create index.html or any name for file and put the code in it. This is the html cede in which download link to the file is available. In download link pass the file name as query string. In bellow code "download.php" is the file where file downloading code is present. We are passing the file name of corresponding file which is to be downloaded.
<html>
<head>
<title>File download in php</title>
</head>
<body>
<table>
<tr><td><a href="download.php?file=download_file_in_php.jpg" class="links">Download file in php</a></td></tr>
<tr><td><a href="download.php?file=file_download.pdf" class="links">Download pdf</a></td></tr>
</table>
</body>
</html>


Step 2:-
Copy this code in a file and save it as "download.php". In above step when we click on link it will call the download.php file which is bellow. The bellow code recieve the file name which was sent in query string and process for downloading it. 


<?php
set_time_limit(0);

//path to the file.
//Your file should be in this folder only.
$file_path='files/'.$_REQUEST['file'];

//Call the download function with file path,file name and file type
download_file($file_path, ''.$_REQUEST['file'].'', 'text/plain');

function download_file($file, $name, $mime_type='')

{
 if(!is_readable($file)) die('File not found.');

 $size = filesize($file);
 $name = rawurldecode($name);

 $known_mime_types=array(

  "pdf" => "application/pdf",
  "txt" => "text/plain",
  "html" => "text/html",
  "htm" => "text/html",
"exe" => "application/octet-stream",
"zip" => "application/zip",
"doc" => "application/msword",
"xls" => "application/vnd.ms-excel",
"ppt" => "application/vnd.ms-powerpoint",
"gif" => "image/gif",
"png" => "image/png",
"jpeg"=> "image/jpg",
"jpg" =>  "image/jpg",
"php" => "text/plain"
 );

 if($mime_type==''){
$file_extension = strtolower(substr(strrchr($file,"."),1));
if(array_key_exists($file_extension, $known_mime_types)){
$mime_type=$known_mime_types[$file_extension];
} else {
$mime_type="application/force-download";
};
 };

 @ob_end_clean(); 


 // required for IE, otherwise Content-Disposition may be ignored
 if(ini_get('zlib.output_compression'))
  ini_set('zlib.output_compression', 'Off');

 header('Content-Type: ' . $mime_type);
 header('Content-Disposition: attachment; file="'.$name.'"');
 header("Content-Transfer-Encoding: binary");
 header('Accept-Ranges: bytes');
 header("Cache-control: private");
 header('Pragma: private');
 readfile($file); 
}
?>
Hope this php tutorial is useful for you. Keep following Php Tutorials for Beginners for more help.

Monday, 15 April 2013

Resize Image or Crop image Using GD Library in Php | PHP Tutorials for Beginners

Image resize or image cropping is one of the important part during any website building. To make our uploaded image fix exactly in same diamention in front end we need to resize or crop that image.
In this tutorial of Php code for beginners we will show you the example of code which resize the uploaded image using GD library.


This is the html code for uploading the image for resizing. After selecting image you will click on submit button. When submit button will click our next code will check for image and prosess them accordingly.
<html>
<head>
<title>Resize image in php</title>
</head>
<body>
 <form name = "form" action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "POST" enctype="multipart/form-data">
<table>
<tr>
<td><label>Insert Image to resize:-</label></td>
<td><input type = "file" name = "file" id = "file"></td>
</tr>
<tr>
<td colspan = 2>
<input type = "submit" value = "submit" name = "submit">
</td>
</tr>
</table>
 </form>
</body>
</html>

Now when you click on submit our php code check whether submit button is clicked or not? As soon as he find submit is clicked the following code run to prosses the image for resizing. 
<?php
if(isset($_POST['submit'])){
$error = 0;
$uploadfile = 'images/'.basename($_FILES['file']['name']);
// $uploadfile is directory name where original image will save. You can unlink it after resizing the image.

$resize =  'resize/';
//It is you resize image directory.

$file_name = basename($_FILES['file']['name']);
$filecheck = basename($_FILES['file']['name']);
$ext = strtolower(substr($filecheck, strrpos($filecheck, '.') + 1));
if (!(($ext == "jpg" || $ext == "gif" || $ext == "png") && ($_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/gif" || $_FILES["file"]["type"] == "image/png") && ($_FILES["file"]["size"] < 2120000))){
echo "File Not Supported";
$error = 1;
die();
}
// In above code we are checking the type and size of image. If it is not image file the error messege will appear.

if($_FILES['file']['name']!="")
{
ini_set('memory_limit','32M');
//Here we set the memory limit.

if (!move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) 
{
echo $message= "Error in uploading a file due to the size of image.\n";
$error = 1;
}
else 
{
$info = pathinfo($uploadfile);
$img = imagecreatefromjpeg( $uploadfile );
$width = imagesx( $img );
$height = imagesy( $img );
$new_width = 100; //New Width for Image.
$new_height = 100; //New Height for Image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
imagejpeg( $tmp_img, "{$resize}{$file_name}" );
// All processing is done and your resized image will store from temporary directory to your resize image directory
}
}
if($error==0) 
{
  echo "Image Uploaded Successfully.";
  exit;
}
}    
?>

Here is the Complete code for you to resize you image according to the height width set by you.
<?php
if(isset($_POST['submit'])){
$error = 0;
$uploadfile = 'images/'.basename($_FILES['file']['name']);
$resize =  'resize/';
$file_name = basename($_FILES['file']['name']);
$filecheck = basename($_FILES['file']['name']);
$ext = strtolower(substr($filecheck, strrpos($filecheck, '.') + 1));
if (!(($ext == "jpg" || $ext == "gif" || $ext == "png") && ($_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/gif" || $_FILES["file"]["type"] == "image/png") && ($_FILES["file"]["size"] < 2120000))){
echo "File Not Supported";
$error = 1;
die();
}

if($_FILES['file']['name']!="")
{
ini_set('memory_limit','32M');

if (!move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) 
{
echo $message= "Error in uploading a file due to the size of image.\n";
$error = 1;
}
else 
{
$info = pathinfo($uploadfile);
$img = imagecreatefromjpeg( $uploadfile );
$width = imagesx( $img );
$height = imagesy( $img );
$new_width = 100; 
$new_height = 100;
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height,     $width, $height );
imagejpeg( $tmp_img, "{$resize}{$file_name}" );
}
}
if($error==0) 
{
  echo "Image Uploaded Successfully.";
  exit;
}
}    
?>
<html>
<head>
<title>Resize image in php</title>
</head>
<body>
 <form name = "form" action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "POST" enctype="multipart/form-data">
<table>
<tr>
<td><label>Insert Image to resize:-</label></td>
<td><input type = "file" name = "file" id = "file"></td>
</tr>
<tr>
<td colspan = 2>
<input type = "submit" value = "submit" name = "submit">
</td>
</tr>
</table>
 </form>
</body>
</html>

Hope this php tutorial is useful for you. Keep following www.phpcodeforbeginner.blogspot.in for more help.