Quantcast
Channel: webdevelopplus.com » PHP Tutorial
Viewing all articles
Browse latest Browse all 6

PHP Code – FIle Uploading Restricting File Size & Extension

$
0
0

File Uploading in PHP Restricting File Size and File Extension

When we Use a form for File Uploading in PHP  then we need  to create a Uploading input of  type File which is used to create Uploading Input in HTML and By the Help of PHP Script we Upload the file to a desired location on the Server.But Some time we need to restrict the File Uploading For Like File Size and File Extension Here we use these Restriction in this tutorial.

Need For Restricting the Files

  1. Some one Upload a Malicious File which corrupt the other file on server.
  2. Some Upload the Large File Which create a less memory on server.

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<html>
<head>
<title> File Uploading Restricting File size and File Extension.</title>
</head>
<body>
 
<?php
$name = $_FILES['file']['name'];
$extension = strtolower(substr($name, strpos($name,'.') + 1)); 
$type = $_FILES['file']['type'];
$size = $_FILES['file']['size'];
 
$max_size = 100000; // Maximum Size Of File
 
$tmp_name = $_FILES['file']['tmp_name']; //Temporary name of File.
 
if(isset($name))
{
if (!empty($name) )
 
  {
    if(($extension=='jpg'|| $extension=='jpeg') &&
 $type=='image/jpeg' && $size<$max_size) // Checking the File Type and File size.
 
   {
     $location = 'uploads/'; // Location of File Uploaded It can be Changed by You.
 
if(move_uploaded_file($tmp_name,$location.$name)) //Moving Uploaded File
 {
  echo 'Uploaded!';
 }
else
 {
   echo'There was an error' ;
 }
}
else
  {
  echo'File must be jpg/jpeg and must be 1 MB 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>
</body>
<html>
<head>
<title> File Uploading Restricting File size and File Extension.</title>
</head>
<body>

<?php
$name = $_FILES['file']['name'];
$extension = strtolower(substr($name, strpos($name,'.') + 1)); 
$type = $_FILES['file']['type'];
$size = $_FILES['file']['size'];

$max_size = 100000; // Maximum Size Of File

$tmp_name = $_FILES['file']['tmp_name']; //Temporary name of File.

if(isset($name))
{
if (!empty($name) )

  {
    if(($extension=='jpg'|| $extension=='jpeg') &&
 $type=='image/jpeg' && $size<$max_size) // Checking the File Type and File size.

   {
     $location = 'uploads/'; // Location of File Uploaded It can be Changed by You.

if(move_uploaded_file($tmp_name,$location.$name)) //Moving Uploaded File
 {
  echo 'Uploaded!';
 }
else
 {
   echo'There was an error' ;
 }
}
else
  {
  echo'File must be jpg/jpeg and must be 1 MB 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>
</body>

 

 


Viewing all articles
Browse latest Browse all 6

Trending Articles