本文目錄一覽:
- 1、php 文件上傳怎樣設置文件的類型為所有類型(jpg,doc,rar,等所有文件類型)
- 2、怎麼用php實現文件的上傳,要求文件類型為jpg,大小不超過2m,上傳的文件存放在u?
- 3、在PHP中怎麼實現文件的分類上傳?
- 4、php上傳文件代碼,能用的代碼
- 5、php 上傳文件
php 文件上傳怎樣設置文件的類型為所有類型(jpg,doc,rar,等所有文件類型)
一般PHP文件上傳時應自行設置檢查文件類型的,如果你不檢查就能上傳所有的文件類型了。
怎麼用php實現文件的上傳,要求文件類型為jpg,大小不超過2m,上傳的文件存放在u?
$_FILES官方文檔
你可以看看官方的$_FILES文檔,裡面有對$_FILES的內容的解釋。
想通過PHP來處理文件信息就得通過$_FILES的內容來處理,比如文件類型可以用type來判斷,要求文件類型為jpg,那就判斷if ($_FILES[‘file1’][‘type’] === ‘image/jpeg’),這裡的file1並不是絕對的,視情況而定。
當然如果你覺得判斷類型太麻煩,你也可以直接從name中判斷後綴名,自己將文件名分割一下就好了。大小可以用size,默認單位是字節,不超過2M就要除以1024*1024了,可以將字節轉換到兆字節。
要將上傳的文件放在U目錄下,就用move_uploaded_file函數來解決,move_uploaded_file官方文檔
在PHP中怎麼實現文件的分類上傳?
先判斷上傳文件的類型,不同的類型寫在不同的文件夾
?
if(isset($_POST[‘send’]) $_POST[‘send’]==’true’){
print_r($_FILES);
$type = $_FILES[‘file’][‘type’];
switch($type){
case ‘image/jpeg’:
$dfolder = ‘jpg’;
break;
case ‘application/pdf’;
$dfolder = ‘pdf’;
break;
case ‘text/plain’;
$dfolder = ‘txt’;
break;
}
mkdir($dfolder,0777);
if(move_uploaded_file($_FILES[‘file’][‘tmp_name’],$dfolder.’/’.$_FILES[‘file’][‘name’])){
echo ‘upload success’;
}
}else{
?
!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “”
html
head
title New Document /title
meta name=”Generator” content=”EditPlus”
meta name=”Author” content=””
meta name=”Keywords” content=””
meta name=”Description” content=””
/head
body
form name=”form1″ method=”post” enctype=”multipart/form-data”
input type=”file” name=”file”
input type=”hidden” name=”send” value=”true”
input type=”submit” value=”submit”
/form
/body
/html
? } ?
php上傳文件代碼,能用的代碼
?php
$uptypes=array(‘image/jpg’, //上傳文件類型列表
‘image/jpeg’,
‘image/png’,
‘image/pjpeg’,
‘image/gif’,
‘image/bmp’,
‘image/x-png’);
$max_file_size=5000000; //上傳文件大小限制, 單位BYTE
$destination_folder=”upload/”; //上傳文件路徑
$watermark=1; //是否附加水印(1為加水印,其他為不加水印);
$watertype=1; //水印類型(1為文字,2為圖片)
$waterposition=1; //水印位置(1為左下角,2為右下角,3為左上角,4為右上角,5為居中);
$waterstring=”newphp.site.cz”; //水印字符串
$waterimg=”xplore.gif”; //水印圖片
$imgpreview=1; //是否生成預覽圖(1為生成,其他為不生成);
$imgpreviewsize=1/2; //縮略圖比例
?
html
head
titleM4U BLOG – fywyj.cn/title
meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″
style type=”text/css”body,td{font-family:tahoma,verdana,arial;font-size:11px;line-height:15px;background-color:white;color:#666666;margin-left:20px;}
strong{font-size:12px;}
aink{color:#0066CC;}
a:hover{color:#FF6600;}
aisited{color:#003366;}
a:active{color:#9DCC00;}
table.itable{}
td.irows{height:20px;background:url(“index.php?i=dots” repeat-x bottom}/style
/head
body
centerform enctype=”multipart/form-data” method=”post” name=”upform”
上傳文件: brbrbr
input name=”upfile” type=”file” style=”width:200;border:1 solid #9a9999; font-size:9pt; background-color:#ffffff” size=”17″
input type=”submit” value=”上傳” style=”width:30;border:1 solid #9a9999; font-size:9pt; background-color:#ffffff” size=”17″brbrbr
允許上傳的文件類型為:jpg|jpeg|png|pjpeg|gif|bmp|x-png|swf brbr
a href=”index.php”返回/a
/form
?php
if ($_SERVER[‘REQUEST_METHOD’] == ‘POST’)
{
if (!is_uploaded_file($_FILES[“upfile”][tmp_name]))
//是否存在文件
{
echo “font color=’red’文件不存在!/font”;
exit;
}
$file = $_FILES[“upfile”];
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];
$destination = $destination_folder.time().”.”.$ftype;
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];
echo ” font color=red已經成功上傳/fontbr文件名: font color=blue”.$destination_folder.$fname.”/fontbr”;
echo ” 寬度:”.$image_size[0];
echo ” 長度:”.$image_size[1];
if($watermark==1)
{
$iinfo=getimagesize($destination,$iinfo);
$nimage=imagecreatetruecolor($image_size[0],$image_size[1]);
$white=imagecolorallocate($nimage,255,255,255);
$black=imagecolorallocate($nimage,0,0,0);
$red=imagecolorallocate($nimage,255,0,0);
imagefill($nimage,0,0,$white);
switch ($iinfo[2])
{
case 1:
$simage =imagecreatefromgif($destination);
break;
case 2:
$simage =imagecreatefromjpeg($destination);
break;
case 3:
$simage =imagecreatefrompng($destination);
break;
case 6:
$simage =imagecreatefromwbmp($destination);
break;
default:
die(“font color=’red’不能上傳此類型文件!/a”);
exit;
}
imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);
imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white);
switch($watertype)
{
case 1: //加水印字符串
imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);
break;
case 2: //加水印圖片
$simage1 =imagecreatefromgif(“xplore.gif”);
imagecopy($nimage,$simage1,0,0,0,0,85,15);
imagedestroy($simage1);
break;
}
switch ($iinfo[2])
{
case 1:
//imagegif($nimage, $destination);
imagejpeg($nimage, $destination);
break;
case 2:
imagejpeg($nimage, $destination);
break;
case 3:
imagepng($nimage, $destination);
break;
case 6:
imagewbmp($nimage, $destination);
//imagejpeg($nimage, $destination);
break;
}
//覆蓋原上傳文件
imagedestroy($nimage);
imagedestroy($simage);
}
if($imgpreview==1)
{
echo “br圖片預覽:br”;
echo “a href=\””.$destination.”\” target=’_blank’img src=\””.$destination.”\” width=”.($image_size[0]*$imgpreviewsize).” height=”.($image_size[1]*$imgpreviewsize);
echo ” alt=\”圖片預覽:\r文件名:”.$destination.”\r上傳時間:\” border=’0’/a”;
}
}
?
/center
/body
/html
絕對好用!!!!給分吧
php 上傳文件
剛學php時寫的一個類,可以給你參考下,你所說的功能基本上也都有。
這個用作學習還是不錯的。
?php
class fileup{
private $savefilepath; //保存路徑
private $filetype=array(‘gif’,’jpg’,’jpeg’,’png’); //文件類型
private $maxsize=1000000; //上傳最大的尺寸 默認值設置為1M
private $savename=true; //是否默認隨機名稱
private $upfileform; //上傳文件表單的name值
//以下是不可以修改的成員屬性
private $tmpname; //上傳的臨時文件名
private $upfilename; //上傳文件的名稱
private $uperror;
private $newname; //新的文件名
//private $upfiletype; //上傳文件的類型
private $upfilesize; //上傳文件的大小。
private $filehz; //文件名的擴展名。
//構造方法
function __construct($upfileform,$savefilepath=’./upload/’){
$this-upfileform=$upfileform;
$this-savefilepath=rtrim($savefilepath,’/’);
$this-tmpname=$_FILES[$upfileform][‘tmp_name’];
$this-upfilename=$_FILES[$upfileform][‘name’];
$this-upfilesize=$_FILES[$upfileform][‘size’];
$this-uperror=$_FILES[$upfileform][‘error’];
$this-getnewname();
}
//設置文件上傳的參數,不設置為默認值。
function setfilepar($par){
$pars=array(‘filetype’,’maxsize’,’savename’);
foreach($par as $key=$value){
if(in_array($key,$pars)){
$this-$key=$value;
}else{
continue;
}
}
}
//檢查上傳
private function checkfileup(){
//判斷文件夾是否正確或文件夾是否有可寫入的權限。
if(!is_dir($this-savefilepath)||!is_writable($this-savefilepath)){
$this-uperror=8;
return false;
}
//判斷文件名是否存在
if(is_file($this-newname)){
$this-uperror=9;
return false;
}
//判斷上傳文件的類型是否正確。
if(!in_array(strtolower($this-filehz),$this-filetype)){
$this-uperror=-1;
return false;
}
return true;
}
//獲取新的文件名字
private function getnewname(){
$tmp=explode(‘.’,$this-upfilename);
$this-filehz=$tmp[count($tmp)-1];
if(is_bool($this-savename)){
if($this-savename){
$this-newname=$this-savefilepath.’/’.date(‘YmdHis’).rand(10000,99999).’.’.$this-filehz;
}else{
$this-newname=$this-savefilepath.’/’.$this-upfilename;
}
}else{
$this-newname=$this-savefilepath.’/’.$this-savename.’.’.$this-filehz;
}
}
//獲取錯誤信息
private function getuperror(){
switch($this-uperror){
case 1: echo ‘上傳文件超過了系統指定的大小’; break;
case 2: echo ‘上傳文件超過了表單中指定的大小’; break;
case 3: echo ‘文件只有部分上傳’; break;
case 4: echo ‘沒有文件上傳’; break;
case 6: echo ‘找不到上傳的文件,系統錯誤’; break;
case 7: echo ‘文件寫入失敗’; break;
case 8: echo ‘文件路徑不存在,或不可寫’; break;
case 9: echo ‘文件名已經存在,請不要重複上傳’; break;
case -1: echo ‘不是指定上傳的文件’; break;
case -2: echo ‘請勿使用非法途徑上傳’; break;
case -3: echo ‘文件上傳失敗’; break;
default: ‘未知錯誤’; break;
}
}
function fileupload(){
if(!$this-checkfileup()||$this-uperror!=0){
$this-getuperror();
return false;
}else{
if(!is_uploaded_file($_FILES[$this-upfileform][‘tmp_name’])){
$this-uperror=-2;
$this-getuperror();
return false;
}else{
if(move_uploaded_file($_FILES[$this-upfileform][‘tmp_name’],$this-newname)){
return true;
}else{
$this-uperror=-3;
return false;
}
}
}
}
//獲取文件名
function getname(){
return $this-newname;
}
}
原創文章,作者:HEWI,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/140426.html