PHP file Upload class and detailed (_php) tutorial

Source: Internet
Author: User
Tags php file upload
PHP Tutorial Configuration in the Upload_tmp_dir this parameter to compare, if the file is under this directory, then Move_uploaded_file will be moved operation. And this comparison is case sensitive,/under Windows is not the same. And in the PHP configuration file parsing, will call a Realpath function, that is to say, before you move_uploaded_file,


Must be to $file[' tmp_name '] = Realpath ($file [' tmp_name ']); Realpath, please.


There is another situation, we have to note that move_uploaded_file if configured as an inaccessible path, then you do not care, Move_uploaded_file always can not move the file successfully.


In the file upload, with Move_uploaded_file This function can not move the file, and with copy or rename is indeed possible.


I have been very confused. In the document, said the more obscure, is move_uploaded_file this function, added a step check, check whether the file is HTTP POST upload,


Below I found a file upload class on the Internet

* * File Upload class **/

Class Upload_file
{
/** Declaration **/
var $upfile _type, $upfile _size, $upfile _name, $upfile;
var $d _alt, $extention _list, $tmp, $arri;
var $datetime, $date;
var $filestr, $size, $ext, $check;
var $flash _directory, $extention, $file _path, $base _directory;
var $url; Jump path after successful file upload;

function Upload_file ()
{
/** Constructor **/
$this->set_url ("index.php"); Jump path after initial upload success;
$this->set_extention (); List of initialization extensions;
$this->set_size (50); Initialize upload file KB limit;
$this->set_date (); Set directory name;
$this->set_datetime (); Set the file name prefix;
$this->set_base_directory ("Attachmentfile"); Initialization file upload root directory name, can be modified!;
}

/** File Type **/
function Set_file_type ($upfile _type)
{
$this->upfile_type = $upfile _type; Obtain the file type;
}

/** get filename **/
function Set_file_name ($upfile _name)
{
$this->upfile_name = $upfile _name; Obtain the file name;
}

/** Get File **/
function Set_upfile ($upfile)
{
$this->upfile = $upfile; Obtain temporary file names stored on the server;
}

/** Get File Size **/
function set_file_size ($upfile _size)
{
$this->upfile_size = $upfile _size; Obtain file size;
}

/** set file upload after successful jump path **/
function Set_url ($url)
{
$this->url = $url; Set the jump path after uploading the file successfully;
}

/** get file extension **/
function Get_extention ()
{
$this->extention = preg_replace ('/.*. *[^.]. *) */iu ', ' 1 ', $this->upfile_name); obtain a file extension;
}

/** Set file name **/
function Set_datetime ()
{
$this->datetime = Date ("Ymdhis"); Generate file names by time;
}

/** Set directory name **/
function Set_date ()
{
$this->date = Date ("y-m-d"); Generate catalog names by date;
}

/** initialization allows uploading of file types **/
function Set_extention ()
{
$this->extention_list = "Doc|xls|ppt|avi|txt|gif|jpg|jpeg|bmp|png"; The extension name that allows uploading by default;
}

/** set maximum upload KB limit **/
function Set_size ($size)
{
$this->size = $size; Set the maximum allowable upload file size;
}

/** Initialize the file Store root directory **/
function Set_base_directory ($directory)
{
$this->base_directory = $directory; Generate file storage root directory;
}

/** Initialize file storage subdirectory **/
function Set_flash_directory ()
{
$this->flash_directory = $this->base_directory. " /". $this->date; Generate file storage subdirectories;
}

/** Error Handling **/
function ShowError ($errstr = "Unknown error! "){
echo "";
Exit ();
}

/** Jump **/
function go_to ($STR, $url)
{
echo "";
Exit ();
}

/** Create a File store directory if the root directory is not created **/
function Mk_base_dir ()
{
if (! file_exists ($this->base_directory)) {//detects if the root directory exists;
@mkdir ($this->base_directory,0777); Does not exist then creates;
}
}

/** Create a File store directory if the subdirectory is not created **/
function Mk_dir ()
{
if (! file_exists ($this->flash_directory)) {// Detects if a subdirectory exists;
@mkdir ($this->flash_directory,0777);//does not exist and is created;
}
}

/** The file type allowed to be uploaded as an array **/
function get_compare_extention ()
{
$this->ext = explode ("|", $this->extention_list);//with "|" To decompose the default extension;
}

/** detect if the extension is illegal **/
function check_extention ()
{
for ($i =0;each ($this->ext); $i + +)//traversal array;
{
if ($this->ext[$i] = = Strtolower ($this->extention))//Compare file extensions to match the default allowed extensions;
{
$this->check = true;//matches the tag;
break;
}
}
if (! $this->check) {$this->showerror ("the correct extension must be". $this->extention_list. " One of them! ");}
//Inconsistent warning
}

/** detect if file size is exceeded **/
function check_size ()
{
if ($this->upfile_size > Round ($ this->size*1024))//The size of the file exceeds the default size;
{
$this->showerror ("Upload attachments must not exceed". $this->size. " KB "); exceeding the warning;
}
}

/** file Full access path **/
function Set_file_path ()
{
$this->file_path = $this->flash_directory. " /". $this->datetime.". $this->extention; Generate file full access path;
}

/** Uploading Files **/
function Copy_file ()
{
if (copy ($this->upfile, $this->file_path)) {//upload file;
Print $this->go_to ("The file has been successfully uploaded! ", $this->url); Upload success;
}else {
Print $this->showerror ("Unexpected error, please try again!") "); Upload failed;
}
}

/** Complete Save **/
function Save ()
{
$this->set_flash_directory (); Initialize the file upload subdirectory name;
$this->get_extention (); Get the file extension;
$this->get_compare_extention (); with "|" To decompose the default extension;
$this->check_extention (); detects file extension violations;
$this->check_size (); Check whether the file size is overrun;
$this->mk_base_dir (); Created if the root directory does not exist;
$this->mk_dir (); Created if the subdirectory does not exist;
$this->set_file_path (); Generate file full access path;
$this->copy_file (); uploading files;
}

}

1 2

http://www.bkjia.com/PHPjc/444804.html www.bkjia.com true http://www.bkjia.com/PHPjc/444804.html techarticle PHP Tutorial Configuration in the Upload_tmp_dir this parameter to compare, if the file is under this directory, then Move_uploaded_file will be moved operation. And this comparison is case- sensitive ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.