當前位置:首頁 » 文件管理 » php上傳文件擴展

php上傳文件擴展

發布時間: 2022-08-18 14:53:13

php如何上傳文件

$f_type=strtolower("swf,rar,jpg,zip,7z,iso,gif,exe,rmvb");//設置可上傳的文件類型

if (!is_uploaded_file($file) || ($_FILES['photoupload']['size'] > 20000 * 1024 * 1024) )
{
$error = 'Please upload only files smaller than 2Mb!';
}
$f_name=$_FILES['photoupload']['name'];
$tmp_type=substr(strrchr($f_name,"."),1);//獲取文件擴展名
$tmp_type=strtolower($tmp_type);
if(!stristr($f_type,$tmp_type)){
$error="對不起,不能上傳格式為:".$tmp_type."的文件:".$f_name;

}
if(move_uploaded_file($_FILES['photoupload']['tmp_name'], $uploadfile)){
$result['result'] = 'success';
$result['size'] =$f_name."文件上傳成功";
}
else{
$result['result'] = 'failed';
$result['error'] =$f_name."上傳失敗";
}

⑵ 關於php實現文件上傳

php的文件上傳機制是把用戶上傳的文件保存在php.ini的upload_tmp_dir定義的臨時目錄(默認是系統的臨時目錄,如:/tmp)里的一個類似phpxXuoXG的隨機臨時文件,程序執行結束,該臨時文件也被刪除。PHP給上傳的文件定義了四個變數:(如form變數名是file,而且register_globals打開)

$file #就是保存到伺服器端的臨時文件(如/tmp/phpxXuoXG )

$file_size #上傳文件的大小

$file_name #上傳文件的原始名稱

$file_type #上傳文件的類型

推薦使用:

$_FILES['file']['tmp_name']

$_FILES['file']['size']

$_FILES['file']['name']

$_FILES['file']['type']

⑶ php獲取上傳文件擴展名$filename= md5(time().rand(0,9).$_FILES["filedata"]["name"]).".bmp";

$filename=md5(time().rand(0,9).$_FILES["filedata"]["name"]).'.'.pathinfo($_FILES["filedata"]["name"],PATHINFO_EXTENSION);

⑷ php 上傳文件

剛學php時寫的一個類,可以給你參考下,你所說的功能基本上也都有。

這個用作學習還是不錯的。


<?php
classfileup{
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();
}
//設置文件上傳的參數,不設置為默認值。
functionsetfilepar($par){
$pars=array('filetype','maxsize','savename');
foreach($paras$key=>$value){
if(in_array($key,$pars)){
$this->$key=$value;
}else{
continue;
}
}
}
//檢查上傳
privatefunctioncheckfileup(){
//判斷文件夾是否正確或文件夾是否有可寫入的許可權。
if(!is_dir($this->savefilepath)||!is_writable($this->savefilepath)){
$this->uperror=8;
returnfalse;
}
//判斷文件名是否存在
if(is_file($this->newname)){
$this->uperror=9;
returnfalse;
}
//判斷上傳文件的類型是否正確。
if(!in_array(strtolower($this->filehz),$this->filetype)){
$this->uperror=-1;
returnfalse;
}
returntrue;
}
//獲取新的文件名字
privatefunctiongetnewname(){
$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;
}
}

//獲取錯誤信息
privatefunctiongetuperror(){
switch($this->uperror){
case1:echo'上傳文件超過了系統指定的大小';break;
case2:echo'上傳文件超過了表單中指定的大小';break;
case3:echo'文件只有部分上傳';break;
case4:echo'沒有文件上傳';break;
case6:echo'找不到上傳的文件,系統錯誤';break;
case7:echo'文件寫入失敗';break;
case8:echo'文件路徑不存在,或不可寫';break;
case9:echo'文件名已經存在,請不要重復上傳';break;
case-1:echo'不是指定上傳的文件';break;
case-2:echo'請勿使用非法途徑上傳';break;
case-3:echo'文件上傳失敗';break;
default:'未知錯誤';break;

}
}


functionfileupload(){
if(!$this->checkfileup()||$this->uperror!=0){
$this->getuperror();
returnfalse;
}else{
if(!is_uploaded_file($_FILES[$this->upfileform]['tmp_name'])){
$this->uperror=-2;
$this->getuperror();
returnfalse;
}else{
if(move_uploaded_file($_FILES[$this->upfileform]['tmp_name'],$this->newname)){
returntrue;
}else{
$this->uperror=-3;
returnfalse;
}
}
}
}
//獲取文件名
functiongetname(){
return$this->newname;
}


}

⑸ 在網站設置上上傳照片時顯示檢測到環境未開啟php_fileinfo拓展,如何解決

找到配置文件,把這個擴展前面的分號去掉就可以了,前提是已經安裝了這個擴展

⑹ 請問在寫出php上傳組件時該如何限制文件的擴展名

function uploadFile(fileName)
{
var i=fileName.length;
var j=i-4;
var substr=fileName.substring(j,i);
if(substr==".doc"||substr==".xls"||substr==".jpg"){
document.forms[0].action.value="confirm";
document.forms[0].submit();
}
else{
alert("你上傳了錯誤的文件類型,只能上傳jpg、excel和word類型的文件");
return false;
}
}

//腳本,但是不安全,最好是交給程序內去判斷

⑺ php 怎樣上傳壓縮包並解壓到目錄

1.查找一般的php上傳類都可以上傳 zip 文件的。 (記得設置好上傳文件格式就好)

2.確認你的php擴展中 包含有 php_zip 這個擴展。
然後找 zip 的相關函數方法吧。 php手冊中去看。

⑻ php 文件上傳 需要開什麼擴展

應該不需要,剛剛後盾人講解過,樓主現在去後盾人學習一下,希望可以幫助你.

熱點內容
文件夾名字不顯示 發布:2025-05-14 07:27:47 瀏覽:773
oracle的資料庫驅動jar 發布:2025-05-14 07:23:20 瀏覽:553
我的世界電腦版伺服器手機版能進嗎 發布:2025-05-14 07:22:01 瀏覽:678
達內培訓php多少錢 發布:2025-05-14 07:19:10 瀏覽:26
python位元組轉字元串 發布:2025-05-14 07:06:35 瀏覽:421
subplotpython 發布:2025-05-14 06:53:51 瀏覽:661
豎屏大屏導航工廠密碼一般是多少 發布:2025-05-14 06:49:29 瀏覽:806
如何在手機里設置無線網密碼 發布:2025-05-14 06:47:54 瀏覽:120
動態ip文件伺服器 發布:2025-05-14 06:44:22 瀏覽:891
文字分行的腳本有什麼 發布:2025-05-14 06:33:10 瀏覽:288