if files exist show link ?

Discussion in 'PHP' started by Marty_, May 18, 2008.

  1. #1
    if file exist show link to file

    I have a simple form with an option to upload a small file, the file names are renamed to id numbers 01-999999 etc...

    Currently I'm showing a link to the file, but not all users will have uploaded a file, so how would I go about not showing a link if the file doesn't exist (vise versa) ?

    PS: I don't want to involve database query's
     
    Marty_, May 18, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    jayshah, May 18, 2008 IP
  3. Marty_

    Marty_ Banned

    Messages:
    1,031
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi and thanks, yeah I'm using that
    <?php
    $filename = '/path/to/foo.txt';
    
    if (file_exists($filename)) {
        echo "The file $filename exists";
    } else {
        echo "The file $filename does not exist";
    }
    ?>
    
    Code (markup):
    But any ideas on how I would implement that to show a link to the file on a template page and of course not a show a link if the file doesn't exist ?
     
    Marty_, May 18, 2008 IP
  4. allaboutgeo

    allaboutgeo Peon

    Messages:
    85
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Do you mean hyper link?
     
    allaboutgeo, May 18, 2008 IP
  5. Marty_

    Marty_ Banned

    Messages:
    1,031
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #5
    yes a <a href=" link to the file, but only if it exist
     
    Marty_, May 18, 2008 IP
  6. allaboutgeo

    allaboutgeo Peon

    Messages:
    85
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Try this
    if (file_exists($filename)) {
        echo "<a href='$filename'>$filename</a>";
    }
    Code (php):
    Are you using relative path or absolute path to know the existence of the file? e.g.
    /path/to/foo.txt
    or
    path/to/foo.txt
     
    allaboutgeo, May 18, 2008 IP
  7. Marty_

    Marty_ Banned

    Messages:
    1,031
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #7
    OK I'm getting there, but I'm having a problem with writing the following into php

    lets assume I'm using your example with the printed result been $play
    if (file_exists($filename)) {
        echo "<a href='$filename'>$play</a>";
    }
    Code (php):
    This is my problem, its not right and I cant figure out whats wrong with it.
    
    $play = "- <a href="javascript:sendtoflash2('previewswf','../adverts/{$link.ID}');"><u>Listen</u></a> | <a href="javascript:sendtoflash2('previewswf','../adverts/nosound.mp3');"><u>Audio Off</u></a>";
    Code (php):
     
    Marty_, May 18, 2008 IP
  8. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #8
    Try

    
    $play = "- <a href=\"javascript:sendtoflash2('previewswf','../adverts/{$link.ID}');\"><u>Listen</u></a> | <a href=\"javascript:sendtoflash2('previewswf','../adverts/nosound.mp3');\"><u>Audio Off</u></a>";
    
    PHP:
    Escape " with \"
     
    jayshah, May 18, 2008 IP
  9. allaboutgeo

    allaboutgeo Peon

    Messages:
    85
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    $play = "- <a href=\"javascript:sendtoflash2('previewswf','../adverts/{$link.ID}');\"><u>Listen</u></a> | <a href=\"javascript:sendtoflash2('previewswf','../adverts/nosound.mp3');\"><u>Audio Off</u></a>";
    Code (php):
     
    allaboutgeo, May 18, 2008 IP
  10. Marty_

    Marty_ Banned

    Messages:
    1,031
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Hmm page wont load, by accounts the \ was what I thought was wrong but nope just wont load, theres some other kind of error stopping the php page from loading and its that line $play =
     
    Marty_, May 18, 2008 IP
  11. allaboutgeo

    allaboutgeo Peon

    Messages:
    85
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Does the page working fine without line $play=?
     
    allaboutgeo, May 18, 2008 IP
  12. Marty_

    Marty_ Banned

    Messages:
    1,031
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #12
    yes

    and the code itself is fine it currently working on the .tpl page but as above I'm trying to replace it with {$play} so the link only shows if there is indeed a file.
     
    Marty_, May 18, 2008 IP
  13. allaboutgeo

    allaboutgeo Peon

    Messages:
    85
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Are you using some template engine? Try without quotes around href value
    $play = "- <a href=javascript:sendtoflash2('previewswf','../adverts/{$link.ID}');><u>Listen</u></a> | <a href=javascript:sendtoflash2('previewswf','../adverts/nosound.mp3');><u>Audio Off</u></a>";
    Code (php):
     
    allaboutgeo, May 18, 2008 IP
  14. Marty_

    Marty_ Banned

    Messages:
    1,031
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #14
    nope... I'm lost:eek:
     
    Marty_, May 18, 2008 IP
  15. allaboutgeo

    allaboutgeo Peon

    Messages:
    85
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Did you try the code i provided in the last post?
     
    allaboutgeo, May 18, 2008 IP
  16. Marty_

    Marty_ Banned

    Messages:
    1,031
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Yes.. page wont load, strange I guess it just doesn't like javascript or something
     
    Marty_, May 18, 2008 IP
  17. allaboutgeo

    allaboutgeo Peon

    Messages:
    85
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Can you send me the complete page source code so that I can look into the issue?
     
    allaboutgeo, May 18, 2008 IP
  18. Marty_

    Marty_ Banned

    Messages:
    1,031
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #18
    Theres nothing really to send, I just need to get that line working so php can read it
     
    Marty_, May 18, 2008 IP
  19. Marty_

    Marty_ Banned

    Messages:
    1,031
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #19
    OMG I'm a noob, just spotted what was wrong, i was using a template variable in the php file {$link.ID}

    Ok thats that solved but I think i still have a problem...
     
    Marty_, May 18, 2008 IP
  20. allaboutgeo

    allaboutgeo Peon

    Messages:
    85
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #20
    What is the problem?
     
    allaboutgeo, May 18, 2008 IP