Jump to content

is there a better way checking if file exists before "require"ing?


Recommended Posts

What I've got works, but I'm concerned about the fact that the only reason the file wouldn't exist is if the user messed with the url. ($in is a url variable, properly sanitized, etc.). I don't want users to see "Warning: require_once(xxx) [function.require-once]: failed to open stream: etc, etc." if they're just poking around and messing with the url, but it seems like such overkill to check every time if the file exists when most of the time it will. There's probably a better way to do this...thoughts?

 

<?php
if(file_exists($in.'.php')){
   require_once($in.'.php')){
}else{
   print 'Woops!';
}?>

typing a function name at the top of each page is alot better than typing that :) I'd put it in a function and just call the function when its needed. I always use include or include_once so no errors show up if something happens that the file isn't there, but then again im still learning PHP lol

This might be worse, if not exactly the same, but you could store an array of the file names and check it off the array instead of using the file_exists function. I can't imagine that being any different, thought, as it does the exact same thing.

 

If you use the URL method, you gotta assume the user is an idiot, and expect for them to play with everything they possibly can. I guess that would be the downfall to using the URL to pass the $in variable, but you are well aware of that, I'm sure.

Just a note: I'm not sure if you are aware of is_file(), but it seems (and I'm not positive) to be a slightly faster alternative. It also won't return true for directories like file_exists would.

 

 

 

 

good idea JBS, thanks.

 

I guess I will switch it to an error surpressed include for now and not check if the file exists. That way if they try to mess with it the most they'll get is a blank page :/

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.