Jump to content

fileSearch - return() not working..


chokri

Recommended Posts

this is the first time i'm using php for website development.  i'm having issues with a fileSearch function i created to return a file's location.  the code is as follows:

[code]  function fileSearch($tempFile,$curdirLoc)
  {
    $fileFound = false;
    $compfileLoc = null;
    $curDir = scandir($curdirLoc);
    foreach($curDir as $curEntry)
    {
        if($curEntry != "." && $curEntry != "..")
{
  $myLoc = $curdirLoc."\\".$curEntry;
          if(is_dir($myLoc))
          {
              fileSearch($tempFile,$myLoc);
          }
          else
          {
              if(strcmp($curEntry,$tempFile) == 0)
              {
                $compfileLoc = $myLoc;
                $fileFound = true;
              }
          }
        }
    }
    if($fileFound) {return $compfileLoc;}
  }[/code]

i know the code works and finds the file.  the problem is when returning $compfileLoc.

if $compfileLoc = "c:\Inetpub\wwwroot\website\sitemap.php" it works
if $compfileLoc = "c:\Inetpub\wwwroot\website\Content\Contact_Us\Contact_Information.php" it doesn't work.

are there any limits to what can be returned?  i can't imagine the "\" causing problems since it works for some files.  i'd truly appreciate any help that can be provided!
Link to comment
Share on other sites

Try chaning the code this way:

[code]<?php

  function fileSearch($tempFile,$curdirLoc)
  {
    $fileFound = false;
    $compfileLoc = null;
    $curDir = scandir($curdirLoc);
    foreach($curDir as $curEntry)
    {
        if($curEntry != "." && $curEntry != "..")
{
  $myLoc = $curdirLoc."\\".$curEntry;
          if(is_dir($myLoc))
          {
              $compfileLoc=fileSearch($tempFile,$myLoc);
              if(compfileLoc!==FALSE){return $compfileLoc;}
          }
          else
          {
              if(strcmp($curEntry,$tempFile) == 0)
              {
                $compfileLoc = $myLoc;
                $fileFound = true;
              }
          }
        }
    }
    if($fileFound) {return $compfileLoc;}
    else{return FALSE;}
  }

?>[/code]

Orio.
Link to comment
Share on other sites

i spoke too soon.

previously:
if $compfileLoc = "c:\Inetpub\wwwroot\website\sitemap.php" [b]it works[/b]
if $compfileLoc = "c:\Inetpub\wwwroot\website\Content\Contact_Us\Contact_Information.php" [b]it doesn't work[/b]

after doing as you suggested orio, now it's:
if $compfileLoc = "c:\Inetpub\wwwroot\website\sitemap.php" [b]it doesn't work[/b]
if $compfileLoc = "c:\Inetpub\wwwroot\website\Content\Contact_Us\Contact_Information.php" [b]it works[/b]
Link to comment
Share on other sites

I [i]think[/i] it should work fine now :)

[code]<?php

  function fileSearch($tempFile,$curdirLoc)
  {
    $fileFound = false;
    $compfileLoc = null;
    $curDir = scandir($curdirLoc);
    foreach($curDir as $curEntry)
    {
        if($curEntry != "." && $curEntry != "..")
{
  $myLoc = $curdirLoc."\\".$curEntry;
          if(is_dir($myLoc))
          {
              $compfileLoc=fileSearch($tempFile,$myLoc);
              if(compfileLoc!==FALSE){return $compfileLoc;}
          }
          else
          {
              if(strcmp($curEntry,$tempFile) == 0)
              {
                $compfileLoc = $myLoc;
                return $compfileLoc;
              }
          }
        }
    }
    if($fileFound) {return $compfileLoc;}
    else{return FALSE;}
  }

?>[/code]

Orio.
Link to comment
Share on other sites

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.