How to code HTML download button where filename version will change |
How to code HTML download button where filename version will change |
larry78723 |
Mar 15 2020, 08:26 AM
Post
#1
|
Member Group: Members Posts: 39 Joined: 5-March 20 Member No.: 27,220 |
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 |
Mar 15 2020, 04:51 PM
Post
#2
|
Member Group: Members Posts: 39 Joined: 5-March 20 Member No.: 27,220 |
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> This post has been edited by larry78723: Mar 15 2020, 04:53 PM |
CharlesEF |
Mar 15 2020, 05:49 PM
Post
#3
|
Programming Fanatic Group: Members Posts: 1,984 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
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 |
Mar 15 2020, 06:10 PM
Post
#4
|
Programming Fanatic Group: Members Posts: 1,984 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
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 |
Mar 15 2020, 06:15 PM
Post
#5
|
Member Group: Members Posts: 39 Joined: 5-March 20 Member No.: 27,220 |
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 |
Mar 15 2020, 06:26 PM
Post
#6
|
Programming Fanatic Group: Members Posts: 1,984 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
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 |
Mar 15 2020, 06:58 PM
Post
#7
|
. Group: WDG Moderators Posts: 9,722 Joined: 10-August 06 Member No.: 7 |
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 |
Mar 15 2020, 07:05 PM
Post
#8
|
. Group: WDG Moderators Posts: 9,722 Joined: 10-August 06 Member No.: 7 |
$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 |
Mar 15 2020, 08:42 PM
Post
#9
|
Programming Fanatic Group: Members Posts: 1,984 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
Here is how it should be done, according to me.
CODE $path = '/tmp'; You still need to sort $files and pick the last or first entry (depending on sort order).// keep '.'.and '..' entries $files = scandir($path); // remove '.'.and '..' entries // This leaves $files as only actual files $files = array_diff(scandir($path), array('.', '..')); OR, you could use glob. CODE foreach(glob($download_directory.'/*') as $file) { ... } OR, you can use this free script: here This post has been edited by CharlesEF: Mar 15 2020, 08:53 PM |
CharlesEF |
Mar 15 2020, 10:16 PM
Post
#10
|
Programming Fanatic Group: Members Posts: 1,984 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
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> This post has been edited by CharlesEF: Mar 15 2020, 10:22 PM |
larry78723 |
Mar 15 2020, 11:30 PM
Post
#11
|
Member Group: Members Posts: 39 Joined: 5-March 20 Member No.: 27,220 |
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 |
Mar 16 2020, 01:55 AM
Post
#12
|
Programming Fanatic Group: Members Posts: 1,984 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
$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.$file2 = "window.location.href = 'https://foxclone.com/'".$files[0]; This will never work. <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 |
Mar 16 2020, 05:38 AM
Post
#13
|
. Group: WDG Moderators Posts: 9,722 Joined: 10-August 06 Member No.: 7 |
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 |
Mar 16 2020, 06:25 AM
Post
#14
|
Member Group: Members Posts: 39 Joined: 5-March 20 Member No.: 27,220 |
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? This post has been edited by larry78723: Mar 16 2020, 06:35 AM |
larry78723 |
Mar 16 2020, 08:15 AM
Post
#15
|
Member Group: Members Posts: 39 Joined: 5-March 20 Member No.: 27,220 |
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. This post has been edited by larry78723: Mar 16 2020, 08:19 AM |
larry78723 |
Mar 16 2020, 08:27 AM
Post
#16
|
Member Group: Members Posts: 39 Joined: 5-March 20 Member No.: 27,220 |
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 |
Mar 16 2020, 09:20 AM
Post
#17
|
Programming Fanatic Group: Members Posts: 1,984 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
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 |
Mar 16 2020, 12:34 PM
Post
#18
|
. Group: WDG Moderators Posts: 9,722 Joined: 10-August 06 Member No.: 7 |
|
larry78723 |
Mar 16 2020, 07:00 PM
Post
#19
|
Member Group: Members Posts: 39 Joined: 5-March 20 Member No.: 27,220 |
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 This post has been edited by larry78723: Mar 16 2020, 07:16 PM |
CharlesEF |
Mar 16 2020, 10:35 PM
Post
#20
|
Programming Fanatic Group: Members Posts: 1,984 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
Thanks to both of you. I guess my question becomes how do I insert the $file2 from 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 <?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? 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. |
Lo-Fi Version | Time is now: 26th September 2024 - 10:26 AM |