PHP封裝 檔案上傳

2022-07-11 18:42:13 字數 1914 閱讀 1577

<?php

function check($file, $folder='uploads', $prefix='')"))");

}#上傳檔案重新命名

$newname= uniqid($prefix,true).strrchr($file['name'],'.');

$newdir= "/".$newname

;

#檔案拓展名驗證

$info=finfo_open(fileinfo_mime_type);

$ext= finfo_file($info, $file['tmp_name']);

$allow= array('image/jpeg','image/png','image/gif');

if(!in_array($ext,$allow

))

#驗證檔案大小是否越界

$filesize= ini_get('upload_max_filesize');

if(substr($filesize, -1, 1)== 'k')

elseif(substr($filesize, -1, 1)== 'm')

elseif(substr($filesize, -1, 1)== 'g')

elseif(substr($filesize, -1, 1)== 't')

if($file['size']> $filesize

)

#驗證是否是http上傳

if(!is_uploaded_file($file['tmp_name']))

#上傳錯誤碼

if($file['error']> 0)

}#檔案上傳

if(move_uploaded_file($file['tmp_name'], $newdir

)) }

if(!empty($_post

)) ?>

doctype html

>

<

html

lang

="en"

>

<

head

>

<

meta

charset

="utf-8"

>

<

meta

name

="viewport"

content

="width=device-width, initial-scale=1.0"

>

<

title

>檔案上傳

title

>

head

>

<

body

>

<

form

action

="upload.php"

enctype

="multipart/form-data"

method

="post"

>

<

input

type

="hidden"

name

="max_file_size"

value

="30000000"

>

選擇檔案:

<

input

type

="file"

name

="userfile"

>

<

input

type

="submit"

value

="上傳檔案"

>

form

>

body

>

html

>

php檔案上傳函式封裝

上傳檔案呼叫 file files image 允許上傳的型別 檔案的上傳 param array file 上傳的檔案的相關資訊 是乙個陣列有五個元素 param array allow 允許檔案上傳的型別 param string error 引用傳遞,用來記錄錯誤的資訊 param strin...

php封裝檔案上傳函式

created by phpstorm.user 17839 date 2020 3 23 time 10 54 header content type text html charset utf 8 檔案上傳 param file 接受的檔案 files file param mime 允許上傳檔...

PHP封裝成類(檔案上傳)

uploadfile.class.php created by phpstorm.user huang date 2017 7 19 time 23 20 header content type text html charset utf 8 new new upload class upload ...