How can i modify my File download link ?

Funky Guy
Funky Guy used Ask the Experts™
on
IN my Php Website..
i want to modify my file download link,when ever any one click on it,using google chrome or Microsoft Edage The Downloading Link Starts Playing...if my  Download link contains audio or video,when ever any one press on it...audio or video starts Running in browser..
i want modification in it...that whenever someone clicks on that ....File Starts Downloading Instead of Running Or Playing ..
Please help..
Thank u in Advance ~!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Couple of ways of doing this. The simplest (but not complete) solution is to use the HTML5 download attribute on your link:

<a href="path/to/file" download>Click here to download</a>

You can read about here - it's not supported by every browser but, as per your question, it is supported by Chrome and Edge

If this isn't enough, then you'd need to manually modify your header files, which you can do in PHP. You'd need to create a PHP script to handle the download, so you link would point to that file rather than the actual file.
Most Valuable Expert 2011
Top Expert 2016

Commented:
I think if this were my app, I would put two client controls on the page.  One of them would download the file.  The other would play the audio or video.

Here is how you can force the download of a file from your server.
<?php // demo/force_download.php
/**
 * Show how to force a file download
 *
 * Choose a file to download, either via a hard-coded file path
 * or via a URL parameter like this
 *
 * path/to/demo/force_download.php?url=path/to/file.txt
 *
 * If you wanted to use the URL parameter, you might want to
 * (1) Secure the script with password protection
 * (2) replace the $url variable with something like this:
 * $url = $_GET['url'];
 *
 * If you use the URL parameter, be careful that the script
 * does not expose sensitive data on your server!
 */
error_reporting(E_ALL);


// WHAT FILE DO WE WANT TO DOWNLOAD?
$url = "http://www.IcoNoun.com/demo/short_text_file.txt";


// USE CASE
force_download($url);


// FUNCTION TO FORCE A DOWNLOAD FROM A FILE
function force_download($url, $filename=NULL)
{
    // GET THE DOWNLOAD FILE NAME
    if (empty($filename)) $filename = basename($url);

    // GET LENGTH AND FILE RESOURCE POINTER
    $hdr = get_headers($url, TRUE);
    $len = trim($hdr['Content-Length']);
    $fpr = fopen($url,'rb');

    // ON SUCCESS
    if ($fpr)
    {
        // THESE HEADERS ARE USED ON ALL BROWSERS
        header("Content-Type: application-x/force-download");
        header("Content-Disposition: attachment; filename=$filename");
        header("Content-length: $len");
        header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
        header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");

        // THIS HEADER MUST BE OMITTED FOR IE 6+
        if (FALSE === strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE '))
        {
            header("Cache-Control: no-cache, must-revalidate");
        }

        // THIS IS THE LAST HEADER
        header("Pragma: no-cache");

        // FLUSH THE HEADERS TO THE BROWSER
        flush();

        // WRITE THE FILE
        fpassthru($fpr);
    }

    // ERROR
    else
    {
        trigger_error("Unable to open $url", E_USER_ERROR);
    }
}

Open in new window

Author

Commented:
sir,
chrish Stanyon
My Webdeveloper has written this code
<a rel="nofollow" href="/files/download/id/<?=$getfile['id']?>/<?=$getfile['name']?>.html"><font color="#F01030" size="4"><b>[REAL DOWNLOAD LINK]</b></font></a>
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
sir,
Ray paseur
Please tell,
where should i use your code??
Most Valuable Expert 2011
Top Expert 2016
Commented:
Install it as a standalone script on your server.  Plug in the name of the file you want to download, and run it to see how it works.  Or just test it on my server, here:
https://iconoun.com/demo/force_download.php
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
The code your developer has written doesn't really make sense on it's own. By the look of that, you're basically linking to HTML files which will display by default rather than download - and if you forced a download, the user would probably get an HTML file. There must be something else going on that identifies that the HTML page to be delivered is in fact a Music or Movie file, and not a standard HTML file.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial