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
I think you can use the function file_exists ... http://www.php.net/file_exists ... to do what you are looking for. Jay
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 ?
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
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):
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 \"
$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):
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 =
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.
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):
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...