本篇文章主要是對php生成自動創建文件夾並上傳文件的示例代碼進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助
代碼如下: <? 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(); } ?>