javascript - Upload img and click on it to appear in new tab

442

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 >

    <div >
      <div ></div>
      <div 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 >
      </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;
}

?>
497

Answer

Solution:

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 thetarget="_blank" attribute.

942

Answer

Solution:

This is what you need;

if(move_uploaded_file($sourcePath, $targetPath)) {
    $ouput .= '<a href="'.$targetPath.'" target="_blank"><img src="'.$targetPath.'" width="150px" height="180px"></a>';
}

People are also looking for solutions to the problem: mysql - PHP $conn->closed() Doesn't Remove Process In PROCESSLIST table

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.