Jump to content

file_exists() - Can't get it to work for me...


Recommended Posts

I'm still not able to get this file_exists function to work for me. Here is the code I am using...

 

<?php
$filename = '/msprog/offroad/cert/eo/'.$row['engine_year'].'/ofci/'.$row['engine_executive_order'].'.pdf';
  if (file_exists($filename)) {
    $engineFamilyName = "<a href=".$filename.">".$row['engine_family_name']."</a>";
  } else {
    $engineFamilyName = $row['engine_family_name']; //This is what it echos even though a file is there.
  }

echo $engineFamilyName . "<br />";

echo "<a href='$filename'>$filename</a>"; //I check the file path and it works. The link goes to the pdf document.
?>

 

Any help would be greatly appreciated. Thanks.

Link to comment
https://forums.phpfreaks.com/topic/126449-file_exists-cant-get-it-to-work-for-me/
Share on other sites

file_exists uses absolute server paths, so add $_SERVER['DOCUMENT_ROOT'] before your $filename variable in the file_exists function.

 

file_exists($_SERVER['DOCUMENT_ROOT'].$filename)

 

Modify: Actually, you may have an extra / in there, because I think the doc root var has one at the end.

Hi F!Fan,

 

Thanks for the reply. I tried that and altered the code to be:

 

<?php
$filename = $_SERVER['DOCUMENT_ROOT'] . '/msprog/offroad/cert/eo/'.$row['engine_year'].'/ofci/'.$row['engine_executive_order'].'.pdf';
  if (file_exists($filename)) {
    $engineFamilyName = "<a href=".$filename.">".$row['engine_family_name']."</a>";
  } else {
    $engineFamilyName = $row['engine_family_name'];
  }

echo $engineFamilyName . "<br />"; //echos the else result, no link.

echo "<a href='$filename'>$filename</a>"; //This no longer links to the document like it did in the previous code.
?>

 

This is the path it is trying to check the file for:

/app/www/outside/msprog/offroad/cert/eo/2002/ofci/U-R-011-0064.pdf

Is that the absolute path on the server, or is it the part after the www.yoursite.com/ ?

 

The reason I ask is because you're using the same thing for your a href and for file_exists. They need to be treated differently. So, let's use this example. Let's say that on your server, the path is:

/data/website/files/somefile.pdf

and the web path is:

www.yoursite.com/files/somefile.pdf

 

That means your file_exists needs to use /data/website/files/somefile.pdf and a href needs to use /files/somefile.pdf

 

Hope that helps.

Gotcha,

 

Okay I did this:

 

<?php
$path = '/app/www/outside';
$filename = '/msprog/offroad/cert/eo/'.$row['engine_year'].'/ofci/'.$row['engine_executive_order'].'.pdf';
$filepath = $path . $filename;
  if (file_exists($filepath)) {
    $engineFamilyName = "<a href=".$filename.">".$row['engine_family_name']."</a>";
  } else {
    $engineFamilyName = $row['engine_family_name'];//this is the one that gets echoed.
  }

echo $engineFamilyName . "<br />"; //echos the else result, no link.

echo "<a href='$filename'>$filename</a>"; //This link works and goes to the file.
?>

 

The $filename now links properly, but the check for file_exists still doesn't link right when checking for the file.

 

Also, I checked and our site's safe_mode is off.

Hmm. Try this maybe:

 

<?php
$filename = 'msprog/offroad/cert/eo/'.$row['engine_year'].'/ofci/'.$row['engine_executive_order'].'.pdf';
  if (file_exists($_SERVER['DOCUMENT_ROOT'] . $filename)) {
    $engineFamilyName = "<a href='/".$filename."'>".$row['engine_family_name']."</a>";
  } else {
    $engineFamilyName = $row['engine_family_name'];//this is the one that gets echoed.
  }

echo $engineFamilyName . "<br />"; //echos the else result, no link.

echo "<a href='/$filename'>$filename</a>"; //This link works and goes to the file.
?>

 

I moved some of the /s around

Thanks again for helping with this.

 

I tried the code above and still no link but I can see why. I am running the script at:

http://domain.com/diesel/verdev/vdb/vdb.php

 

The files are located here:

http://domain.com/msprog/offroad/cert/eo/2002/ofci/U-R-011-0064.pdf

 

When I tried the code above it is trying to find the file here:

http://domain.com/diesel/verdev/vdb/msprog/offroad/cert/eo/2002/ofci/U-R-011-0064.pdf

 

 

the path is: '../../../msprog/offroad/cert/eo/'

forget about the server root, it works for me without the server root stuff, so the new code can be:

<?php
$filename = '../../../msprog/offroad/cert/eo/'.$row['engine_year'].'/ofci/'.$row['engine_executive_order'].'.pdf';
  if (file_exists($filename)) {
    $engineFamilyName = "<a href=".$filename.">".$row['engine_family_name']."</a>";
  } else {
    $engineFamilyName = $row['engine_family_name']; //This is what it echos even though a file is there.
  }

echo $engineFamilyName . "<br />";

echo "<a href='$filename'>$filename</a>"; //I check the file path and it works. The link goes to the pdf document.
?>

That's really odd. I just verified and the doc root variable should give the root, which I believe in your case is "/app/www/outside/". Check out the $_SERVER variable and it's available keys: http://us2.php.net/manual/en/reserved.variables.server.php

 

If that doesn't work, try hard-coding it like you started to do before.

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.