ホームページ  >  記事  >  php教程  >  PHP アップロードのクラスを一緒に行う方法を学びましょう

PHP アップロードのクラスを一緒に行う方法を学びましょう

WBOY
WBOYオリジナル
2016-06-21 08:50:57859ブラウズ

up.class.php

クラスアップ{

パブリック $f // $_FILES[xx]

を表します。

Public $s //アップロードされたファイルのサイズ

public $n; //ファイル名を指定するかどうか

public $d; //フォルダーを指定するかどうか

function __construct($f,$s=1,$n=false,$d=false){

$this->f=$f;

$this->s=$s*1000000;

$this->n=$n;

$this->d=$d;

$this->loading();

}

関数ロード(){

$this->fsize();

$this->ftype();

$this->fmv();

}

関数 fname(){

if($this->n){

return $this->n.$this->ftype();

}その他{

return time().rand(111,999).$this->ftype();

}

}

関数 fdir(){

if($this->d){ //ユーザーがフォルダー名を指定したかどうかを判断します

if(!is_dir($this->d)){ //指定されたフォルダー名が存在するかどうかを判定します

mkdir($this->d);

}

return $this->d."/";

}その他{

if(!is_dir(date("Ymd"))){

mkdir(date("Ymd"));

}

return date("Ymd")."/";

}

} //fdir

を終了します

関数 fsize(){

If($this->f['size']>$this->s){

echo "ファイルのサイズが超過しています";

exit();

}その他{

true を返します;

}

}

関数 ftype(){

switch($this->f['type']){

case "image/x-png": $ok=".png";

休憩;

case "image/png": $ok=".png";

休憩;

case "アプリケーション/pdf": $ok=".pdf";

休憩;

case "image/pjpeg": $ok=".jpg";

休憩;

case "image/jpeg": $ok=".jpg";

休憩;

case "画像/jpg": $ok=".jpg";

休憩;

デフォルト: $ok=false;

休憩;

}

if($ok){

$ok を返します;

}その他{

echo "ファイルの種類が正しくありません";

exit();

}

}

関数 fmv(){

Move_uploaded_file($this->f['tmp_name'],$this->fdir().$this->fname());

}

}

?>

test.php アップロード ファイル

include("up.class.php");

if(!empty($_POST['sub'])){

foreach($_FILES as $id=>$v){

新しいアップ($_FILES[$id]);

}

}

?>

<フォームアクション=""メソッド="ポスト" enctype="multipart/form-data">









声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。