-----controller
public function actionFileadd(){
$file = $_FILES['photoimg'];
$type=substr($file['name'],strrpos($file['name'],'.')+1);
$size=$file['size'];
$valid_formats = array("jpg", "png", "gif", "bmp");
if(!in_array($type,$valid_formats)){
echo "图片格式不符合要求";exit;
}
$filename = rand(111,999).time().'.'.$type;
$path = "./video/".$filename;
move_uploaded_file($file['tmp_name'],$path);
echo "<img src='".$path."' class='preview' width='100'>";
}
-----view
<script type="text/javascript" src="./js/j.js"></script>
<!-- form类 --!>
<script type="text/javascript" >
$(document).ready(function(){
$('#photoimg').change(function(){
$("#preview").html('');
$("#preview").html('<img src="./video/leader.bmp" alt="Uploading...." title="asd"/>');
$("#imageform").ajaxForm({
target:'#preview'
}).submit();
});
});
</script>
<div style="width:600px">
<form id="imageform" method="post" enctype="multipart/form-data" action='index.php?r=ee/fileadd'>
<input type="hidden" name="_csrf" value="<?php echo yii::$app->request->csrfToken ?>" />
上传 <input type="file" name="photoimg" id="photoimg" />
</form>
<div id='preview'></div>
</div>