PHP Question

Discussion in 'Technology' started by Phil Mitchell, Jul 30, 2006.

Users Viewing Thread (Users: 0, Guests: 0)

  1. Phil Mitchell

    Phil Mitchell check me a dollar brer?

    Joined:
    May 19, 2005
    Messages:
    8,965
    Likes Received:
    1
    Location:
    Melbourne
    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
  2. 1615634792921.png
  3. Wor Jackie

    Wor Jackie Smackheed

    Joined:
    Jan 31, 2006
    Messages:
    1,193
    Likes Received:
    0
    Location:
    On the game.
    Re: PHP Question

    :confused: i havent got a scooby:dunce:
  4. Jason Bourne

    Jason Bourne Registered User

    Joined:
    Oct 14, 2002
    Messages:
    5,337
    Likes Received:
    0
    Re: PHP Question

    Is this so you can record what has been downloaded?
    I'm finding it hard to understand exactly what your doing..
  5. Phil Mitchell

    Phil Mitchell check me a dollar brer?

    Joined:
    May 19, 2005
    Messages:
    8,965
    Likes Received:
    1
    Location:
    Melbourne
    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
  6. Phil Mitchell

    Phil Mitchell check me a dollar brer?

    Joined:
    May 19, 2005
    Messages:
    8,965
    Likes Received:
    1
    Location:
    Melbourne
    elp me Mr Nice, your my only hope
  7. Jason Bourne

    Jason Bourne Registered User

    Joined:
    Oct 14, 2002
    Messages:
    5,337
    Likes Received:
    0
    Will do fella.. when I work it out :lol:
  8. Jason Bourne

    Jason Bourne Registered User

    Joined:
    Oct 14, 2002
    Messages:
    5,337
    Likes Received:
    0
    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 :lol:
  9. jamesrw

    jamesrw Member

    Joined:
    Aug 22, 2005
    Messages:
    31
    Likes Received:
    0
    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>

  10. Jason Bourne

    Jason Bourne Registered User

    Joined:
    Oct 14, 2002
    Messages:
    5,337
    Likes Received:
    0
    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 :up:
  11. Phil Mitchell

    Phil Mitchell check me a dollar brer?

    Joined:
    May 19, 2005
    Messages:
    8,965
    Likes Received:
    1
    Location:
    Melbourne
    beejesus , looks heavy


    cheers Jamesrw/Chandler, will have a work through it
  12. Rob

    Rob Registered User

    Joined:
    Jun 5, 2006
    Messages:
    7,757
    Likes Received:
    3
    Location:
    Sancho Panza
    Go team PowerText. ;)

Share This Page