上传后图片无法显示

时间:2013-12-31 07:35:17

标签: php zend-framework

Hii我是php新手,我正在尝试上传服务器上的图像并以表格形式预览。图像正确上传但未显示。我想预览它,请帮助我。

这是我的控制器代码。

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

            echo "<span style='color: green;'>Your file was successfully uploaded!</span>";
          echo json_encode(array('img'=>"<img src='public/image/upload/' />",'msg'=>""));
 return;
        }
    }
}
  }

这是我的index.phtml代码

<script>

$("#imageform").ajaxForm(
    {
        dataType:'json',
        success:function(json){
           $('#output').html(json.img);
           $('#preview').html(json.msg);
        }
    }).submit();

</script>

</head>
<body>
<div id="output"></div>
 <form action="" id="imageform" 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>

<div id='preview'>
</div>
</body>

1 个答案:

答案 0 :(得分:0)

您应在此处指定图片名称"<img src='public/image/upload/".$name."' />"