Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    May 2005
    Posts
    262
    Thanks
    4
    Thanked 0 Times in 0 Posts

    is_file() or file_exists() - URL

    Hello

    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:

    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).";
    else
    $value .= "<a href=\"".$file."\">".$row["ModuleName"]."</a>";
    
    $value .= "</td>
                  <td width=300>
                     ".$row["Version"]."
                  </td>
                  <td>
                  ".$row["LastChangeDate"]."
                  </td>
          </tr>";
    }

    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.

  • #2
    Regular Coder
    Join Date
    May 2004
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    c.c.

  • #3
    Regular Coder
    Join Date
    May 2005
    Posts
    262
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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

  • #4
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Are you sure $file is being built correctly relative to the location of the script?

  • #5
    Regular Coder
    Join Date
    May 2005
    Posts
    262
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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

  • #6
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think that's your problem. If I remember correctly, neither is_file() nor file_exists() will work with http.

  • #7
    Regular Coder
    Join Date
    May 2005
    Posts
    262
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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?

  • #8
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    PHP Code:
    if(!@fopen('http://domain.com/foo/bar.gif'))
    {
     echo 
    'failure';
    }
    else
    {
     echo 
    'success';


  • #9
    Regular Coder
    Join Date
    May 2005
    Posts
    262
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks a lot! That worked. I will just use the relative path instead.

    Thanks for your time
    Last edited by tripwater; 06-10-2005 at 11:52 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •