|
|
|
|
|
|
| |
The Class also validates max file size and gives the possibility to control files types which are forbidden to be uploaded.
This class can be used as a start point in procedure of developing of FILE Manager.
Author: Zeljko Radulovic: January 8, 2006.
index.php
---------
| <?php
include "file.php";
include "messages.php";
/*
* In this method we are deleting file from given folder
*/
if($_GET['action']=="del_file"){
$a = new file();
//Parameter is file name which comes from $_GET[] - method
$a ->del($_GET['file_name']);
$a = new file();
//List of files in given folder
$k= $a->show_list();
if ($k!=false) {
include "show_file.php";
}else{
//System sends message to the user
$b=new messages();
$b->message="No files in given Folder";
$b->show_mess();
include "show_file.php";
}
/*
* In this method application uploads file in given folder
*/
}elseif(isset($add_file_act)){
$a = new file();
$a->userfile=$userfile;
$a->userfile_name=$userfile_name;
$a->userfile_size=$userfile_size;
$a->userfile_type=$userfile_type;
//Uploading file
$a->insert();
$a = new file();
$k= $a->show_list();
if ($k!=false) {
include "show_file.php";
}else{
$b=new messages();
$b->message="No files in given Folder";
$b->show_mess();
include "show_file.php";
}
}elseif($_GET['action']=="add_new_file"){
$a = new file();
//Application calls interface for uploading file
$a->show_insert();
}else{
$a = new file();
$k= $a->show_list();
if ($k!=false) {
include "show_file.php";
}else{
$b=new messages();
$b->message="No files in given Folder";
$b->show_mess();
include "show_file.php";
}
}
?> | |
show_file.php
-------------
| <?php
print("<table><tr><th colspan=3 >File List</th> ".
" </tr> <tr><td colspan=3> ".
" <a href=index.php?action=add_new_file>Add New</a> </td></tr>");
for($i=0;$i<$a->file_number;$i++){
print("<tr><td class=head>".$a->files[$i]."</td> ".
" <td class=odd><a href='storage/".$a->files[$i]."' target=_blank > ".
" Download</a><td/><td class=odd><a href='index.php?action=".
"del_file&file_name=".$a->files[$i]."' >Delete</a><td/></tr>");
}
print("</tr></table>");
?> | |
add_file.html
-------------
| <form enctype="multipart/form-data" action="index.php" method="post">
<TABLE>
<tr>
<th colspan="2">Add File </th>
</tr>
<tr>
<td class="head">
Send this file:
</td>
<td class="even">
<input name="userfile" type="file">
<input type="submit" value="Send File" name=add_file_act>
</td>
</tr>
</TABLE>
</form> | |
file.php
--------
| <?php
/*
* This class helps programmer in procedure of uploading
* files on server. Class also validate max file size and
* gives us possibility of controlling types of files which
* are forbidden to be uploaded.
* Author: Zeljko Radulovic: January 8, 2006.
* Site:http://pad.pmf.kg.ac.yu/articles
* Description: http://pad.pmf.kg.ac.yu/am/techer/index.php?action=preview&co_id=8
* Forum:http://pad.pmf.kg.ac.yu/RS_forum/index.php
*/
class file{
var $userfile; //uploading file
var $userfile_name;//uploading file name
var $files; //Array of files in given folder
var $upload_dir = 'storage';//uploading file folder
var $userfile_size; //uploading file size
var $userfile_type; //uploading file type
var $max_size= 500000;//maximum size of uploading file (b)
var $forbidden_types; //type of files which are forbidden to be uploaded
var $file_number;//number of files in given folder
var $f_t_n;//number of forbidden file types
function file(){
$this->forbidden_types[0]='application/pdf';
/*
* $this->forbidden_types[1]='some other type';
* .
* .
* .
*$this->forbidden_types[n]='some other type';
*/
//in constructor of this class we are setting forbidden types
$this->f_t_n=count($this->forbidden_types);
}
//Function, which shows us uploading form
function show_insert(){
include "add_file.html";
}
//Function which show us list of files in given folder
function show_list(){
$handle=opendir($this->upload_dir);
$n=0;
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$this->files[$n]=$file;
$n=$n+1;
}
}
$this->file_number=$n;
if ($n>0) {
return true;
}else{
return false;
}
closedir($handle);
}
//Uplading function
function insert(){
if (is_uploaded_file($this->userfile)) {
if ($this->userfile_size>$this->max_size) {
$b=new messages();
$b->message="File is to big - maximum file size is ".$this->max_size;
$b->show_mess();
}else{
$l=0;
for($i=0;$i<$this->f_t_n;$i++){
if ($this->forbidden_types[$i]==$this->userfile_type) {
$l=$l+1;
}
}
if ($l>0) {
$b=new messages();
$b->message="This type of file ".$this->userfile_type." is forbbiden to be upload </br>";
$b->show_mess();
}else{
copy($this->userfile, "storage/".$this->userfile_name );
}
}
}else{
$b=new messages();
$b->message="Possible file upload attack: filename ".$this->userfile;
$b->show_mess();
}
}
//function which deletes given file from given folder
function del($par){
unlink($this->upload_dir."/".$par);
}
}
?> | |
messages.php
------------
| <?php
/*This is a class, which shows messages - in this example.
* In production surround I am using Smarty Template - for
* implementing of this class.
*/
class messages{
var $mesaage;
function show_mess(){
print("<table bgcolor=#FFFF80><tr><td><b>Message</b></td><tr><td><i>");
print($this->message."</i></td></tr></table></br> ");
}
}
?> | |
Site: http://pad.pmf.kg.ac.yu/articles
Forum: http://pad.pmf.kg.ac.yu/RS_forum/index.php
Description: http://pad.pmf.kg.ac.yu/am/index.php?action=preg_kursa&co_id=8 |
|
| 3 lines of Code to extract Tar, Zip, Gzip etc.. Categories : PHP, Filesystem, PHP Classes, Compression | | | Class that allows the PHP developer to create and manage UNIX like password files suitable for use as Apache authentication password files.
Categories : HTTP, PHP, PHP Classes, Filesystem | | | Remote Archive (Zip, Tar, Gzip) downloader with FTP and local extration support Categories : PHP, FTP, Filesystem, PHP Classes, Compression | | | A File Browser Class.To Read Drives,Directories and Files .Files writing is also possible Categories : PHP, PHP Classes, Filesystem | | | Search and Replace Text : Searches Files for Specified Text and Replaces It by a Given Text Categories : PHP, PHP Classes, Search, Filesystem | | | Easy upload class Categories : PHP Classes, Filesystem, HTTP, PHP | | | Compare two texts and display a block of text with the differences between them. Categories : PHP, PHP Classes, Filesystem, Strings, Arrays | | | An efficient iterative and buffered text file reader Categories : PHP, Classes and Objects, Filesystem, PHP Classes, Log Files | | | PHP4 DirectoryIterator Class Categories : PHP, PHP Classes, Filesystem, Directories | | | Bs_IniHandler is a class that can read and write ini-style files (and strings) Categories : PHP, Filesystem, PHP Classes | | | filesplit : Split big text files in multiple small ones Categories : PHP, Log Files, Filesystem, PHP Classes | | | PHP Transfer data from text file to Mysql Table Categories : PHP, PHP Classes, Filesystem, Databases, MySQL | | | file class , uploade file , download file already uploaded on another website Categories : PHP, PHP Classes, Filesystem, Web Services | | | very simple ftp class Categories : PHP, PHP Classes, FTP | | | PHP Paypal IPN Integration Class v1.0.0 Categories : PHP, PHP Classes, Payment Gateways | |
| | | | Olaf Lederer wrote :1375
Hello,
its better to use unqiue names for classes, "file" is also a php function.
Olaf
| |
|
|
|