Home  >  Article  >  Backend Development  >  PHP file upload code (restrict jpg files)_PHP tutorial

PHP file upload code (restrict jpg files)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:42:05857browse

Copy code The code is as follows:

/* Image upload class Only JPG format images*/
class uploadFile
{
var $inputName; //input name
var $fileName; //file name
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, return suffix
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 limit is ".$this->fileSize."bytes");
}
if(!file_exists($this->filePath)){
mkdir($this->filePath); //If the file storage directory does not exist, create it;
}
move_uploaded_file($_FILES[$this->inputName]["tmp_name"],
$this->filePath.$this- >fileName);
}
}
if($_GET['action']=="fileSave"){
$f=new uploadFile("file");
echo ' Upload successful! Browse';
}else{
echo '



';
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321040.htmlTechArticleCopy the code as follows: ?php /* The image upload class is limited to JPG format images*/ class uploadFile { var $ inputName; //input name var $fileName; //file name var $fileProperty; //file...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn