Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I write a php script that downloads one of several songs on a page?
    primarykey
    data
    text
    <p>I have a page with songs on it and a download button next to each song:</p> <pre><code>&lt;table&gt; &lt;tr&gt; &lt;td&gt;Off the Grid&lt;/td&gt; &lt;td&gt;&lt;a href="http://example.com/php/download.php?Off_the_Grid.mp3"&gt;Download&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Far-Sighted&lt;/td&gt; &lt;td&gt;&lt;a href="http://example.com/php/download.php?Far_Sighted.mp3"&gt;Download&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre> <p>Then I have the following script that forces a download:</p> <pre><code>&lt;?php $audioFile = "Off_the_Grid.mp3"; $fileName = $_GET['$audioFile']; // Fetch the file info. $filePath = '../audio/' . $audioFile; if(file_exists($filePath)) { $fileName = basename($filePath); $fileSize = filesize($filePath); // Output headers. header("Cache-Control: private"); header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3"); header("Content-Length: ".$fileSize); header("Content-Disposition: attachment; filename=".$fileName); // Output file. readfile ($filePath); exit(); } else { die('The provided file path is not valid.'); } ?&gt; </code></pre> <p>My goal is to turn this script into a function such that when the download link of a song is clicked -- e.g., "Far-Sighted" -- "Far-Sighted" and only "Far-Sighted" is forced to download. And if instead the "Off the Grid" download link is clicked, the function will run for "Off the Grid". And etc. for all the songs I post.</p> <p>The above code works insofar as it forces the download of "Off the Grid". But, I'm trying to get it to force the download for any song that is clicked. Right now, if I clicked on "Download" next to "Far-Sighted" then according to my posted code, "Off the Grid" would download and "Far-Sighted" would not download. Obviously, "Far-Sighted" should download; however, I'm stuck :-/ </p> <p>Is there a way to do this cleanly? I've been trying to think of a way to do this to no concise avail. Is there a way to make it so that my variable "$audiofile" changes its value depending on what song download link is clicked? I was going to resort to creating php files for each song and just post the script unique to the song there, or just one php file with several instances of the script tailored to each song. But that seemed ridiculous.</p> <p>Is there a nice way to make this happen? Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload