简体   繁体   中英

php glob and scandir function is not working on web server

i am using this glob function on my website dont know what is the problem.wasted too much time on this.its working on localhost but not working in webserver. please help me

code :

 $result = mysql_query($qry) or die ("Query failed");
    $count=mysql_num_rows($result);
    $HTML='';
    if($count > 0){
    while ($friendList = mysql_fetch_array($result))
     {

     $_SESSION['PropertyId'] = $friendList['property_Id'];
     $Username = $friendList['UserName'];
     $qry1 = "SELECT Mobile_Number1,FirstName FROM registration WHERE UserName = '".$Username."'";
    $result1 = mysql_query($qry1) or die ("Query failed");
    $friendList1 = mysql_fetch_array($result1);
    $mobNo = $friendList1['Mobile_Number1'];
    $Name = $friendList1['FirstName']; 
    $image = "";
    $dir = "propertyImages/".$friendList['property_Id']."";
    $files = array();
    $files = glob("$dir/*.*");
    $image = "";
    print_r($files);
    if (count($files) > 0) 
    {
    $image = $files[0];
    }
    else
    {
    $image = 'img/1.jpg';
    }

As @Emilio stated, try readdir() instead of glob() , especially because you are not really needing the pattern recognition glob() does provide.

Could you give this a try?

// enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');

// initialise empty $files array
$files= array();

if (file_exists($dir)) {
  $dh= opendir($dir);
  if ($dh) {
    while ($file= readdir($dh)) {

      // skip "." and ".."
      if ($file != "." && $file != "..") {
        echo "$file\n";
        $files[]= $file;
      }
    }
    closedir($dh);
  } else {
    print 'Directory handle could not be opened.';
  }
} else {
  print 'Directory not accessible or does not exist.';
}

General comments:

1.) another +1 for @Emilio's comment about mysql_* functions

2.) If you are running your script on a shared host you may also experience problems due to safe_mode being enabled.

3.) Checking for errors when using ajax calls: Firebug's Network panel will reveal the response returned from the server, just have it open while doing the call. If any error is output by the server you will see the error on the Response tab of the corresponding AJAX call.

4.) Regarding code: you don't need to initialise variables like $image or $files if they are directly overwritten, the two double quotes at the end of the $dir = ... are also obsolete. I would also recommend to decide for one variable style, best would be CamelCase syntax with a lowercase letter after the $.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM