php Upload Picture Timestamp Name of Save Path

  • 2021-07-10 19:04:11
  • OfStack

html code:


<div id="images" style="width:250px;height:120px;background:#fff;border:1px solid #ccc;">  
  <h2><strong> Image import </strong></h2> 
  <form enctype="multipart/form-data" action="./includer/importimg.inc.php?action=img" method="post" name="imge">      
  <input type="hidden" name="MAX_FILE_SIZE" value="100000000" />
  <input value=" Import file " type="file" name="img" id="file"/><br/><br/>
  <input type="submit" id="imgbut" class="buttons" value=" Upload pictures " />                                          
  </form> 
</div>

php code:


<?php
/**
*Mwbe Version1.0
*-----------------------------------------------
*Copy 2013-2014 ylt
*Web: communicate
*-----------------------------------------------
*Author: tao *Data: 2014-7-22
*/
header("Content-Type:text/html;charset=utf-8");
//step 1  Use $_FILES['pic']["error"]  Check for errors 
if(isset($_GET["action"])=="img"){
if($_FILES["img"]["error"] > 0){
  switch($_FILES["img"]["error"]) {
  	case 1:
  	  echo "<script type='text/javascript'>alert(' Uploaded more files than  php.ini  Medium  upload_max_filesize  Option limits the value <br>');history.back();</script>";
  	  break;
  	case 2:
  	  echo "<script type='text/javascript'>alert(' The size of the uploaded file exceeds  HTML  In the form  MAX_FILE_SIZE  The value specified by the option ');history.back();</script>";
  	  break;
  	case 3:
  	  echo "<script type='text/javascript'>alert(' Only part of the file is uploaded ');history.back();</script>";
  	  break;
  	case 4:
  	  echo "<script type='text/javascript'>alert(' No files were uploaded ');history.back();</script>";
  	  break;
  	default:
  	  echo "<script type='text/javascript'>alert(' Unknown error ');history.back();</script>";
  }
  exit;
}
$maxsize=2000000; //50k
//step 2  Use $_FILES["pic"]["size"]  Limit size   Unit byte  2M=2000000
if($_FILES["img"]["size"] > $maxsize ) {
  echo "<script type='text/javascript'>alert(' The uploaded file is too large to exceed {$maxsize} Byte ');history.back();</script>";
  exit;
}
//step 3  Use $_FILES["pic"]["type"] Or the extension of the file   Restriction type  MIME image/gif image/png  gif png jpg
 /*  list($dl, $xl) = explode("/", $_FILES["pic"]["type"]);
 if($dl!="image"){
 echo " Please upload 1 Pictures, other types of files are not allowed ";
 exit;
 }
 */
 $allowtype=array("png", "gif", "jpg", "jpeg");
 $arr=explode(".", $_FILES["img"]["name"]);
 $hz=$arr[count($arr)-1];
 if(!in_array($hz, $allowtype)){
 echo "<script type='text/javascript'>alert(' This is a disallowed type ');history.back();</script>";
		exit;
 }
 //step 4  Will rename the passed file name 
 $filepath="../imgweb/";
 $fileimgweb="imgweb/";// In order to conform to UBB Path of 
	$randname=date("Y").date("m").date("d").date("H").date("i").date("s").rand(100, 999).".".$hz;
	  // Move the files in the temporary location to the specified directory 
	  if(is_uploaded_file($_FILES["img"]["tmp_name"])){
	    if(move_uploaded_file($_FILES["img"]["tmp_name"],$filepath.$randname)){
	      echo "<script type='text/javascript'>history.back();</script>";
	      session_start();
	      $_SESSION['images'] = $fileimgweb.$randname;
	      }else{
	      echo "<script type='text/javascript'>alert(' Upload failed ');history.back();</script>";
	      }
	}else{
		echo"<script type='text/javascript'>alert(' No 1 Upload files ');history.back();</script>";
	}	
}
?>

Related articles: