0

I want a user can only upload DOC and XLS file. But if the file is an image, the image file is displayed in the web page (upload.php) and then deleted automatically from the server.

Here is my piece code:

if (file_exists("uploads/" . $_FILES["file"]["name"])) {
    echo "<br>File already exists!.<br>";
    } else {
      move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $_FILES["file"]["name"]);
      if (($_FILES["file"]["type"] == "image/gif" )
      ||($_FILES["file"]["type"] == "image/jpeg" )
      ||($_FILES["file"]["type"] == "image/jpg" )
      ||($_FILES["file"]["type"] == "image/png" )
      && in_array($extension, $allowedExts)){
        echo '<br><br><img src="uploads/'.rawurlencode($_FILES["file"]["name"]).'" style="height:30%;"><br>';
        } else {
          echo '<br><br><a href="uploads/'.rawurlencode($_FILES["file"]["name"]).'"><p><strong>Download File</strong></p></a><br>';
          }
          }
Odie Ardhia
  • 15
  • 1
  • 9

1 Answers1

0

You may write a handler for such image.

echo '<br><br><img src="handle.php?imagename='.rawurlencode($_FILES["file"]["name"]).'" style="height:30%;"><br>';

And this is how you write the handle.php

<?php
header('Content-Type: image/jpeg');
$src = "image path goes here".$_GET['imagename']; 
readfile($src);
unlink("image path goes here".$_GET['imagename']);
?>
Odie Ardhia
  • 15
  • 1
  • 9
Samay
  • 465
  • 6
  • 19
  • I mean, if the file is DOC and XLS file, will perform the option to download the file. – Odie Ardhia Nov 05 '16 at 09:59
  • Yes. You can also force download using PHP. Ref: [http://stackoverflow.com/questions/7263923/how-to-force-file-download-with-php](http://stackoverflow.com/questions/7263923/how-to-force-file-download-with-php) – Samay Nov 05 '16 at 10:00
  • I got confused, but your advice works well. Thanks for your help! – Odie Ardhia Nov 05 '16 at 10:57