How to generate filename?

Discussion in 'PHP' started by mikelbeck, Oct 25, 2005.

  1. #1
    I have a bunch of data in a database... I have a script that pulls the data from the database to display, and the URL of the final piece of the script looks like this:

    script.php?mode=get_data&data_id=1234

    One of the items in the database is "item-description", and what I'd like is to be able to pull the data out of the database and have the final URL look like this:

    item-description.html

    Does this make sense to anybody? How would I do something like this? Can I do it from within my script or do I have to get involved with mod_rewrite?
     
    mikelbeck, Oct 25, 2005 IP
  2. briandunning

    briandunning Active Member

    Messages:
    262
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    98
    #2
    No problem. Here is a function I use to generate a page name from a text description (this assumes that your description is in a variable called $name):
    function gen_filename($name)
    {
    	$filename = strtolower(trim($name));
    	$filename = str_replace(' ', '-', $filename);
    	$filename = preg_replace('/[^a-zA-Z0-9\-]/','', $filename);
    	$filename = preg_replace('/-+/','-', $filename);
    	$filename = trim(substr($filename, 0, 30), '-');
    	return $filename;
    }
    Code (markup):
    Note that by itself, this-is-a-filename.html is not good enough, because it's probably not unique, so I always append a unique identifier to it so that my final filename looks like this:

    /this-is-some-text.12345.html
    Code (markup):
    Then you use mod_rewrite in htaccess to convert that incoming filename to what your script actually wants, here's how I do it:

    RewriteRule ^(.*)\.(.*)\.html$ /detail.php?id=$2 [L]
    Code (markup):
    Hope that helps. :)
     
    briandunning, Oct 25, 2005 IP
  3. mikelbeck

    mikelbeck Well-Known Member

    Messages:
    790
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    108
    #3
    Thanks...

    However, I already have a routine that generates a unique filename. I think my real question is how do I actually output the data so that the user would see that filename ("this-is-some-text.12345.html") and not what the script wants to see ("detail.php?id=12345") ?
     
    mikelbeck, Oct 25, 2005 IP
  4. briandunning

    briandunning Active Member

    Messages:
    262
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    98
    #4
    I guess I don't understand your question. You're asking how to output the filename, and you're saying you already have a routine to output the filename. We're miscommunicating somewhere? :)
     
    briandunning, Oct 25, 2005 IP
  5. mikelbeck

    mikelbeck Well-Known Member

    Messages:
    790
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    108
    #5
    Yeah, I think we might be. Could be because I don't know how to explain myself. ;-)

    I have a script that gets the data... The script is drinks.php. I have a routine that generates a unique filename, that comes up with "drink-name.html". What do I need to do in my script so that when the user is on that page (drinks.php) and clicks on a link that generates that unique filename and gathers the data, the user is then sent to a page that displays that data and uses the unique filename?

    Here, maybe a visual would make more sense, it always does to me. ;-)

    This page: http://www.happyhourpub.com/forum/drinks.php?mode=show_drink&drinkid=4467 shows the ingredients and instructions for making a "Crooked Monkey". Instead of this page being called "http://www.happyhourpub.com/forum/drinks.php?mode=show_drink&drinkid=4467", I'd like it to be called "crooked_monkey.html". The user would actually be on a page like this when they click the link for that particular drink: http://www.happyhourpub.com/forum/drinks.php?mode=show_glass&glass=Beer mug

    Does that make more sense?
     
    mikelbeck, Oct 25, 2005 IP
  6. briandunning

    briandunning Active Member

    Messages:
    262
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    98
    #6
    The long address is always going to work. If you want the short address to be displayed, you have to link to the short address instead of the long one.
     
    briandunning, Oct 25, 2005 IP
  7. mikelbeck

    mikelbeck Well-Known Member

    Messages:
    790
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    108
    #7
    Ah, ok, I've got it now. Thanks for the help!
     
    mikelbeck, Oct 25, 2005 IP