PHP file upload code of limit JPG file

  • 2020-03-31 20:17:47
  • OfStack


<?php 
 
class uploadFile 
{ 
var $inputName; //Input the name
var $fileName; //The file name
var $fileProperty; //File attributes
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 that return the dropout name
private function getProperty(){ 
if($_FILES[$this->inputName]["type"]=="image/pjpeg"||$_FILES[$this->inputName]["type"]=="image/jpeg"){ 
return ".jpg"; 
}else{ 
exit(" Wrong file format "); 
} 
} 
//File storage
private function fileSave(){ 
if($_FILES[$this->inputName]["size"]>$this->fileSize){ 
exit(" The file is too large , The maximum limit is ".$this->fileSize." byte "); 
} 
if(!file_exists($this->filePath)){ 
mkdir($this->filePath); //If the file storage directory does not exist, creates;
} 
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.'" />  Uploaded successfully ! <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>'; 
} 
?> 

Related articles: