File upload class php
Posted by in PhpPHP UPLOAD CLASS
Properties
You can choose type of file to be uploaded.
For every type of file to be uploaded, you can choose biggest size of file which is allowed.
Day, month and year choices are assigned as folder.( day/month/year or year/day depends on your wish.)
Usage
1- Write type of file and the biggest size of file as byte format to $file_types variable.For example, for jpep type “image/jpeg” => 30000. For more mime type, you can visit:
http://www.w3schools.com/media/media_mimeref.asp
2- Write the file to be uploaded to $path variable. For year “%year%” is used, for month “ %month% ” for day %day% are also used.As a example, your file name is af and for archiving the file monthly, “af %month%” is used.
3- To rename your file, choice of $new_name must be true otherwise choice must be changed as false.
4- $path_right is used for new file to be created.
5- Include “af_fileupload_class.php” file to the file to be used.
6- Create a new class, $a= new af_upload();
7- Send received file to upload function,
$a->upload ($_FILES ['uploadedfile']);
af_fileupload_class.php
<?php
/*
* @ Author: Onur AKTAŞ (alonon@gmail.com)
* @ Homepage: http://www.acikfikir.org
* @ Release date: 02.05.2010
* @ Version 1.0
* @ Thanks Süleyman Çelik, Alper İpek, Emre Çamalan
*/
class af_upload {
/* mime types and max size (as byte)
* 1000 byte = 1 KB
* for more mime type visit http://www.w3schools.com/media/media_mimeref.asp
*/
var $file_types = array ("image/jpeg" => 30000, "application/pdf" => 4000 );
// %year% for year, %month% for month, %day% for day, or just write exact path.
var $path ='alper/%year%/%month%/%day%';
// false don't give a new name, true give a new name.
var $new_name = FALSE;
var $path_right = '0777';
var $error = "";
var $is_error = FALSE;
public function control_file($file) {
if (!array_key_exists ( $file ['type'], $this->file_types )) {
$this->error='this type is not allowed';
$this->is_error = TRUE;
}
else {
if ($file ['size'] > $this->file_types [$file ['type']]) {
$this->error="file is too big";
$this->is_error = TRUE;
}
}
}
public function upload($file,$path="",$new_name="") {
$this->control_file($file);
if ($this->is_error) {
echo $this->error;
}
else {
$ext = substr(basename($file['name']), strrpos(basename($file['name']), '.') + 1);
//giving name start
if($this->new_name)
$filename = time().".".$ext;
else
$filename = basename($file['name']);
//giving name finish
// Replace path with real value start
$path_keys = array('%year%','%month%','%day%');
$replace_keys = array(date("Y"),date("m"),date("d"));
for($i=0;$i<=2;$i++) {
$this->path =str_replace($path_keys[$i], $replace_keys[$i], $this->path);
}
$umask = umask(0);
if(!is_dir($this->path)) {
if(!@mkdir($this->path,0777,true)) {
$this->error="can not create a folder";
$this->is_error = TRUE;
}
}
if(!@move_uploaded_file($file['tmp_name'], $this->path.'/'.$filename)) {
$this->error="can not upload file";
$this->is_error = TRUE;
}
umask($umask);
}
}
}
?>
index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title> Af php upload sınıfı</title> </head> <body> <form enctype="multipart/form-data" action="example.php" method="POST"> Dosya seçin: <input name="uploadedfile" type="file" /> <br /> <input type="submit" value="Upload File" /> </form> </body> </html>
example.php
<?php include "af_fileupload_class.php"; <pre>$a = new af_upload (); $a->upload ($_FILES ['uploadedfile']); ?>
To download, use this adress: www.acikfikir.org/dosyalar/af_upload_class.tar.gz For advice and question, contact mail : alonon {@} gmail.com
Related posts
You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.
