2009年12月9日 星期三

[php]用 class.upload.php 上傳檔案

class.upload.php





<?php
include('class_upload/class.upload.php'); // 引入物件檔,路徑請自行修改

// 設置變數
$dir_dest = (isset($_GET['dir']) ? $_GET['dir'] : 'test');
$dir_pics = (isset($_GET['pics']) ? $_GET['pics'] : $dir_dest);

if (!file_exists($dir_dest)) mkdir($dir_dest);

$handle = new upload($_FILES['photo_name'],'zh_TW');// 將上傳物件實體化
if ($handle->uploaded) { // 如果檔案已經上傳到 tmp
$handle->file_new_name_body = 'image_name'; // 重新設定新檔名
$handle->image_resize = true; // 重設圖片大小
$handle->image_x = 400; // 設定寬度為 400 px
$handle->image_ratio_y = true; // 按比例縮放高度
$handle->process($dir_dest); // 檔案搬移到目的地
if ($handle->processed) { // 判斷搬移執行結果
echo 'image resized';
echo '<fieldset>';
echo ' <legend>file uploaded with success</legend>';
echo ' <p>' . round(filesize($handle->file_dst_pathname)/256)/4 . 'KB</p>';
echo '<img src="'.$dir_pics.'/' . $handle->file_dst_name . '" />';
echo ' link to the file just uploaded: <a href="'.$dir_pics.'/' . $handle->file_dst_name . '">' . $handle->file_dst_name . '</a>';
echo '</fieldset>';
$handle->clean(); // 若搬移成功,則釋放記憶體
} else {
echo 'error : ' . $handle->error; // 秀出錯誤訊息。
}
}
?>

官網:http://www.verot.net/php_class_upload.htm
教學:http://www.verot.net/res/sources/class.upload.html

沒有留言: