PHP File upload code, limit jpg file
<?php
/* Picture upload Class www.lost63.com original code only JPG picture * *
Class UploadFile
{
var $inputName; Input name
var $fileName; File naming
var $fileProperty; File properties
var $fileSize = 2097152; File size limit, 2M
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 properties, returning the suffix name
Private Function GetProperty () {
if ($_files[$this->inputname]["type"]== "Image/pjpeg" | | $_files[$this->inputname]["type"]== "Image/jpeg") {
return ". jpg";
}else{
Exit ("Incorrect file format");
}
}
File storage
Private Function FileSave () {
if ($_files[$this->inputname]["size"]> $this->filesize) {
Exit ("The file is too large, maximum is limited to". $this->filesize. " byte ");
}
if (!file_exists ($this->filepath)) {
mkdir ($this->filepath); Established if the file storage directory does not exist;
}
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= "value=" '. $f->filepath. $f->filename. "/> Upload success! <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= "/>"
<input type= "Submit" name= "Submission" value= "submitted"/>
</form> ';
}
?>