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
Screenshot1 |
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>
Screenshot3 |
Screenshot4 |
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.