XMPP JavaScript Library

File Uploader Manager in PHP

PHP, Sachin Puri, 2012-08-15 17:57:31

http://www.sachinpuri.com/library/1345033423_large.jpg

This is an easy to create and integrate file manager developed using PHP, HTML and CSS. I haven't implemented database in this, you can do this if you want.

I have tried to implement all sort of security in file upload, but if i am missing something please post your suggestions in comment box.

Please report any bug in comment box.

How to implement

1. create a new php file and copy paste give code.

2. create a folder named "uploads" in same directory where you have created your file

3. download following image and save it in same direcoty .

http://www.sachinpuri.com/library/1345034788_large.jpg

<?php
##########Config##################

$allowedFileType=array('image/jpeg','image/gif','image/bmp','image/png');
$allowedExtensions=array('jpg','gif','png','bmp');
$maxFileSize='200';//File Size in KB

#########Config###################

#Function to check for errors
function fileUploadErrorMessage($errcode){
    switch($errcode)
    {
        case UPLOAD_ERR_INI_SIZE:
            return "File size is greater then specified in php.ini file";
            break;
        case UPLOAD_ERR_FORM_SIZE:
            return "File size is greater then specified in form";
            break;
        case UPLOAD_ERR_PARTIAL:
            return "File partially uploaded";
            break;
        case UPLOAD_ERR_NO_FILE:
            return "No file was uploaded";
            break;
        case UPLOAD_ERR_NO_TMP_DIR:
            return "Either temp directory is not available or it is not writable";
            break;
        case UPLOAD_ERR_CANT_WRITE:
            return "Failed to write file to disk.";
            break;
        case UPLOAD_ERR_CANT_WRITE:
            return "Extension error.";
            break;
        default:
            return "Other error.";
            break;
    }
}


#Uploading File
$name='';
if(count($_POST)>0){
    $name=$_FILES['txtFile']['name'];
    $type=$_FILES['txtFile']['type'];
    $tmp_name=$_FILES['txtFile']['tmp_name'];
    $error=$_FILES['txtFile']['error'];
    $size=$_FILES['txtFile']['size'];
    $ext=substr($name,strrpos($name,'.')+1,strlen($name));

    if($error!=0){
        $errmsg=fileUploadErrorMessage($error);    
    }elseif(($size/1024)>$maxFileSize){
        $errmsg="File size can't be greate than 200KB";
    }elseif(!in_array($type,$allowedFileType) || !in_array($ext,$allowedExtensions)){
        $errmsg="Invalid file type";
    }else{
        if(move_uploaded_file($tmp_name,'uploads/'.$name)){
            $errmsg="File uploaded Sucessfully";
        }else{
            $errmsg="Some error occurred";
        }
    }
}

#Deleting file
if(isset($_REQUEST['del'])){
    unlink('uploads/'.$_REQUEST['del']);
    header('Location: ' . $_SERVER['PHP_SELF']);
}
?>
<html>
    <head>
        <style>
            body{font:normal 12px verdana}
            input{border:1px solid; font:normal 11px verdana}            
            #fileListing{position:relative; border:3px solid #515151; width:500px; height:300px; border-radius:5px; overflow:hidden; margin:0px auto}
            #fileListing ul{list-style:none; padding:0px; margin:0px; height:240px; overflow:auto}
            #fileListing ul li{background:#EBEBEB url('images.jpg') no-repeat left center; line-height:30px; text-indent:20px; }
            #fileListing ul li.alt{background:#DADADA url('images.jpg') no-repeat left center;}
            #fileListing ul li a{text-decoration:none; color:#333333}
            #fileListing ul li a:Hover{text-decoration:underline; color:red}
            #fileListing div{background:#515151; color:#fff; line-height:30px; text-indent:10px}
            #alert{position:absolute; width:50%; top:100px; left:20%; border:1px solid #333333; background:#999999; padding:10px; border-radius:5px; color:#fff; text-align:center; -moz-opacity:0.5; opacity:0.7; box-shadow:2px 2px 2px 2px #000;}
            .link-delete{float:right; margin-right:10px}                        
        </style>
    </head>
    <body>
       
        <?php if(strlen($name)>0){ ?>
            <!-- <img src="uploads/<?php echo $name ?>"/> -->
        <?php } ?>

        <div id="fileListing">
            <div>File Manager</div>
            <ul>                
                <?php
                $dir=dir('uploads');
                $i=1;
                while($entry=$dir->read()){
                    if($entry=='.' || $entry=='..')    continue;                        
                    $class=($i%2==0)?"alt":"";
                    $i++;
                    ?>
                    <li class="<?php echo $class ?>">
                        <a href="uploads/<?php echo $entry ?>" target="_new"><?php echo $entry ?></a>
                        <a class="link-delete" href="<?php echo $_SERVER['PHP_SELF'] ?>?del=<?php echo $entry ?>">Delete</a>
                    </li>
                <?php } ?>
            </ul>

            <?php if(isset($errmsg)){ ?>
            <span id="alert">
                <?php echo $errmsg ?><br/><br/>
                <input type="button" value="OK" onclick="document.getElementById('alert').style.display='none'"/>
            </span>
            <?php } ?>

            <div>
                <form name="frmFileUpload" method="post" enctype="multipart/form-data">
                    <label>Select a file</label>
                    <input type="file" name="txtFile"/>
                    <input type="hidden" name="MAX_FILE_SIZE" value="2" />
                    <input type="submit" value="Upload" name="cmdSubmit"/>
                </form>
            </div>
        </div>
    </body>
</html>



 

Download
Download Code
Note: Creating this type of File manage which don't use database are slow when there are many file to manage.
Add Your Comment
   
    Yes! I want to receive all comments by email

  by www.ebookgratuit.cc on 03-Mar-2019 11:12 pm
Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained!
  • Reply
  •  0 Like
  •  0 Dislike
  • Report