PHP Question I want to use a button for users to click on when they want to download a file but i also want to add to an sql database aswell as it stands , i have just got the button downloading the file( see below) <form> <div align="left"> <input type="Submit" onClick="http://domain.com/filename.xtn" value="Get File"> </div> </form> but what would i add to get it to add to the database?? There will be numerous download buttons on the page so I want an a different entry to the database for each button e.g Button 1 add 'File 1' to the db Button 2 adds 'File 2' to the db PHP and SQL only please chears
Re: PHP Question Is this so you can record what has been downloaded? I'm finding it hard to understand exactly what your doing..
Re: Re: PHP Question Yeah sorry that post didnt make any sense I want to add a record to a table which records which file has been downloaded e.g. download file 1 then it adds 'file 1' to the table
tbh.. it looks like it would be pretty difficult. Afaik you can only pass on one variable from that button.. which means its either got to be the file number or the download extension. I think it might be possible to pass the file number to the database and then retieve it automatically on the next page and link to the file.. So it would go - Choose file -> submit -> pass file number -> add file number to database -> retrieve number from last record in the database -> execute download string. Perhaps having a confirmation button or a toc page between the download would help. Although that would then allow the user to cancel the download but you would still be left with a false entry. As for the sql.. it should be as easy as - (this is valid for asp so the required $'s etc need to be added..) <% 'Retrieve the variables from the previous page (form) FN = Request.Form( "Filenumber" ) %> <% 'Open the database connection set conn = createobject("ADODB.Connection") conn.open = "DRIVER={MySQL ODBC 3.51 Driver};"_ & "SERVER=******;"_ & "DATABASE=********;"_ & "UID=******;PWD=*******;" %> <% 'Add new record sqlAddrecord ="INSERT INTO records ( `file number`) VALUES ('" & FN &"')" conn.execute sqlAddrecord %> As I say thats a mixture of asp and php.. purely cos I had it handy and I'm in a rush.. but it might help you a bit.. tbh.. I'm slightly confused now
Are the files local? or remote? I hate inline PHP, although just knocked this up, not tested it - although it should work. onclick of a button sets a hidden field to the file path. if request is a post it will scan post vars for file_0, file_2, etc. if it finds one it will load the path from the array, add it to the database and send it as a download. Code: -- table for files create table file ( id int auto_increment primary key, data blob, filename varchar(255) created int ) PHP: <? // list all the files you want here $files = array ( 0 => "file1.blah", 1 => "file2.blah" ); if ($_SERVER["REQUEST_METHOD"] == "POST"){ foreach ($_POST as $key => $value){ if (preg_match ("/^file_(.+)$/", $key, $matches)){ mysql_connect ('host', 'user', 'pass'); mysql_select_db ('whatever'); $data = file_get_contents ($files[$matches[1]]); mysql_query (" insert into file set data = '" . mysql_escape_string ($data) . "', filename = '" . $files[$matches[1]] . "', created = '" . mktime () . "';"); // download file header ("Content-disposition: attachment;"); echo $data; // redirect back to self header ("Location: index.php"); } } } ?> <html> <script language="javascript"> function set_filepath (path){ document.getElementById ('filepath').value = path; } function submit_form (){ document.forms[0].submit (); } </script> <body> <form method="post"> <input type="hidden" name="filepath"> <? foreach ($files as $key => $value){ printf ('<input type="button" onclick="set_filepath(\'file_%s\'); submit_form(); " value="file %s"><br />', $key, $value); } ?> </form> </body> </html>
This guy is better than me.. I was gonna type loads about hidden fields till I saw his post.. His code is far better constructed.. use his