Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2002
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    creating download link in php

    Hi all. i have a quick (and possibly very stupid) question, and was wondering if anyone could help me with this.

    In the php script, i have it accessing a mysql database. The database has the name of the file, and the format it is in (jpg, igf, doc, pdf, etc).

    The link i want to create would use the file name, and the suffix.

    How can i get the script to put in the title and suffix from the database?

    example:
    title: doc1
    format: doc
    preferred link: ahref="title.format"

    any suggestions?

    thanks all,

    dan
    dan

  • #2
    Regular Coder
    Join Date
    May 2002
    Location
    Virginia, USA
    Posts
    621
    Thanks
    0
    Thanked 6 Times in 6 Posts
    Looks like you've just about answered your own question.

    Supposing you have a mysql database called "db_files", and table called "tbl_files" and....you have the files you placed below, let's say your database has a row with the following info:

    item_number: 1
    item_title: my_document
    item_extension: doc


    PHP Code:
    <?php

    /* Standard DB Connect */
    $dbName     "db_files";        // the database name
    $tableName "tbl_files";        // the table name
    $dbServer    "localhost";      // the address of your server
    $dbServerPort "3306";         // the database server port

    $dbUserName "my_username";
    $dbPassword  "my_password";

    $serverConnection mysql_connect($dbServer ":" $dbServerPort$dbUserName$dbPassword)
           or die (
    'Unable to connect to server: ' mysql_error());

    $dbConnection mysql_select_db($dbName,$serverConnection);



    /* Query the table in the database, and generate the link */

    $sql_query "SELECT * FROM '$tableName' WHERE item_number = '1' ";

    $result mysql_query($sql_query);

    while (
    $row mysql_fetch_assoc($result)) {

        
    $fileWithExtension $row["item_title"] . "." $row["item_extension"];

        
    $theLink "<a href=\"" $fileWithExtension "\">" $fileWithExtension "</a>";

    }

    /* print out the generated link */
    print $theLink;

    ?>
    That's really sloppy code, and i'm just typing it off the top of my head, so I make no guarantees, but it *should* work... The output of that would be:


    a link, pointing to mydocument.doc, with the link text (what you'd click on), being the word 'mydocument.doc'


    ***************************

    Things to consider:
    - If you store file names with spaces or special characters, like "this ! is my Documen&t", then you need to rawurlencode the data before you either print it, or store it in the database

    -You need to know what link you're pulling. If you want to pull all items out of the database, then place "print $theLink;" before the curly brace "}" above it. Also change "$sql_query" to:
    PHP Code:
    $sql_query "SELECT * FROM '$tblName'"
    That should select EVERYTHING in that table.

    Too much info? not enough info? need it simpler than that? More complex? what are you looking for? Do you need the code so it isn't so easily updatable? ie, would you like for me to shrink it down to 4 or 5 lines of code? (I can, but it'll be harder to read..well...maybe )

    -Celt

  • #3
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,084
    Thanks
    11
    Thanked 100 Times in 98 Posts
    Originally posted by Celtboy ie, would you like for me to shrink it down to 4 or 5 lines of code? (I can, but it'll be harder to read..well...maybe )

    -Celt [/B]

    cough cough
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Sheffield, UK
    Posts
    552
    Thanks
    0
    Thanked 0 Times in 0 Posts
    how about doing
    Code:
    $filename = "href=\".mysql_result(mysql_query("SELECT CONCAT(title,'. ',format) FROM tblFiles WHERE ID = 1"),0)."\"";
    this will work if you already have a mySQL conection open, and filename is the docname field with format being the extension field and ID as the unique identifer. change tblFiles to whatever your table is called et viola.

    I can work this into a proper script if you want, basically the idea is if you only want the link, you can just select one field and use the DB to concatenate your strings.
    "To be successful in IT you don't need to know everything - just where to find it in under 30 seconds"

    (Me Me Me Me Me Me Me Me Me)

  • #5
    New Coder
    Join Date
    Jun 2002
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thank you

    thank you all for the help. I was able to get this figured out, plus some.

    If you would like the answer, here it is.

    my items in the table were title, category, and format. these were also the names in the php script to get the information to/from the table.

    PHP Code:
    <snip>
    $myrow["title"], $myrow["category"], $myrow["format"]

    printf("<a href=\"%s/%s.%s\">link_text</a>"$myrow["category"], $myrow["title"], $myrow["format"]);
    </
    snip
    In the example above, i created folders pertaining to the different categories used (pictures, family, recipes, etc). i then used the $category as the folder name in the link (the first %s). the other two are kind of self-explanatory after that.

    Thanks again for the help. yes, i was right there, but i guess i needed some time away from it to look at it anew.
    dan


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •