首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >上传图片后在php中预览

问上传图片后在php中预览
EN

Stack Overflow用户
提问于 2013-12-31 13:47:20
回答 1查看 419关注 0票数 0

嗨,我是php的新手,我想在上传图像后预览它,请告诉我应该如何做。我在zend框架上工作。在这件事上请帮帮我。

这是我的控制器代码。

代码语言:javascript
复制
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
        }
    }
}
}
}

这是我的脚本代码。

代码语言:javascript
复制
<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>

​

EN

回答 1

Stack Overflow用户

发布于 2013-12-31 13:54:25

传输后,打印图像URL并使用Javascript

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20852431

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档