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
https://forums.phpfreaks.com/topic/20737-filesearch-return-not-working/
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.
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]
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.

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.