Location: PHPKode > scripts > Upload and list files > upload-and-list-files/fileupload.lib.php
<?php
/*
Created by Andras Zoltan-Gyarfas from Transilvania in 2009
email: hide@address.com
Use this script as you wish, just please send a notice for me.
For more questions/suggestions, please contact me.
*/ 

class files
{
var $db;
var $a;
var $upload_error;
var $lang=array();
var $types=array();
var $dir;

    public function __construct($c)
    {
    if(!empty($c['dir'])) { $this->dir="/".$c['dir']; }
    else { $this->dir="/dl"; }

    if(!empty($c['types'])) { $this->types = $c['types']; }
    else { $this->types=array("application/msword", "application/excel", "application/vnd.ms-excel", "application/x-excel", "application/x-msexcel", "application/rtf", "application/x-rtf", "text/richtext", "application/pdf", "application/mspowerpoint", "application/powerpoint", "application/vnd.ms-powerpoint", "application/x-mspowerpoint", "application/x-compressed", "application/x-zip-compressed", "application/zip", "multipart/x-zip", "application/octet-stream", "text/html", "image/jpg", "image/jpeg", "image/png", "image/gif"); }
    $this->upload();
    if(is_array($_POST['del']))
    {
        foreach($_POST['del'] as $k => $v)
        {
            if(unlink($_SERVER['DOCUMENT_ROOT'].$this->dir."/".$v))
            {}
        }
    }

        if ($k_azon = opendir($_SERVER['DOCUMENT_ROOT'].$this->dir))
        {
            while (false !== ($fajl = readdir($k_azon)))
            {
                if ($fajl != "." && $fajl != "..")
                {
                    $this->a[]=$fajl;
                }
            }
            closedir($k_azon);
        }
    }

    protected function upload()
    {
        if(isset($_POST['submit']))
        {
                if(isset($_FILES["file"]))
                {
                $this->upload_error.="";
                $format=$_FILES['file']["type"];
                $id=$_SESSION['login']['id'];
                $name=basename($_FILES["file"]["name"]);
                $dir=$_SERVER['DOCUMENT_ROOT'].$this->dir;
                if(!is_dir($dir))
                {
                    if(@mkdir($dir, 0777)===false)
                    {$this->upload_error.="The directory ".$dir." could not been created!"; return; }

                }

                $fname=$dir."/".$name;
                    $tname=$_FILES["file"]["tmp_name"];
                    if($name=="")
                    {
                        $this->upload_error.="No file uploaded: ".$name;
                        return;
                    }
                    if(!in_array($format, $this->types))
                    {
                        $this->upload_error.="Illegal file type: ".$format;
                        return;
                    }
                    else
                    {
                        if(@file_exists($fname) && @filesize($fname)==$_FILES["file"]["size"])
                        {
                            $this->upload_error.="File allready exists: <a href=\"".$_SERVER['name'].$this->dir."/".$name."\" target=\"_blank\">".$name."</a>";
                            return;
                        }
                        else
                        {
                            if(@file_exists($fname))
                            {
                            $fname=$dir."/1_".$name;
                            }
                            if(@move_uploaded_file($tname, $fname))
                            {
                                $this->upload_error.="Upload ok: <a href=\"".$_SERVER['name'].$this->dir."/".$name."\" target=\"_blank\">".$name."</a>";
                            }
                            else
                            {
                                $this->upload_error.="File could not been uploaded";
                            }
                        }
                    }
                }
        }
    }



    public function display_upload()
    {
    $ret="
                        <form enctype=\"multipart/form-data\" method=\"post\" action=\"\">
                        <table border=\"0\" cellspacing=\"0\" cellpadding=\"3\" id=\"uploadtable\">
                            <tr>
                                <td align=\"right\">
                                    Choose a file:
                                </td>
                                <td>
                                    <input type=\"file\" name=\"file\" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <input type=\"submit\" name=\"submit\" value=\"Upload\" />
                                </td>
                                <td>".$this->upload_error."</td>
                            </tr>
                        </table>
                        </form>
";
        return $ret;
    }


    public function display()
    {
        $ret="<p align=\"center\"><form method=\"post\" action=\"\">
<table cellpadding=\"3\" cellspacing=\"0\" border=\"0\" style=\"margin: 0px 0px 10px 0px;\"><tr><td colspan=\"2\" style=\"height: 1px\" bgcolor=\"silver\"></td></tr><tr><th>Delete file</th><th>File name</th></tr><tr><td colspan=\"2\" style=\"height: 1px\" bgcolor=\"silver\"></td></tr>";
        $l=array();
        if(is_array($this->a))
        {
        $size=0;
            foreach($this->a as $v)
            {
                if(file_exists($_SERVER['DOCUMENT_ROOT'].$this->dir."/".$v) && !in_array($v, $l))
                {
                $fs=round(filesize($_SERVER['DOCUMENT_ROOT'].$this->dir."/".$v)/1024, 1);
                $size+=$fs;
                    $ret.="<tr align=\"left\"><td width=\"100\" align=\"center\"><input type=\"checkbox\" name=\"del[]\" value=\"".$v."\" /></td><td><a href=\"".$_SERVER['name'].$this->dir."/".$v."\" target=\"_blank\">".$v."</a> (".$fs." KB)</td></tr><tr><td colspan=\"2\" style=\"height: 1px\" bgcolor=\"silver\"></td></tr>";
                }
            }
        }
        $ret.="</table>
        <input type=\"submit\" value=\"Delete checked\" />&nbsp;&nbsp;&nbsp;Total size of files: ".$size." KB</form></p>";
        return $ret;
    }
}
?>
Return current item: Upload and list files