Sunday 30 March 2014

Uploading Files Restricting in php

Uploading Files the Basic we had Seen how to Upload Files Different Types or any file Which is insecure. Now let say you had a website that allow user's to upload images you may want to specify selected amount of file types that you can accept file extension that you want to allow to the user's to upload to your website You need to perform checks. For example i had uploaded .jpg files but i don't wanna allow php file or doc or any other type of file extension.

Code For uploading Files Restricting File In Php

<?php

$name = $_FILES['file']['name'];

$extension = strtolower(substr($name, strpos($name, '.') +1 ));

$size = $_FILES['file']['size'];

$type = $_FILES['file']['type'];



$tmp_name = $_FILES['file']['tmp_name'];



if (isset($name)) {

    if(!empty($name)) {

                   if(($extension=='jpg'||$extension=='jpeg')&&$type='image/jpeg') {

       

                        $location = 'uploads/';

       

                            if(move_uploaded_file($tmp_name, $location.$name)) {

                            echo 'Uploaded!';

                  }else {

                   echo 'There was an error.';

                   }

        

                   }else {

         echo 'File Must Be Jpeg/jpg.';

          }

    }else {

    

     echo 'Please Choose a file.';

    }

}



?>



<form action = "upload.php" method="POST" enctype="multipart/form-data">

<input type="file" name="file"><br><br>

<input type="submit" value="Submit">

</form>

Uploading Files Restricting in Php Output


File Extension in Php
Screenshot1
Uploading Files Restriction In Php
Screenshot2
As Its Uploading Files the basic we had seen uploading files Now Above Code is restricting File Extensions For Restricting File we can Restrict the file type which is uploaded.

So, Here we will check the file type for uploading system in php programming. We can do that will file type property in php programming. 

$type = $_FILES['file']['type'];

If I will Upload image file its type will be image/jpe also in Script we had check whether the file extension with substr function. 

$extension = strtolower(substr($name, strpos($name, '.') +1 ));

We had Two arguments such as $name and the . where we want to take the option and strtolower function will also identify the lower case and if it been in uppercase it will make it to lower case. Here we are using substr and strpos function to find out the extension of the file from the name of the file with extension. We getting out the extension to check whether the extension is in image format. After that we had checks with if else conditions. 

Code for uploading Restricting File Size

<?php

$name = $_FILES['file']['name'];

$extension = strtolower(substr($name, strpos($name, '.') +1 ));

$type = $_FILES['file']['type'];

$size = $_FILES['file']['size'];



$max_size = 2097152;







$tmp_name = $_FILES['file']['tmp_name'];



if (isset($name)) {

   if(!empty($name)) {

                   if(($extension=='jpg'||$extension=='jpeg')&&$type='image/jpeg' && $size<=$max_size)  {

       

                        $location = 'uploads/';

       

                            if(move_uploaded_file($tmp_name, $location.$name)) {

                            echo 'Uploaded!';

                  }else {

                    echo 'There was an error.';

                    }

        

                   }else {

         echo 'File Must Be Jpeg/jpg and must be 2mb or less.';

           }

    }else {

    

     echo 'Please Choose a file.';

    }

}



?>



<form action = "upload.php" method="POST" enctype="multipart/form-data">

<input type="file" name="file"><br><br>

<input type="submit" value="Submit">

</form>

Output

File Size Conversion
Screenshot3
uploading file size restriction in php
Screenshot4
In this example we are checking file size. So, we make sure that we had maximum security possible. What happen if we want to check file size now file size is actually process through in bytes. If we don't want image more then 2 megabytes. As you can see in screenshot3 google calculates 2 megabytes = 2097152 bytes. So we Just copy and paste bytes into the variables.

if(($extension=='jpg'||$extension=='jpeg')&&$type='image/jpeg' && $size<=$max_size)  {


This line of code i had perform simple check to whether the maximum size of the file is less then or equal to this size. with && another logical operators $size <= $max_size. also modify or error message file must be jpeg/jpg and must be 2 mb or less. So, now we are checking file must be an image file and also less then 2 megabytes. So, this is we are providing security to the server for uploading file as much as possible to allow image file extension upload and size restricting in php.


No comments:

Post a Comment

Thanks For Comment Will get you Soon..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates