欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php 生成自动创建文件夹并上传文件的示例代码

程序员文章站 2023-11-28 08:50:46
复制代码 代码如下:

复制代码 代码如下:

<?
session_start();
if($_session['company']=='')
{
 //exit();
}
?><?php //上传图片

$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;    //上传文件大小限制, 单位byte


  $addtime=date("ymd",time());     
  $testdir="./".$addtime."/";  
  if(file_exists($testdir)):  
  else:  
  mkdir($testdir,0777);  
  endif;  

$destination_folder=$addtime."/"; //上传文件路径
$imgpreview=1;    //是否生成预览图(1为生成,其他为不生成);
$imgpreviewsize=1/2;   //缩略图比例

if ($_server['request_method'] == 'post')
{
if (!is_uploaded_file($_files["pic"][tmp_name]))
//是否存在文件
{
echo "<font color='red'>文件不存在!</font>";
exit;
}

$file = $_files["pic"];
if($max_file_size < $file["size"])
//检查文件大小
{
echo "<font color='red'>文件太大!</font>";
exit;
   }

if(!in_array($file["type"], $uptypes))
//检查文件类型
{
echo "<font color='red'>只能上传图像文件或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'>同名文件已经存在了!</a>";
      exit;
}

if(!move_uploaded_file ($filename, $destination))
{
      echo "<font color='red'>上传文件出错!</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;//日后修改
$infor->issuetime = time();
$infor->viewnum = 0;
$infor->state = 1;//现在未定,日后修改
$infor->top = 0;
$infor->recommend = 0;
$infor->bookmember->id = 0;
$infor->booktime = 0;
$infor->bookremark = 0;
$bll_trade->createinfor($infor);
echo '发布信息成功!';
}
catch (exception $err){
 echo $err->getmessage();
}
?>