View Full Version : is_file() or file_exists() - URL

06-10-2005, 09:15 PM

Here is what I am trying to do. I have a directory on my server that holds modules for download. Users can upload modules to this dir.

Now I query my DB for all of the Module names and list them out. Now it is possible that a old module is still in the DB but the actual file is not in the modules directory. So if it is not in the directory, I jsut want to show the name, otherwise I want to show a link so they can download the latest module.

Here is some basic code:

//here I query for the URL of the site from the company table in order to put it in the download URL

$query = "select URL from company";

$tempresult = @mysql_query($query);

$temprow = @mysql_fetch_array($tempresult);

for ($i = 0; $i < @mysql_num_rows($result); $i++)
$row = @mysql_fetch_array($result);

$file = $temprow["URL"]."/modules/".$row["ModuleName"];

$value .= "<tr>
<td width=300>";

//here I check to see if the file exists in the dir. If not then I do not show a link
//to download it

if (!is_file($file))
$value .= $row["ModuleName"]." (File Missing).";
$value .= "<a href=\"".$file."\">".$row["ModuleName"]."</a>";

$value .= "</td>
<td width=300>

As of right now if I use either is_file() or file_exists() and pass in $file, everything is normal text not a link. I know for a fact most of these are in the dir. I just know that I am doing something wrong. Can you not pass in the full URL to a file into one of these functions? If not, then how do I determine if the file does in fact exist at this location?

Thank you for any help with this.

06-10-2005, 09:36 PM
for ($i = 0; $i < @mysql_num_rows($result); $i++)

should this be:
for ($i = 0; $i < @mysql_num_rows($tempresult); $i++)
$row = @mysql_fetch_array($tempresult);

I think thats why there are mysql_num_rows.

06-10-2005, 09:41 PM
Thanks, but no it is correct.

I left out some code and queries because they are not important to my question. Neither is mysql_fetch_array() important to my question. I grabbed the row I needed for the URL of the company. It is only one record. The for loop is looping through all of my modules from a query that is not displayed here. I did not think it was necessary to show this.

I need to know how to find out if a file exists on the server in a designated directory.

I thought you could use file_exists() but it does not seem to handle the full path to the file. Can anyone help me with this please?

Thank you

Kid Charming
06-10-2005, 09:45 PM
Are you sure $file is being built correctly relative to the location of the script?

06-10-2005, 10:22 PM
Thanks for the reply.

Yes, I have echoed the var $file and it holds the FULL path.

$file = $temprow["URL"]."/modules/".$row["ModuleName"];

The URL and ModuleName are dynamic. The modules dir is the only thing that will not change.

$temprow["URL"] - holds the URL of the site (Example : http://tasks.mysite.com)

/modules/ - is the dir holding the modules in the root directory of my site

$row["moduleName"] - holds the name of the file and its extention (example: this.exe)

so when I echo $file it shows - http://tasks.mysite.com/modules/this.exe

If I do not check to see if the file exists and just hardcode it, if the file is there, it will start to download. However, if the file is not in the dir, then it says cannot find page on server. This is why I am trying to check to see if the file does in fact exist on the server first, then either show a link to download or just the name of the file.

Thanks again

Kid Charming
06-10-2005, 11:18 PM
I think that's your problem. If I remember correctly, neither is_file() nor file_exists() will work with http.

06-10-2005, 11:24 PM
Then is there a different function I can use to check to see if the file exists?

This just seems odd that there is not a simple way to see if a file exists on the server within a directory.

I am using the URL field from the DB because we sell this application and from company to company the URL can be different, or be a subdomain so I always have to get it from the DB.

Thanks again

Anyone have ideas on this? Or have run into this before?

Kid Charming
06-10-2005, 11:46 PM
If you're dealing with the same server, you need to use the filesystem instead of the domain. Depending on where your script is located, you may be able to simply use if(!file_exists('/modules/foo/bar.gif')).

If you have to use the domain, I guess you could do something like this:

echo 'failure';
echo 'success';

06-10-2005, 11:49 PM
Thanks a lot! That worked. I will just use the relative path instead.

Thanks for your time