Saturday, October 5, 2013

How to upload files or Images using PHP

A lot of websites these days allawing users to upload their files of a different types and sizes, and they still control of this operation to reject any file type or size, so I will show you in a simple codes how to upload files and add some restrictions for this operation.

File upload form

1
2
3
4
5
6
7
8
9
10
<html>
<body>
<H2>Simple form to upload files</H2><br>
<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Upload file">
</form>
</body>
</html>


PHP file uploader

simple php file uploader with a simple restrictions for uploaded files, the $_FILES["file"]["size"] return the size of the file by byte so I make a condition to be accept only size less than 1 Mb, and I check file extension to accept or reject files, for example I can accept all image extension like (png, gif, bmp, jpg, jpeg), but in my example I accept all files except executable files such as (exe, com, rar).
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>
</head>
<body>
<?
    $notAllowedExts = array("exe", "rar", "com");
    $AllowedSize = (1 * 1024 * 1024); //=>bytes.
 
    $temp = explode(".", $_FILES["file"]["name"]);
    $extension = end($temp);
?>
 
<h2>Uploaded File Info:</h2>
<ul>
<li>temp file: <?php echo $_FILES["file"]["tmp_name"];  ?>
<li>Sent file: <?php echo $_FILES['file']['name'];  ?>
<li>File size: <?php echo $_FILES['file']['size'];  ?> bytes
<li>File type: <?php echo $_FILES['file']['type'];  ?>
<li>File Extension: <?php echo $extension;  ?>
</ul>
<?
    if ($_FILES["file"]["size"] > $AllowedSize){
        echo '<font face="Tahoma" size="3" color="#F00000" ><b>The size of "'.
        $_FILES["file"]["name"] .'" file is more than '.
        ($AllowedSize / (1024*1024)).' Mb. </b></font>';
        return 0;
    }
 
    if (in_array($extension, $notAllowedExts)) {
        echo '<font face="Tahoma" size="3" color="#F00000" ><b>"'.
        $_FILES["file"]["name"] .'" - not accepted file. </b></font>';
        return 0;
    }
 
    if ($_FILES["file"]["error"] > 0){
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        return 0;
    }
 
 
    if (file_exists("t1/" . $_FILES["file"]["name"]))
        echo '<font face="Tahoma" size="3" color="#F00000" ><b>"'.
        $_FILES["file"]["name"] .'" - already exists. </b></font>';
    else{
        move_uploaded_file($_FILES["file"]["tmp_name"],  "t1/" .
        $_FILES["file"]["name"]);
        echo '<font face="Tahoma" size="3" color="#008000" ><b>"'.
        $_FILES["file"]["name"].'" - was successfully uploaded.</b></font>';
    }
?>
</body>
</html>




see more about php file uploader:

PHP File Upload
PHP File uploader

No comments:

Post a Comment

Share it

Online Marketing