Help - Search - Members - Calendar
Full Version: How to code HTML download button where filename version will change
HTMLHelp Forums > Programming > Server-side Scripting
larry78723
I'm creating a webpage where the user will be able to download an .iso file. The version of the iso will change. For example: xxxx.1.iso will become xxxx.2.iso when the iso is updated. How do I code the download button so that I don't need to re-code the button when the version changes. If I were to create a download subdirectory, and the .iso file was kept there do you think this would work if the only file in the download subdirectory is the .iso file?

<?php
$files = array_slice(scandir('/download/'), 2);
?>
<div class="navbar-custom">
<a class="btn-solid-lg page-scroll" href="/Download/" download=$files>Download the App</a>
</div>

larry78723
I've tried the following method but it immediately downloads index.html and the site never loads in the browser.

<?php
$files = array();
foreach (glob("/download/*.iso") as $file) {
$files[1] = $file;
}
$file2 = "window.location.href = 'https://foxclone.com/download/' + '$file'"
?>

<div class="navbar-custom">
<button onclick="$file2" >Download the App</button>
CharlesEF
Maybe I don't understand correctly, but I think you are over thinking the problem. I would code the button to always download 'xxxx.iso'. When a new version comes out just rename 'xxxx.iso' to 'xxxx.1.iso', and so on. This way the download name will always be 'xxxx.iso'.
CharlesEF
If you are using a web server you could use server-side code (like PHP) to scan the download directory and write the last filename found into the button.
larry78723
QUOTE(CharlesEF @ Mar 15 2020, 07:10 PM) *

If you are using a web server you could use server-side code (like PHP) to scan the download directory and write the last filename found into the button.


CharlesEF, that's what I've been trying to do with the code in my first two posts. Neither one is working.

Thanks for the response,
Larry
CharlesEF
What language are you using? I'll use PHP to show an example:
CODE
<button onclick="<?php echo($file2);?>" >Download the App</button>
. If you post your actual script that scans the download directory then I might be of more help.
Christian J
QUOTE(CharlesEF @ Mar 16 2020, 12:10 AM) *

If you are using a web server you could use server-side code (like PHP) to scan the download directory and write the last filename found into the button.

Just be careful about relying on the server's "last modified" meta data when sorting the files. If the webhost one day decides to install new server software they may backup and then reinstall all files, resetting all file dates in the process (happened to me once). It's safer to sort the files after file name, at least if new files are named correctly.
Christian J
QUOTE(larry78723 @ Mar 15 2020, 02:26 PM) *

$files = array_slice(scandir('/download/'), 2);

Haven't done PHP for a long time, but I think the above returns all array items after the second, compare https://www.php.net/manual/en/function.array-slice.php

If you just want the third item, maybe you could combine the above code with $files[0] ?
CharlesEF
Here is how it should be done, according to me. biggrin.gif
CODE
$path    = '/tmp';
// keep '.'.and '..' entries
$files = scandir($path);
// remove '.'.and '..' entries
// This leaves $files as only actual files
$files = array_diff(scandir($path), array('.', '..'));
You still need to sort $files and pick the last or first entry (depending on sort order).

OR, you could use glob.
CODE
foreach(glob($download_directory.'/*') as $file) {
    ...
}


OR, you can use this free script: here
CharlesEF
Here is a sample of what you want to do. I've never seen a button onclick download a file but that doesn't mean it won't work.
CODE
<?php
$path = "temp";
// The 2nd parameter, 1, in scandir means to sort in decending order
$files = array_diff(scandir($path, 1), array(".", ".."));
$file = $files[0];
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
  <title>Scan Dir, Pick Last File</title>
</head>
<body>
<button onclick="<?php echo("/{$path}/" . $file);?>">Download the App</button>
</body>
</html>


Which produces this HTML:
CODE
<!DOCTYPE HTML>
<html lang="en">
<head>
  <title>Scan Dir, Pick Last File</title>
</head>
<body>
<button onclick="/temp/name.5.iso">Download the App</button>
</body>
</html>
larry78723
QUOTE(CharlesEF @ Mar 15 2020, 07:26 PM) *

What language are you using? I'll use PHP to show an example:
CODE
<button onclick="<?php echo($file2);?>" >Download the App</button>
. If you post your actual script that scans the download directory then I might be of more help.


Charles, thanks for getting back to me. I re-wrote the block of code like this:

<?php
$files = array();
$files = glob('download/*.iso');
$file2 = "window.location.href = 'https://foxclone.com/'".$files[0];
?>

<div class="navbar-custom">
<button onclick=<?php echo $file2;?>Download the App</button>
</div>

I uploaded a new index.html with the code block as above and it didn't work. My download folder is located in my public_html folder and the iso is in there so it should.

I'm fairly new to web development and learning as I go.

Thanks again,
Larry
CharlesEF
QUOTE(larry78723 @ Mar 15 2020, 11:30 PM) *
$files = glob('download/*.iso');
If you use glob() you have to remember that xxxx.iso will appear after all others. To get the last entry you can do this:
CODE
$file = $files[count($files) - 1];
Then just echo out the $file variable in the HTML.
QUOTE(larry78723 @ Mar 15 2020, 11:30 PM) *
$file2 = "window.location.href = 'https://foxclone.com/'".$files[0];
This will never work.
QUOTE(larry78723 @ Mar 15 2020, 11:30 PM) *
<button onclick=<?php echo $file2;?>Download the App</button>
I still don't think a button will trigger a download. I think you should use the '< a href="xxxx">Download the App< /a>' approach, just remove the space before the a and /.

If you want to use a button then the onclick should call a Javascript function. You would also need to use a 'data-url' attribute to store the file. Once clicked the function will run, be sure to pass 'this' as a parameter. In the function you use 'this' and the getAttribute command to get the 'data-url' attribute value. Then you can assign it to window.location.href, If you need any help with it just post back.
Christian J
QUOTE(CharlesEF @ Mar 16 2020, 07:55 AM) *

I still don't think a button will trigger a download.

True, an onclick event needs to be used with javascript. But with a link you can force a download with the DOWNLOAD attribute: https://davidwalsh.name/download-attribute
larry78723
QUOTE(Christian J @ Mar 16 2020, 06:38 AM) *

True, an onclick event needs to be used with javascript. But with a link you can force a download with the DOWNLOAD attribute: https://davidwalsh.name/download-attribute


Thanks to both of you. I guess my question becomes how do I insert the $file2 from

CODE
               <?php
                    $files = array();
                    $files = glob('download/*.iso');
                    $file = $files[count($files) - 1];
                    $file2 = "window.location.href = 'https://foxclone.com/'".$file;
                  ?>

into

CODE
<button onclick=<a href="?????">Download the App</a></button>


Is it just a matter of replacing "?????" with $file2?
larry78723
Would I be better off trying to use jquery to get the filename instead of php?

Disregard, just realized jquery acts locally, not on the server.
larry78723
QUOTE(CharlesEF @ Mar 16 2020, 02:55 AM) *

If you want to use a button then the onclick should call a Javascript function. You would also need to use a 'data-url' attribute to store the file. Once clicked the function will run, be sure to pass 'this' as a parameter. In the function you use 'this' and the getAttribute command to get the 'data-url' attribute value. Then you can assign it to window.location.href, If you need any help with it just post back.

Charles, I don't understand how to use the getAttribute command to get the value returned from $file.

Thanks again for your help,
Larry
CharlesEF
If you are a beginner then you must understand the difference between server side and client side. PHP is server side, meaning it only runs when the web server is serving the page to the browser. Once the page is done loading then Javascript, client side, takes over. 'getAttribute' is a Javascript command and can only be used client side.

I explained the Javascript side thinking you could do some web searches, I always learn better when I do my own web searches. All PHP can do is place the $file value into the 'data-url' attribute of the button. If you take this route then write some code and post it here, if you need any help.

I still think you can get this to work using an anchor tag '< a >'
Christian J
QUOTE(CharlesEF @ Mar 16 2020, 03:20 PM) *

I still think you can get this to work using an anchor tag '< a >'

Yes there's no point at all in using a form button, and it doesn't let you use the DOWNLOAD attribute either. You can always make a link look like a button with CSS if you want.
larry78723
QUOTE(Christian J @ Mar 16 2020, 01:34 PM) *

QUOTE(CharlesEF @ Mar 16 2020, 03:20 PM) *

I still think you can get this to work using an anchor tag '< a >'

Yes there's no point at all in using a form button, and it doesn't let you use the DOWNLOAD attribute either. You can always make a link look like a button with CSS if you want.


OK guys, you've convinced me about using an anchor tag. Now my question is how do I put the result of a php glob into it. Code follows:
CODE

                 <?php
                    $files = array();
                    $files = glob('download/*.iso');
                    $file2 = "window.location.href = 'https://foxclone.com/'".$files[0];
                  ?>
                <li class="nav-item">
                    <a href="<?php echo $file2;?>" download>DOWNLOAD</a>       <----Doesn't work, get syntax error
                </li>


Thanks in advance
Larry
CharlesEF
QUOTE(larry78723 @ Mar 16 2020, 06:25 AM) *
Thanks to both of you. I guess my question becomes how do I insert the $file2 from

CODE
               <?php
                    $files = array();
                    $files = glob('download/*.iso');
                    $file = $files[count($files) - 1];
                    $file2 = "window.location.href = 'https://foxclone.com/'".$file;
                  ?>

into

CODE
<button onclick=<a href="?????">Download the App</a></button>


Is it just a matter of replacing "?????" with $file2?
I didn't see this post before but here goes. In the PHP code $file2 is useless and should be removed, the entire line, because of the way href works it will NEVER work. You want to insert $file into the href attribute, like this:
CODE
<a href="<?php echo "/{$file}";?>" download>DOWNLOAD</a>
Notice the '/' at the start of the href. That represents the home directory. This means your download directory is located under your public_html directory.

Hope this helps, post back if you have any other questions.
larry78723
QUOTE(CharlesEF @ Mar 16 2020, 11:35 PM) *

I didn't see this post before but here goes. In the PHP code $file2 is useless and should be removed, the entire line, because of the way href works it will NEVER work. You want to insert $file into the href attribute, like this:
CODE
<a href="<?php echo "/{$file}";?>" download>DOWNLOAD</a>
Notice the '/' at the start of the href. That represents the home directory. This means your download directory is located under your public_html directory.

Hope this helps, post back if you have any other questions.

Charles, I need some more help. I've changed to using a button.jpg and I'm getting a syntax error on the button. Here's the code as currently wriiten:
CODE

<?php
  $files = array();
  $files = glob('download/*.iso');
  $file = $files[count($files) - 1];
?>
    
<div id="download" class="filter">
    <div class="container">
       <div class="row">
            <div class="stylized">
                <h1>FoxClone Download Page</h1>
              
                <h3>Get the latest version of FoxClone </h3>
                    <a href="<?php echo "/{$file}";?>" <img src="images/button_get-the-app.png" alt="" width="104" height="40"></a>


I'd also like to know how I can check the members of the array(). I've heard about print_r($myarray) and var_dump($myarray) but have no idea how to insert it into my code so I can verify that the php code is working.

EDIT: I ran the code on my hosts server and I'm getting a 404 error with this information:
"This www.foxclone.com page can’t be found
No webpage was found for the web address: https://www.foxclone.com/<?php%20echo
HTTP ERROR 404"

I get the same error on my apache server. It appears that the "<?php echo "/{$file}";?>" isn't being translated. Is there a way to convert that to a standard variable before using it in the button?

Thanks again for all your help,
Larry
Christian J
QUOTE(larry78723 @ Mar 17 2020, 01:43 PM) *

<a href="<?php echo "/{$file}";?>" <img

The link's start tag is missing its ">" character before the image. Try this instead:

CODE
<a href="<?php echo "/{$file}";?>"><img


QUOTE
I'd also like to know how I can check the members of the array(). I've heard about print_r($myarray) and var_dump($myarray) but have no idea how to insert it into my code so I can verify that the php code is working.

You can just insert it anywhere you want the result printed. It's a good idea to use a PRE element around it for better readability.

QUOTE
It appears that the "<?php echo "/{$file}";?>" isn't being translated.

Does the web page have a .php file extension? Usually, ordinary web pages with .html extensions won't run PHP (though that can be reconfigured).

QUOTE
Is there a way to convert that to a standard variable before using it in the button?

Not sure I understand, $file is a varaibale (never mind those curly characters around it). unsure.gif
CharlesEF
In case you haven't read this before, PHP will convert variables automatically when they are enclosed in double quotes. The curly braces are used to separate the variable from other text, sometimes PHP can't understand a variable name because of the other text. So, to avoid that problem I always use curly braces.

One suggestion, you should stick to 1 thing and get it working before you change to another. This way you can get an understanding of things. Then you can change to another.
larry78723
QUOTE(Christian J @ Mar 17 2020, 12:15 PM) *

Does the web page have a .php file extension? Usually, ordinary web pages with .html extensions won't run PHP (though that can be reconfigured).


Christian, the web page is index.html. I did a 'Save As' index.php. When I opened it in the browser, it immediately downloaded itself but never opened in the browser. I removed all php and download code and tried again with the same result. I'm running a LAMP stack on this machine if that makes a difference. I think this might be a browser related problem. Looking into its' configuration now.

EDIT: It's definitely a browser problem. Any .php file I try to open does exactly the same thing.
CharlesEF
How many browsers did you try it on? You should try it on several browsers, before thinking it's a browser problem. Below is a working example:
CODE
<?php
$files = array();
$files = glob("temp/*.txt");
$file = $files[count($files) - 1];
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Scan Dir, Pick Last File</title>
</head>
<body>
<a href="<?php echo("/{$file}");?>" alt="<?php echo("/{$file}");?>">Download the App</a>
</body>
</html>
I don't have any '.iso' files to test with so I used '.txt'. Be sure to change txt to iso in the PHP code. And, be sure to save the file as '.php'. Since I tested with text files I get the file loaded into the browser.

How does it work for you?
larry78723
QUOTE(CharlesEF @ Mar 17 2020, 03:47 PM) *

How many browsers did you try it on? You should try it on several browsers, before thinking it's a browser problem. Below is a working example:[code]<?php



I don't think it's a browser problem anymore. I've tried on Opera, Chromium, and Firefox and they all act the same. Regardless of what php file I try to open in any of those browsers, the file is immediately downloaded and never displays. I think the problem is either in apache2.conf or phpadmin.conf but I don't know what to look for. I really don't want to purge the entire LAMP stack and do a total re-install.
CharlesEF
Did you try my example code? Change the download folder and extension in the PHP code then save it in a new page, by itself. Be sure to use a .php extension when you save it. Now load the page in a browser. What happens?
Christian J
QUOTE(larry78723 @ Mar 17 2020, 10:32 PM) *

Regardless of what php file I try to open in any of those browsers, the file is immediately downloaded and never displays. I think the problem is either in apache2.conf or phpadmin.conf but I don't know what to look for. I really don't want to purge the entire LAMP stack and do a total re-install.

Maybe the server doesn't send the correct Mime type/Content-Type header ("text/html") along with the web page? But I assume LAMP includes the PHP module, and would then send the correct one.

Try testing it on your web host's server as well.

pandy
My thought also. Or the actual script changes the content-type?

Use some online tool to check what the content type is, for example this one: https://websniffer.cc/ .

Thing is, the content-type must be set to something the browser can't display (hence it offers to download it). If it wasn't set att all it would default to text/plain and the page would be displayed as text in the browser. At least in the best of worlds, that's what would happen.
pandy
Oh, you run this on your local machine? You can use Firefox.
Tools | Page Info
larry78723
Thanks everyone. Got it working on the host server.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2021 Invision Power Services, Inc.