duanliao6789 2018-07-03 13:29
浏览 125

上传图片并单击它以显示在新标签中

I have worked out a little web app, need to select images by user and show it in the browser, and I did that, now want the user if click on that img, will open new browser tab and show the img as full Hight and Width. "Now when user click on the uploaded image that has recently uploaded them will appear in new tab and full H and W." my html code:

<div id="wrapper">
    <div id="content">
    <div class="container" style="color: white">

        <div class="row" style="margin-top: 150px; color: white">
            <div class="col-md-3"></div>
            <div class="col-md-6" id="double">
                <h2>Please Upload Your Images</h2>
                <form id="uploadForm" action="upload.php" method="post">
                    <h4>Select an Image</h4> <input name="files[]" type="file" multiple /><br /><br />
                    <input type="submit" value="Submit"/>
                </form>
            </div>
            <div class="col-md-3">
            </div>
        </div>

        </div>
</div>

my script:

<script>
    $(document).ready(function(){
        $('#uploadForm').on('submit', function(e){
            e.preventDefault();
        $.ajax({
            url : "upload.php",
            type : "POST",
            data : new FormData(this),
            contentType : false,
            processData : false,
            success : function(data){
                $('#gallery').html(data);
                alert("Image Uploaded");
            }
        });

    });
});

my php:

<?php 
//upload.php

$output = '';

if(is_array($_FILES)){
foreach ($_FILES['files']['name'] as $name => $value) {

    $file_name = explode(".", $_FILES['files']['name'][$name]);
    $allowed_ext = array("jpg", "jpeg", "png", "gif", "pdf");

    if (in_array($file_name[1], $allowed_ext)) {
        $new_name = md5(rand(). '.' . $file_name[1]);
        $sourcePath = $_FILES['files']['tmp_name'][$name];
        $targetPath = "upload/" . $new_name;

        if (move_uploaded_file($sourcePath, $targetPath)) {
            $output .= '<img src="'.$targetPath.'" width="150px" height="180px" />';
        }
    }
}
echo $output;
}

?>
  • 写回答

2条回答 默认 最新

  • doujiangao4229 2018-07-03 13:36
    关注

    So you want to display the images as a link which opens in a new tab / window? Try this:

    <?php
      echo '<a href="'.$LinkToFullSizeImage.'" target="_blank"><img alt="myAltText" src="'.$LinkToFullSizeImage.'" width="150px" height="180px"></a>';
    ?>
    

    This will wrap each image in a link that will, when clicked, open a new tab with the full size image displayed because of the target="_blank" attribute.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题