-1

I already saw this question, but it didn't work for me.

What I need to do is to check whether a file exists, in PHP, without knowing the extension. I use this code:

<?php 
if (count(glob("/database/".$_REQUEST['thetitle'].".*")) == 0) {
echo 'true';
} else {
echo 'false';
}
?>

EDIT: Maybe it's relevant saying that the script is located in

[root]/functions/validatefilename.php

and the database in

[root]/database/

But it always returns false, no matter what the filename ($_REQUEST['thetitle']) is.

Community
  • 1
  • 1
Isaiah
  • 1,703
  • 4
  • 22
  • 41
  • I doubt your file is at `/database/...`. That's the *root of the disk!* – deceze Aug 28 '13 at 14:48
  • Right, I forgot. In HTML, `/database/` would be a folder at the root of my domain. How would you do that in PHP then? – Isaiah Aug 28 '13 at 14:56
  • 1
    *URLs* in HTML are relative to the current/root *URL*, *local file paths* are relative to where the files are on disk/the disk root. PHP doesn't really care about URLs. URLs have nothing to do with local file paths and vice versa. – deceze Aug 28 '13 at 14:58

2 Answers2

2

try:

count(glob("./database/".$_REQUEST['thetitle'].".*"))
Dusan Plavak
  • 3,187
  • 3
  • 21
  • 31
1

Looks to me like it works fine except that you should be specifying the full path:

if (count(glob( "/path/to/" . "database/" .$_REQUEST['thetitle']. ".*")) == 0) {
  • As my edited question now illustrates, /database/ is directly after the root, like website.com/database/ – Isaiah Aug 28 '13 at 14:33
  • you still may need to explicitly map out the entire path – XaxD Aug 28 '13 at 14:39
  • I tried `$thevar = glob("http://www.daltonempire.nl/database/Hello.*"); echo '"'; print_r($thevar); echo '"';` and I put a file named "Hello.png" there, but it would still echo "" – Isaiah Aug 28 '13 at 14:53