關於單檔案上傳的封裝

2022-05-13 02:04:32 字數 2356 閱讀 2842

<?php 

//$fileinfo=$_files['myfile'];/**

* @param $fileinfo string 上傳的檔案引數 name

* @param uploadpath string 檔案上傳的路徑,預設的為uploads

* @param allowext array 執行檔案上傳的型別 預設的為:'jpeg','jpg','gif','png'

* @param maxsize int 允許檔案上傳的最大限制,預設為2兆

//檢測上傳檔案的型別

if (!in_array ( $ext, $allowext ))

//$maxsize = 2097152;

//2m

//檢測上傳檔案大小是否符合規範

if ($fileinfo ['

size

'] >$maxsize)

//檢測是否為真實的型別

//$flag=true;

if($flag)

}//檢測檔案是否是通過http post方式上傳上來

if (! is_uploaded_file ( $fileinfo ['

tmp_name

'] ))

//$uploadpath = 'uploads';

if (!file_exists ( $uploadpath ))

$uniname = md5 ( uniqid ( microtime ( true ), true ) ) . '.'

. $ext;

$destination = $uploadpath . '/'

. $uniname;

if (! @move_uploaded_file ( $fileinfo ['

tmp_name

'], $destination ))

//echo '檔案上傳成功';

//return array(

//'newname'=>$destination,

//'size'=>$fileinfo['size'],

//'type'=>$fileinfo['type']

//);

return

$destination;

}

函式的呼叫如下doaction.php:

前段**如下:

"

-//w3c//dtd xhtml 1.0 transitional//en""

">

"">

PHP單檔案上傳(含封裝函式)

1,demo.php html htmllang en head metacharset utf 8 title 檔案上傳title head body formaction error2.php method post enctype multipart form data span 請上傳您的檔...

PHP單檔案上傳原理及上傳函式的封裝操作示例

表單 0.php 無標題文件 單檔案上傳函式的封裝 00.php 檔案上傳原理 將客戶端的檔案上傳到伺服器端,再將伺服器端的臨時檔案移動到指定目錄即可。檔案的方向 客戶端 伺服器 臨時檔案 指定目錄,當檔案進入伺服器時它就是臨時檔案了,這時操作中要用臨時檔案的名稱tmp name。在客戶端設定上傳檔...

PHP封裝 檔案上傳

function check file,folder uploads prefix 上傳檔案重新命名 newname uniqid prefix,true strrchr file name newdir newname 檔案拓展名驗證 info finfo open fileinfo mime t...