suttercain Posted September 29, 2008 Share Posted September 29, 2008 Hi guys, I am debating on having a script which checks a directory for a specific file. If the file is found, a URL to the file will be provided. If no file is found, there is no URL to go to so no link. My concern though is with resources. I do a mysql_query which sometimes renders hundreds of results. I would need this script to run for each specific result and than loop through the directory. Do you think this would use to much resources or no? Thanks for your opinions. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 29, 2008 Share Posted September 29, 2008 Wait, what do you mean "run this script for each specific result"? You can probably optimize this if you do it right. Quote Link to comment Share on other sites More sharing options...
suttercain Posted September 29, 2008 Author Share Posted September 29, 2008 I do the mysql query and create a while loop to display the results. Within the while loop I foreach loop that looks like this: foreach (glob("$dir/".$row['filename'].".pdf", GLOB_BRACE) as $file) If the file matches the sql result (Filename === Filename) echo <a href="#"></a> If no file matches no link. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 29, 2008 Share Posted September 29, 2008 Why not just: if (file_exists("$dir/{$row['filename']}.pdf") { } inside the while loop? Quote Link to comment Share on other sites More sharing options...
suttercain Posted September 29, 2008 Author Share Posted September 29, 2008 Because I am a jackass who didn't even know a function like that existed Thanks. You just saved me a lot of work Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 29, 2008 Share Posted September 29, 2008 Because I am a jackass who didn't even know a function like that existed Thanks. You just saved me a lot of work Lol, you knew about glob(), but not file_exists()? You'd be surprised at how often it's the other way around. Anyway, glad I could help. Quote Link to comment Share on other sites More sharing options...
suttercain Posted September 29, 2008 Author Share Posted September 29, 2008 Thanks again for getting back to me with this. I am trying the file_exists function and it's returning FALSE even though the file does exist. I checked to ensure safe_mode is off and it is. Any other reason this may be kicking back FALSE instead of true? Thanks again. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 29, 2008 Share Posted September 29, 2008 What's stored in $dir? Quote Link to comment Share on other sites More sharing options...
suttercain Posted September 29, 2008 Author Share Posted September 29, 2008 I have it set up like this, because the directory location depends on some of the mysql results: <?php //This is in the WHILE Loop used to mysql_fetch_array() $filename = 'http://www.domain.com/msprog/offroad/cert/eo/'.$row['engine_year'].'/ofci/'.$row['engine_executive_order'].'.pdf'; if (file_exists($filename)) { $engineFamilyName = "<a href=".$filename.">".$row['engine_family_name']."</a>"; } else { $engineFamilyName = $row['engine_family_name']; } echo $engineFamilyName; ?> When I echo $filename I get the correct link and am able to go to it via the URL. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 29, 2008 Share Posted September 29, 2008 file_exists() doesn't work through the URL wrapper. Are they actually going to always be URLs like that? If they are, I can suggest a bit of a rewrite to get it to work. Quote Link to comment Share on other sites More sharing options...
suttercain Posted September 29, 2008 Author Share Posted September 29, 2008 Yeah the directories are horrible (not in my control) and they will always be like this. So the file_exists will nto work with an absolute path? I should instead do ..../../../msprog/offroad/cert/eo/'.$row['engine_year'].'/ofci/'.$row['engine_executive_order'].'.pdf'; ? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 29, 2008 Share Posted September 29, 2008 Yeah the directories are horrible (not in my control) and they will always be like this. So the file_exists will nto work with an absolute path? I should instead do ..../../../msprog/offroad/cert/eo/'.$row['engine_year'].'/ofci/'.$row['engine_executive_order'].'.pdf'; ? Wait, are they on your site? file_exists() doesn't work with URLs, but it works with relative or absolute filesystem paths. Quote Link to comment Share on other sites More sharing options...
suttercain Posted September 29, 2008 Author Share Posted September 29, 2008 Yeah they are on the site. The sire is a mess though with over 1000 direcotires, but I should be able to figure out the path. Quote Link to comment Share on other sites More sharing options...
suttercain Posted September 29, 2008 Author Share Posted September 29, 2008 I tried both the relative and absolute paths and they still turn up FALSE... Quote Link to comment Share on other sites More sharing options...
suttercain Posted September 30, 2008 Author Share Posted September 30, 2008 I'm still not able to get this file_exists function to work for me. Here is the code I am using... <?php $filename = '/msprog/offroad/cert/eo/'.$row['engine_year'].'/ofci/'.$row['engine_executive_order'].'.pdf'; if (file_exists($filename)) { $engineFamilyName = "<a href=".$filename.">".$row['engine_family_name']."</a>"; } else { $engineFamilyName = $row['engine_family_name']; //This is what it echos even though a file is there. } echo $engineFamilyName . "<br />"; echo "<a href='$filename'>$filename</a>"; //I check the file path and it works. The link goes to the pdf document. ?> Any help would be greatly appreciated. Thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.