您的位置:首页 > 编程语言 > PHP开发

thinkphp 整合swfupload

2015-08-05 16:33 597 查看
在view下新建文件swfupload.html

代码如下

<span style="font-size:18px;"><span style="font-size:18px;"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>文件上传</title>
<link href="__PUBLIC__/swfupload/default.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.swfupload {
position: absolute;
z-index: 1;
}
</style>

<script type="text/javascript" src="__PUBLIC__/swfupload/js/swfupload.js"></script>
<script type="text/javascript" src="__PUBLIC__/swfupload/js/swfupload.queue.js"></script>
<script type="text/javascript" src="__PUBLIC__/swfupload/js/fileprogress.js"></script>
<script type="text/javascript" src="__PUBLIC__/swfupload/js/handlers.js"></script>
<script type="text/javascript">
var swfu;

SWFUpload.onload = function () {
var settings = {
flash_url : "__PUBLIC__/swfupload/js/swfupload.swf",
flash9_url : "__PUBLIC__/swfupload/js/swfupload_fp9.swf",
upload_url: "{:U('Uimages/upload_pl')}",
file_size_limit : "100 MB",
file_types : "*.jpg;*.gif;*.png",
file_types_description : "jpg图片,gif图片,png图片",
file_upload_limit : 100,
file_queue_limit : 0,
custom_settings : {
progressTarget : "fsUploadProgress",
cancelButtonId : "btnCancel"
},
debug: false,

// Button Settings
button_placeholder_id : "spanButtonPlaceholder",
button_width: 61,
button_height: 22,
button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
button_cursor: SWFUpload.CURSOR.HAND,

// The event handler functions are defined in handlers.js
swfupload_preload_handler : swfUploadPreLoad,
swfupload_load_failed_handler : swfUploadLoadFailed,
swfupload_loaded_handler : swfUploadLoaded,
file_queued_handler : fileQueued,
file_queue_error_handler : fileQueueError,
file_dialog_complete_handler : fileDialogComplete,
upload_start_handler : uploadStart,
upload_progress_handler : uploadProgress,
upload_error_handler : uploadError,
upload_success_handler : uploadSuccess,
upload_complete_handler : uploadComplete,
queue_complete_handler : queueComplete	// Queue plugin event

};

swfu = new SWFUpload(settings);
}

</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div id="content">
<h2>图片上传</h2>
<form id="form1" action="" method="post" enctype="multipart/form-data">
<div id="divSWFUploadUI">
<div class="fieldset flash" id="fsUploadProgress">
<span class="legend">上传状态</span>
</div>
<p id="divStatus">已上传0个文件</p>
<p>
<span id="spanButtonPlaceholder"></span>
<input id="btnUpload" type="button" value="选择文件" style="width: 61px; height: 22px; font-size: 8pt;" />
<input id="btnCancel" type="button" value="取消上传" disabled="disabled" style="margin-left: 2px; height: 22px; font-size: 8pt;" />
<input id="hdFileName" name="hdFileName" type="hidden" value="" />
</p>
<br style="clear: both;" />
</div>
</form>
</div>

</body>
</html>
</span></span>


在要批量添加图片的页面添加

<span style="font-size:18px;"><span style="font-size:18px;"><tr style="display:none">
<td>图片地址</td>
<td><input name="garrys" id="garrys" type="text"  size="30" style="vertical-align: middle;" /></td>
</tr>
<tr>
<td>图片批量上传</td>
<td>
<IFRAME name=fo frameBorder=0 marginHeight=1 marginWidth=1 scrolling=yes BORDERCOLOR="#CCCCFF" src="{:U('Uimages/swfupload')}" style="vertical-align: middle;width:100%; min-height:400px"></iframe>
</td>
</tr></span></span>


在controller页面添加

<span style="font-size:18px;"><span style="font-size:18px;">public function swfupload()
{
$this->display("Uimages/swfupload");
}

public function upload_pl()
{
$POST_MAX_SIZE = ini_get('post_max_size');
$unit = strtoupper(substr($POST_MAX_SIZE, -1));
$multiplier = ($unit == 'M' ? 1048576 : ($unit == 'K' ? 1024 : ($unit == 'G' ? 1073741824 : 1)));

if ((int)$_SERVER['CONTENT_LENGTH'] > $multiplier*(int)$POST_MAX_SIZE && $POST_MAX_SIZE) {
header("HTTP/1.1 500 Internal Server Error"); // This will trigger an uploadError event in SWFUpload
echo "POST exceeded maximum allowed size.";
exit(0);
}
$save_path =  APP_PATH ."upload/images/".date('Ym',time()).'/';				// The path were we will save the file (getcwd() may not be reliable and should be tested in your environment)
create_folders($save_path);
$upload_name = "Filedata";
$max_file_size_in_bytes = 2147483647;				// 2GB in bytes
$extension_whitelist = array("jpg", "gif", "png");	// Allowed file extensions
$valid_chars_regex = '.A-Z0-9_ !@#$%^&()+={}\[\]\',~`-';
$MAX_FILENAME_LENGTH = 260;
$file_name = "";
$file_extension = "";
$uploadErrors = array(
0=>"There is no error, the file uploaded successfully",
1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
3=>"The uploaded file was only partially uploaded",
4=>"No file was uploaded",
6=>"Missing a temporary folder"
);
if (!isset($_FILES[$upload_name])) {
HandleError("No upload found in \$_FILES for " . $upload_name);
exit(0);
} else if (isset($_FILES[$upload_name]["error"]) && $_FILES[$upload_name]["error"] != 0) {
HandleError($uploadErrors[$_FILES[$upload_name]["error"]]);
exit(0);
} else if (!isset($_FILES[$upload_name]["tmp_name"]) || !@is_uploaded_file($_FILES[$upload_name]["tmp_name"])) {
HandleError("Upload failed is_uploaded_file test.");
exit(0);
} else if (!isset($_FILES[$upload_name]['name'])) {
HandleError("File has no name.");
exit(0);
}
$file_size = @filesize($_FILES[$upload_name]["tmp_name"]);
if (!$file_size || $file_size > $max_file_size_in_bytes) {
HandleError("文件大小超过允许最大的文件");
exit(0);
}

if ($file_size <= 0) {
HandleError("文件太小");
exit(0);
}
$file_name = preg_replace('/[^'.$valid_chars_regex.']|\.+$/i', "", basename($_FILES[$upload_name]['name']));
$file_name=date('dHis',time()). $file_name;
if (strlen($file_name) == 0 || strlen($file_name) > $MAX_FILENAME_LENGTH) {
HandleError("无效文件名");
exit(0);
}
if (file_exists($save_path . $file_name)) {
HandleError("文件已经存在");
exit(0);
}
$path_info = pathinfo($_FILES[$upload_name]['name']);
$file_extension = $path_info["extension"];
$is_valid_extension = false;
foreach ($extension_whitelist as $extension) {
if (strcasecmp($file_extension, $extension) == 0) {
$is_valid_extension = true;
break;
}
}
if (!$is_valid_extension) {
HandleError("无效文件扩展名");
exit(0);
}
if (!@move_uploaded_file($_FILES[$upload_name]["tmp_name"], $save_path.$file_name)) {
//echo  $save_path.$file_name;
//echo $_FILES[$upload_name]["tmp_name"];
HandleError("文件不能保存.");
exit(0);
}
echo  str_replace('./','/',$save_path.$file_name);
exit(0);
}</span></span>


最后保存
<span style="font-size:18px;"><span style="font-size:18px;">                    $garrys = empty($_POST['garrys']) ?  '': $_POST['garrys'];
$garrys_arr = explode(",", $garrys);
//查看该相册下是否有封面图片
$model = D('Uimages');
$condition['cat_id'] = $data['cat_id'];

foreach ($garrys_arr as $key => $value)
{
if(!empty($value))
{

$data['img_path'] = $value;
$res = D('Uimages')->add($data);
}
}</span></span>


swfuploa中的handles.js中当上传成功后需要把上传的路径传递给父容器

function uploadSuccess(file, serverData) {
try {
var progress = new FileProgress(file, this.customSettings.progressTarget);
progress.setComplete();
progress.setStatus("Complete.");
progress.toggleCancel(false);
//把值赋给parent
window.parent.document.getElementById("garrys").value += serverData + ",";
} catch (ex) {
this.debug(ex);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: