Copy codeThe 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> ';
}
?>