Jump to content

How to determine if a file is missing from my server


Smudly

Recommended Posts

When I upload new files, their file name is stored into my database. I have been told that a couple of the file downloads weren't working. Upon further investigation I figured out that the file the person wanted to download was nowhere to be seen in my downloads directory. How can I determine if a file exists in my directory? I assume I can somehow use the location that is stored inside my database (ex: myfile.pdf) and compare that with what's in the folder.

 

 

Thanks for the reply. I tried using file exist to check each row in the table and determine if the file does in fact exist. If it does not, that row will be displayed to the page. If it does exist, nothing will be done. The problem I am having is it is displaying ALL rows to the page.

I've tried changing up the code in various ways but nothing seems to work.

 

Here is my code. (no errors are displayed either)

 


$result = mysql_query("SELECT * FROM sheets WHERE active='yes'");

$path = "/uploads/";
echo "<table border='0' cellspacing='0' cellpadding='0'>";
echo "<th>ID</th>
  <th>Artist</th>
  <th>Title</th>
  <th>URL</th>
";
$i = 1;
while($row = mysql_fetch_array($result)){

$url = $row['url'];
$sheet = $path.$url;
if (file_exists($sheet)){
$artist = "";
$title = "";
$sheet = "";
}
else{
$artist = $row['artist'];
$title = $row['title'];
echo "<tr>";
echo "<td>$i</td>";
echo "<td>$artist</td>";
echo "<td>$title</td>";
echo "<td>$url</td>";
echo "</tr>";
$i++;
}

}
echo "</table>";

Since file_exists() is a PHP function, it needs the real path to the file; not the path relative to your document root.  If that uploads directory is in your document root, you can change that to:

$path = $_SERVER['DOCUMENT_ROOT'] . "/uploads/";



$sheet = $path.$url;
if (file_exists($sheet)){

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.