suttercain Posted September 30, 2008 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 https://forums.phpfreaks.com/topic/126449-file_exists-cant-get-it-to-work-for-me/ Share on other sites More sharing options...
F1Fan Posted September 30, 2008 Share Posted September 30, 2008 file_exists uses absolute server paths, so add $_SERVER['DOCUMENT_ROOT'] before your $filename variable in the file_exists function. file_exists($_SERVER['DOCUMENT_ROOT'].$filename) Modify: Actually, you may have an extra / in there, because I think the doc root var has one at the end. Quote Link to comment https://forums.phpfreaks.com/topic/126449-file_exists-cant-get-it-to-work-for-me/#findComment-653814 Share on other sites More sharing options...
suttercain Posted September 30, 2008 Author Share Posted September 30, 2008 Hi F!Fan, Thanks for the reply. I tried that and altered the code to be: <?php $filename = $_SERVER['DOCUMENT_ROOT'] . '/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 . "<br />"; //echos the else result, no link. echo "<a href='$filename'>$filename</a>"; //This no longer links to the document like it did in the previous code. ?> This is the path it is trying to check the file for: /app/www/outside/msprog/offroad/cert/eo/2002/ofci/U-R-011-0064.pdf Quote Link to comment https://forums.phpfreaks.com/topic/126449-file_exists-cant-get-it-to-work-for-me/#findComment-653823 Share on other sites More sharing options...
F1Fan Posted September 30, 2008 Share Posted September 30, 2008 Is that the absolute path on the server, or is it the part after the www.yoursite.com/ ? The reason I ask is because you're using the same thing for your a href and for file_exists. They need to be treated differently. So, let's use this example. Let's say that on your server, the path is: /data/website/files/somefile.pdf and the web path is: www.yoursite.com/files/somefile.pdf That means your file_exists needs to use /data/website/files/somefile.pdf and a href needs to use /files/somefile.pdf Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/126449-file_exists-cant-get-it-to-work-for-me/#findComment-653831 Share on other sites More sharing options...
suttercain Posted September 30, 2008 Author Share Posted September 30, 2008 Gotcha, Okay I did this: <?php $path = '/app/www/outside'; $filename = '/msprog/offroad/cert/eo/'.$row['engine_year'].'/ofci/'.$row['engine_executive_order'].'.pdf'; $filepath = $path . $filename; if (file_exists($filepath)) { $engineFamilyName = "<a href=".$filename.">".$row['engine_family_name']."</a>"; } else { $engineFamilyName = $row['engine_family_name'];//this is the one that gets echoed. } echo $engineFamilyName . "<br />"; //echos the else result, no link. echo "<a href='$filename'>$filename</a>"; //This link works and goes to the file. ?> The $filename now links properly, but the check for file_exists still doesn't link right when checking for the file. Also, I checked and our site's safe_mode is off. Quote Link to comment https://forums.phpfreaks.com/topic/126449-file_exists-cant-get-it-to-work-for-me/#findComment-653843 Share on other sites More sharing options...
F1Fan Posted September 30, 2008 Share Posted September 30, 2008 Hmm. Try this maybe: <?php $filename = 'msprog/offroad/cert/eo/'.$row['engine_year'].'/ofci/'.$row['engine_executive_order'].'.pdf'; if (file_exists($_SERVER['DOCUMENT_ROOT'] . $filename)) { $engineFamilyName = "<a href='/".$filename."'>".$row['engine_family_name']."</a>"; } else { $engineFamilyName = $row['engine_family_name'];//this is the one that gets echoed. } echo $engineFamilyName . "<br />"; //echos the else result, no link. echo "<a href='/$filename'>$filename</a>"; //This link works and goes to the file. ?> I moved some of the /s around Quote Link to comment https://forums.phpfreaks.com/topic/126449-file_exists-cant-get-it-to-work-for-me/#findComment-653851 Share on other sites More sharing options...
suttercain Posted September 30, 2008 Author Share Posted September 30, 2008 Thanks again for helping with this. I tried the code above and still no link but I can see why. I am running the script at: http://domain.com/diesel/verdev/vdb/vdb.php The files are located here: http://domain.com/msprog/offroad/cert/eo/2002/ofci/U-R-011-0064.pdf When I tried the code above it is trying to find the file here: http://domain.com/diesel/verdev/vdb/msprog/offroad/cert/eo/2002/ofci/U-R-011-0064.pdf Quote Link to comment https://forums.phpfreaks.com/topic/126449-file_exists-cant-get-it-to-work-for-me/#findComment-653865 Share on other sites More sharing options...
mursalat Posted September 30, 2008 Share Posted September 30, 2008 the path is: '../../../msprog/offroad/cert/eo/' forget about the server root, it works for me without the server root stuff, so the new code can be: <?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. ?> Quote Link to comment https://forums.phpfreaks.com/topic/126449-file_exists-cant-get-it-to-work-for-me/#findComment-653873 Share on other sites More sharing options...
F1Fan Posted September 30, 2008 Share Posted September 30, 2008 That's really odd. I just verified and the doc root variable should give the root, which I believe in your case is "/app/www/outside/". Check out the $_SERVER variable and it's available keys: http://us2.php.net/manual/en/reserved.variables.server.php If that doesn't work, try hard-coding it like you started to do before. Quote Link to comment https://forums.phpfreaks.com/topic/126449-file_exists-cant-get-it-to-work-for-me/#findComment-653874 Share on other sites More sharing options...
suttercain Posted September 30, 2008 Author Share Posted September 30, 2008 Cool. Thanks for your time. Quote Link to comment https://forums.phpfreaks.com/topic/126449-file_exists-cant-get-it-to-work-for-me/#findComment-653875 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.