2

I am trying to achieve two actions from an input file tag. I have the following input:

<input id='file-input' name='attach' type='file' style='margin-left:15px;'/> 

This is found in messages.php. What I am trying to achieve are two things:

  1. If the file uploaded is over 1mb in size (or is a file which is not an image), then produce a button which on click opens the save as menu, from where a user can select where they wish to download the data.
  2. And secondly, as mentioned, if the file size is lower than 1mb, then simply display the data on the page (only works for image files).

I have other pages where I have used input type="file" to upload profile images, and have just displayed the image on the page. But I am unsure on how I can execute (1) - how I can open a menu from where the user can save the data?

Freddy
  • 683
  • 4
  • 35
  • 114
  • I have made you a solution, but mssing one detail to finish it. when user upload, than I guess he upload to server, now why you should save as the file if you are uploading, you should not give option where user should save files on server? Or do I have misunderstand you? – Maytham Fahmi Apr 01 '16 at 20:02
  • Or I can also understand it if the file below 1Mb so the user can only view the file, and if the file is more than 1Mb than the user can upload the file to the server? is that correct? – Maytham Fahmi Apr 01 '16 at 20:07
  • @maytham-ɯɐɥʇʎɐɯ - No sorry, I should have been more clear. So assume I send an attachment to you, I will select an image for example, and click send, which will send the image to you as a button (i.e. if I send an attachment to you, this is what you will see http://images.phpgang.com/2012/08/download.jpg). Then, when the button is clicked, the image, can be saved anywhere on your personal computer. I don't want anything stored on the server. – Freddy Apr 01 '16 at 21:48
  • OK and if the File is less than 1 MB view it otherwise download It and Save it some where on Local disk – Maytham Fahmi Apr 01 '16 at 21:52
  • Hmm, Iv been thinking, regardless of its size, I always want to prompt the user to save it on local disk. I believe its a more easier solution? – Freddy Apr 01 '16 at 21:56
  • ok I am putting a solution for you, will come with it soon ;) – Maytham Fahmi Apr 01 '16 at 21:59
  • Thank you, appreciate it :) – Freddy Apr 01 '16 at 22:28

4 Answers4

2

Just serve the file with a Content-Disposition: attachment header, see PHP Outputting File Attachments with Headers

Community
  • 1
  • 1
Creynders
  • 4,573
  • 20
  • 21
  • don't forget the `content-length` header it's really annoying to download a file and not know how big it's going to be. – Barkermn01 Apr 06 '16 at 10:10
1

You won't need to fake a click or something. You probably need something like this.

  1. User selects file and clicks the "Upload file" button.
  2. File gets uploaded using e.g. PHP.
  3. PHP displays the contents of the file, using the correct headers.
  4. PHP determines if the file is smaller or larger than 1MB.
    1. If the file is larger, set a header that forces the user to download the file (causing a select location popup).
    2. If the file is smaller, do not set the header, causing the file to display in the browser.

Where Italic is a user action and Bold is a server action.

Community
  • 1
  • 1
Tijme
  • 39
  • 2
  • 24
  • 41
1

Among other nice answers I will do it this way.

I have made it possible by clicking on your suggested Download button, if file is greater than 1Mb you will get download pop up like this enter image description here

Otherwise if file less than 1Mb it will just show it on browser will look like this: enter image description here

Btw, the code is self explaining.

PHP part called index.php

<?php
$filename = "test3.jpg";
$maxSize = 1000000; // 1Mb

if (isset($_POST['save']))
    fileHandler($filename, $maxSize);

function fileHandler($filename, $maxSize)
{
    $fileinfo = getimagesize($filename);
    $filesize = filesize($filename);
    $fp = fopen($filename, "rb");
    if ($fileinfo && $fp)
    {
        header("Content-Type: {$fileinfo['mime']}");
        if ($filesize > $maxSize)
        {
            header('Content-Disposition: attachment; filename="NewName.jpg"');
        }
        fpassthru($fp);
        exit;
    } else
    {
        echo "Error! please contact administrator";
    }
}

?>

HTML part inside index.php but it is important this code should comes after php tags and not before.

<form action="index.php" method="post">
    <button type="submit" style="border: 0; background: transparent" name="save">
        <img src="download.jpg" alt="submit" />
    </button>
</form>

Note: it is important your php document start directly with <?php ..., please read

Community
  • 1
  • 1
Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
  • Sorry I haven't reviewed your answer yet. I am currently struggling with an issue related to the attachment file (See: http://stackoverflow.com/questions/36389726/if-no-attachment-then-send-message-otherwise-portray-download-button), which needs to be fixed before I can implement your solution. Hopefully I will fix soon, and can test your solution :) – Freddy Apr 04 '16 at 22:49
  • I have fixed your question, let me know, your other question give more meaning to understand this question, so fill free to let me know if the answer here need to be updated – Maytham Fahmi Apr 05 '16 at 03:53
  • Hi again Maytham! I have some good news, so after yesterday, I figured out how to get the size of the image, and it now downloads the image! But .. the image does not display when opened. Can you join this chat room: http://chat.stackoverflow.com/rooms/108446/attachment-images - Just the final hurdle left! – Freddy Apr 06 '16 at 21:06
0

You can do this by getting the image mime type and setting the content disposition and content type headers:

$file = 'path/to/file';

    if (file_exists($file)) {

            $contents = file_get_contents($file);
            $fileSize = filesize($file);
            $image_info = getImageSize($file);
            $mimeType = $image_info['mime'];
            header("content-disposition: attachment; filename=" . basename($file));
            header("content-type:$mimeType");
            header("Content-length: $fileSize");
            echo $contents;
            exit;
    }
andrew
  • 9,313
  • 7
  • 30
  • 61