Rename File on Download

Discussion in 'PHP' started by cesarcesar, May 3, 2007.

  1. #1
    I have a php script that uploads a file to a directory. Since this program doesn't like spaces and crazy characters in the file name, the name gets changed to the current timecode (funky named-4.jpg to 1178253719.jpg). After the change i save both names to a db. This all works great. Next i have a page that reads the directory and displays the timecoded file name, when a user clicks it, it downloads fine. still works great.

    I would like to find a way to rename the file on download, to the old saved named of the file (1178253719.jpg to funky named-4.jpg). How can i do this? Thanks.
     
    cesarcesar, May 3, 2007 IP
  2. designcode

    designcode Well-Known Member

    Messages:
    738
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #2
    Following peice of code will work for you

    
    header('Content-type: image/jpeg');
    header('Content-Disposition: attachment; filename="newname.jpg"');
    readfile("originalname.jpg");
    
    PHP:
    Hope this helps.
     
    designcode, May 3, 2007 IP
  3. Spikeon

    Spikeon Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    wow...i gotta write that down... thanks for the hint
     
    Spikeon, May 3, 2007 IP
  4. cesarcesar

    cesarcesar Peon

    Messages:
    188
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    cesarcesar, May 3, 2007 IP
  5. designcode

    designcode Well-Known Member

    Messages:
    738
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #5
    I am glad, you got the fix ;)
     
    designcode, May 3, 2007 IP