CopyCode The Code is as follows: <? PHP
/* Only JPG images can be uploaded */
Class uploadfile
{
VaR $ inputname; // input name
VaR $ filename; // file name
VaR $ fileproperty; // file Property
VaR $ filesize = 2097152; // file size limit, 2 m
VaR $ filepath = "upload/"; // file storage path
Function uploadfile ($ inputname ){
$ This-> inputname = $ inputname;
$ This-> getname (); // get a new name
$ This-> filesave ();
}
// Random name
Private function getname (){
$ This-> filename = date ("ymdhms"). Rand (0, 9). $ this-> getproperty ();
}
// File attribute, returns the suffix name
Private function getproperty (){
If ($ _ FILES [$ this-> inputname] ["type"] = "image/pjpeg" | $ _ FILES [$ this-> inputname] ["type"] = "image/JPEG ") {
Return ". jpg ";
} Else {
Exit ("File Format incorrect ");
}
}
// File storage
Private function filesave (){
If ($ _ FILES [$ this-> inputname] ["size"]> $ this-> filesize ){
Exit ("the file is too large, and the maximum value is". $ this-> filesize. "Byte ");
}
If (! File_exists ($ this-> filepath )){
Mkdir ($ this-> filepath); // if the file storage directory does not exist, create;
}
Move_uploaded_file ($ _ FILES [$ this-> inputname] ["tmp_name"],
$ This-> filepath. $ this-> filename );
}
}
If ($ _ Get ['action'] = "filesave "){
$ F = new uploadfile ("file ");
Echo '<input name = "textfield" type = "text" size = "30" value = "'. $ F-> filepath. $ F-> filename. '"/> upload successful! <A href = "'. $ F-> filepath. $ F-> filename.'"> browse </a> ';
} Else {
Echo '<form action = "? Action = filesave "method =" Post "enctype =" multipart/form-Data "name =" form1 "id =" form1 ">
<Input type = "file" name = "file" size = "30"/>
<Input type = "Submit" name = "Submit" value = "Submit"/>
</Form> ';
}
?>