在php上传后的图像预览

时间:2013-12-31 05:47:20

标签: php zend-framework

您好我是php的新手,我想在上传后预览图片,请告诉我该怎么做。我正在研究zend框架。请帮帮我。

这是我的控制器代码。

public function indexAction()
{

$valid_formats = array("jpg", "png", "gif", "zip", "bmp");
$max_file_size = 1024*100; //100 kb
$path = "public/image/upload/"; // Upload directory
$count = 0;

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to exeicute all files
foreach ($_FILES['files']['name'] as $f => $name) {     
    if ($_FILES['files']['error'][$f] == 4) {
        continue; // Skip file if any error found
    }          
    if ($_FILES['files']['error'][$f] == 0) {              
        if ($_FILES['files']['size'][$f] > $max_file_size) {
            $message[] = "$name is too large!.";
            continue; // Skip large files
        }
        elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION),    $valid_formats) ){
            $message[] = "$name is not a valid format";
            continue; // Skip invalid file formats
        }
        else{ // No error found! Move uploaded files 
            if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
            $count++; // Number of successfully uploaded file
        }
    }
}
}
}

这是我的脚本代码。

<script>
$(document).ready(function() {
var f = $('form');
var l = $('#loader'); // loder.gif image
var b = $('#button'); // upload button
var p = $('#preview'); // preview area

b.click(function(){
// implement with ajaxForm Plugin
f.ajaxForm({
  beforeSend: function(){
    l.show();
    b.attr('disabled', 'disabled');
    p.fadeOut();
  },
  success: function(e){
    l.hide();
    f.resetForm();
    b.removeAttr('disabled');
    p.html(e).fadeIn();
  },
  error: function(e){
    b.removeAttr('disabled');
    p.html(e).fadeIn();
  }
});
});
});


</script>



<body>

<form action="" method="post" enctype="multipart/form-data">
<input type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
<input type="submit" id="submit" value="Upload!" />

</form>

1 个答案:

答案 0 :(得分:0)

转让后打印图片网址并使用Javascript