php generates sample code to automatically create folders and upload files

  • 2021-01-25 07:14:29
  • OfStack


<?
session_start();
if($_SESSION['Company']=='')
{
 //exit();
}
?><?php // To upload pictures 
$uptypes=array('image/jpg','image/jpeg','image/png','image/pjpeg','image/gif','image/bmp','application/x-shockwave-flash','image/x-png'); 
$max_file_size=5000000;    // Upload file size limit ,  unit BYTE

  $addtime=date("Ymd",time());      
  $testdir="./".$addtime."/";   
  if(file_exists($testdir)):   
  else:   
  mkdir($testdir,0777);   
  endif;   
$destination_folder=$addtime."/"; // Upload file path 
$imgpreview=1;    // Whether to generate a preview (1 To generate , Others are not generated );
$imgpreviewsize=1/2;   // Thumbnail scale 
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (!is_uploaded_file($_FILES["Pic"][tmp_name]))
// Is there a file? 
{ 
echo "<font color='red'> File does not exist! </font>";
exit;
}
$file = $_FILES["Pic"];
if($max_file_size < $file["size"])
// Check file size 
{
echo "<font color='red'> File too big! </font>";
exit;
   }
if(!in_array($file["type"], $uptypes))
// Check file type 
{
echo "<font color='red'> You can only upload image files or Flash ! </font>";
exit; 
}
if(!file_exists($destination_folder))
 mkdir($destination_folder);
$filename=$file["tmp_name"];
$image_size = getimagesize($filename); 
$pinfo=pathinfo($file["name"]);
$ftype=$pinfo[extension];
$PicName = time().".".$ftype;
$destination = $destination_folder.$PicName;
if (file_exists($destination) && $overwrite != true) 
{
      echo "<font color='red'> The file of the same name already exists! </a>";
      exit;
}
if(!move_uploaded_file ($filename, $destination))
{
      echo "<font color='red'> Error uploading file! </a>";
      exit;
}
$pinfo=pathinfo($destination);
$fname=$pinfo[basename];
}
?><?
$path = dirname(__FILE__);
require_once($path.'/../../Module/Factory.php');
$Factory = new Factory();
$BLL_Trade = $Factory->FactoryTrade();
try {
$Infor = new Infor();
$Infor->Title = $_POST['Title'];
$Infor->Deposit = $_POST['Deposit'];
$Infor->Hire = $_POST['Hire'];
$Infor->Location = $_POST['Location'];
$Infor->Pic = $destination;
$Infor->Intro = $_POST['Intro'];
if($_SESSION['MemberId'] ==''){
 $Infor->Member->ID='';
}else {
$Infor->Member->ID = $_SESSION['MemberId'];}
if($_POST['GoodsBarCode'] ==''){
 $Infor->Goods->BarCode = 0;
}else {
$Infor->Goods->BarCode = $_POST['GoodsBarCode'];}
$Infor->Class->ID = 0;// Changes in the future 
$Infor->IssueTime = time();
$Infor->ViewNum = 0;
$Infor->State = 1;// It is not decided now, it will be revised later 
$Infor->Top = 0;
$Infor->Recommend = 0;
$Infor->BookMember->ID = 0;
$Infor->BookTime = 0;
$Infor->BookRemark = 0;
$BLL_Trade->CreateInfor($Infor);
echo ' Successful release of information! ';
}
catch (Exception $Err){
 echo $Err->getMessage();
}
?>


Related articles: