Return to Snippet

Revision: 64937
at October 6, 2013 20:58 by codingforever99


Initial Code
<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>

Initial URL
http://function-code.blogspot.com/2013/10/how-to-upload-files-or-images-using-php.html

Initial Description
simple php file uploader with check for file extension and size.

Initial Title
Upload files or Images using PHP

Initial Tags
php, file

Initial Language
PHP