Checking whether a file exists within a certain folder

Discussion in 'PHP' started by derek34, Nov 13, 2007.

  1. #1
    I need to check whether a file exists using the is_dir() function. However I need to check whether a file exists within a certain folder. This is what I thought the code would look like:
    if ($_GET['filename'])
    	{
    	
    	$file = "courses/".$_GET['filename'];
    	if (is_dir($file)){
    	echo "yes";
    	}else {
    	echo "no";
    	}
    	}
    	
    	
    
    
    else
    {
    
    die('must specify filename');
    
    }
    PHP:
    this is supposed to work isn't it?
    I'm getting no coding errors, the script is just not detecting the file. The funny thing is that when I just enter "courses" in the $file variable, it detects a folder name 'courses'
    By the way, 'filename' is urlencoded $_GET from a previous script that lets the user choose the file to be displayed.
    Is there a different way of doing this?
     
    derek34, Nov 13, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    is_dir() checks specifically for directories. Use file_exists() or is_file() instead.

    (Note that file_exists() works for directories AND files)
     
    nico_swd, Nov 13, 2007 IP
  3. derek34

    derek34 Guest

    Messages:
    34
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    ah ok that makes sense...
    thanks for the response
     
    derek34, Nov 13, 2007 IP