Working with Images – PHP

Working with Images – PHP

We need to create an HTML form that allows you to choose the file to be uploaded.

Note: please make sure you have form method type is post and enctype=”mutipart/form-data” to to handle file uploading process successfully.

HTML Code

<form enctype=”multipart/form-data” action=”process.php” method=”POST”>

Choose File : <input name=”myfile” type=”file” />

<input type=”submit” value=”Upload” />

</form>

 

Now create a PHP file process.php for handling file information Please note you cannot view your uploaded file directly on this page. We will use $_FILES super global to retrieve uploaded file information.

 

Example: $_FILES[‘myfile’][‘name’];

$_FILES[][] is an associate 2-dimension array and keeps all the information related to uploaded file. The first [] contain name of file field[myfile] and second[] contain predefined attributes[name]

 

There are some other predefined attributes given below-

 

$_FILES[‘file’][‘tmp_name’] − the uploaded file in the temporary directory on the web server.

$_FILES[‘file’][‘name’] − the actual name of the uploaded file.

$_FILES[‘file’][‘size’] − the size in bytes of the uploaded file.

$_FILES[‘file’][‘type’] − the MIME type of the uploaded file.

$_FILES[‘file’][‘error’] − the error code associated with the file.

 

So let’s have an example-

<?php

// get the uploaded file name

$filename= $_FILES[‘myfile’][‘name’];

// get the file type

$filetype= $_FILES[‘myfile’][‘type’];

// get the file size (in bytes)

$filesize= $_FILES[‘myfile’][‘size’];

echo “<h2>Uploaded file information</h2> “;

echo “File Name : “.$filename;

echo “<br> File Size : “.$filesize;

echo “<br>File Type : “.$filetype;

?>

 

Once your file is uploaded successfully you may add some validations to make it more useful as per your requirement-

 

 

<?php

// get the uploaded file name

$filename= $_FILES[‘myfile’][‘name’];

// get the file type

$filetype= $_FILES[‘myfile’][‘type’];

// get the file size (in bytes)

$filesize= $_FILES[‘myfile’][‘size’];

// You may put your validations here i.e restrict file size if($filesize>=1000000)

{

echo “ File is too Large, Maximum file size should be 1 MB”;

exit;

}

// Validation to check if uploaded file is an Image

if($filetype !=”image/jpeg” && $filetype !=”image/png” && $filetype !=”image/JPEG”)

{

echo “ Only Images allowed , Kindly upload valid Image.”;

exit;

}

else

{

// create a folder – upload (in the same location where you have this PHP file) to move uploaded image from your local computer to server

$target = “upload/”;

$target = $target . basename( $_FILES[‘myfile’][‘name’]) ;

move_uploaded_file($_FILES[‘myfile’][‘tmp_name’], $target);

echo  ” Image Uploaded Successfully”;

}

?>

You will be saving your uploaded images to a folder on the web server, which means you need a directory that is writable by the web server. To make the folder writable by the web server, you can use your FTP client for the same.

To write the uploaded image to the target folder, you use the function move_uploaded_file. This PHP function will retrieve the image and move it to the designated location.

Enjoy the tutorial? Please spread the word :)

Follow by Email
Facebook
Facebook
Google+2k
Google+
http://hirdeshbhardwaj.com/2017/08/30/working-images-php-2
YouTube41
YouTube