8

I want to show the user a image before the user uploads it to the server to make sure that this image is what the user wanted to upload.

I have tryed to do this with $_FILES['name']['tmp_name'] and put this in a tag but nothing happens.

 <form action='' method='POST'  enctype="multipart/form-data">
        <header class='pageHeading'>Kop afbeedling toevoegen</header>            
        <section class='body'>               
            <div class='inputFrame'>  


                <img src="<?php if(isset($_FILES['image']['tmp_name'])){echo $_FILES['image']['name'];}?>"/>

                        <div class='input'>
                            <div class="cellFrame">
                                <div class="inputHeading">Afbeelding uploaden</div>
                                <div class="frame">
                                    <div class="frameAlign">
                                        <div class="displayFlie">
                                            <input type='file' name='image'/>
                                        </div>
                                        <button name='upload' class='btnUpload btn'>Upload</button>
                                        <div class="clr"></div>
                                    </div>
                                </div>
                            </div>
                        </div>



                <div class="clr"></div>   
            </div>
         </form>
phpFreak
  • 103
  • 1
  • 1
  • 7
  • Show your code, just need to show the file form tmp as you said, so there must be something wrong with your code – Sal00m Aug 29 '14 at 09:01
  • tmp_name (as whole $_FILES) you have AFTER upload, not before. – Nebojsa Susic Aug 29 '14 at 09:04
  • Check http://khantmontu.blogspot.in/2012/08/display-image-preview-before-it-upload.html and http://stackoverflow.com/questions/16207575/how-to-preview-a-image-before-and-after-upload – Jenz Aug 29 '14 at 09:05
  • Maybe this link can be help you and others for the future. http://stackoverflow.com/questions/14069421/show-an-image-preview-before-upload – Ridwan Pujakesuma May 16 '16 at 14:33
  • You can see the solution of your problem here using js: [Preview an image before it is uploaded](https://stackoverflow.com/questions/4459379/preview-an-image-before-it-is-uploaded) As you can see in that thread there are different solutions for IE and FF/Chrome. So take care of this as well. – WhiteAngel Aug 29 '14 at 09:06
  • Try this jQuery Method https://stackoverflow.com/a/62218223/11766145 OR javaScript https://stackoverflow.com/a/62167551/11766145 – Merrin K Oct 01 '20 at 14:59

6 Answers6

13

simple solution - grab your image data convert to base64 and output in your current view, so you don't need to copy the current tmp image into an public loction- like

$imageData = file_get_contents($_FILES['image']['tmp_name']); // path to file like /var/tmp/...

// display in view
echo sprintf('<img src="data:image/png;base64,%s" />', base64_encode($imageData));
ins0
  • 3,880
  • 1
  • 18
  • 28
1

After the image is submitted, it's already uploaded to the server in a temporary file. If you need to show the contents of the image from that temporary file, then you need to do a script that reads that file and outputs it with the right headers. Here's an example:

header('Content-Type: image/x-png');
readfile($_FILES['name']['tmp_name']);
exit();
alez007
  • 301
  • 1
  • 6
1

any files uploaded to the server if not moved will be deleted automatically. but do not worry. before uploading, you can display it using javascript or jQuery.

<form>
 <input type="file" accept="image/*" name="" onchange="previewImage()">
 <img id="preview">
</form>

<script>
 function previewImage(){
  var previewBox = document.getElementById("preview");
  previewBox.src = URL.createObjectURL(event.target.files[0]);
 }
</script>
0

Have you tried reading the file directly from the /tmp folder? Of course this can only happen after the upload, not before. So you have to do this before moving the file to its destinated folder, but right after the upload.

readfile($_FILES["name"]["tmp_name"]);
JoelP
  • 399
  • 1
  • 2
  • 12
  • $_FILES["name"]["tmp_name"] already has the full path, adding another /tmp/ before it will not show anything, the temporary directory is config based – alez007 Aug 29 '14 at 09:06
0

You have two ways of doing this.

The first is using a javascript code to show the preview before uploading to server, you can check this answer: Image Upload with Preview and Delete

The second one is to upload the image and retrieve a thumbnail from server to show in clientside, you can check this post: http://www.sitepoint.com/image-upload-example/

Community
  • 1
  • 1
Aboca
  • 545
  • 2
  • 9
0

Yes, it is possible to show the image in the tmp folder. Add an onchange event for input type = file; which triggers a function and it loads the image.

!!! Don't forget to set width for img tag

function preview() {
 $('#frame').attr('src',URL.createObjectURL(event.target.files[0]));
}
<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
  <form>
     <input type="file" onchange="preview()">
     <img src="" width="100px" height="100px" id="frame">
  </form>   
</body>
Merrin K
  • 792
  • 1
  • 7
  • 17