PHP函数在localhost上运行,但在我的服务器上运行

PHP函数在localhost上运行,但在我的服务器上运行

问题描述:

In a .php page I have the function below. In localhost the JSON response is different than NULL, but when I put the page in on the server (FTP) the JSON response is NULL.

THE CODE

// Check for required parameters
if (isset($_POST["placetype"]) && isset($_POST["placeName"])) {
  $pt = $_POST["placetype"];
  $pn = $_POST["placeName"];
  $dir = 'Pictures/Attractions/'.$pt.'/'.$pn.'/';
  $files = scandir($dir);
  foreach($files as $ind_file) {
    $result[] = array(
      "data" => $ind_file
    );
  sendResponse(200, json_encode($result));
  return true;
}//ifisset
sendResponse(400, 'Invalid request');
return false;

Note: the same folders are on both local and on the server.

Thanks.

在.php页面中,我有以下功能。 在localhost中,JSON响应不同于NULL,但是当我将页面放在服务器(FTP)上时,JSON响应为NULL。 p>

代码 strong>

  //检查所需参数
if(isset($ _ POST [“placetype”])&& isset($ _ POST [“placeName”])){
  $ pt = $ _POST [“placetype”]; 
 $ pn = $ _POST [“placeName”]; 
 $ dir ='Pictures / Attractions /'.$ pt。'/'。$ pn。'/';  
 $ files = scandir($ dir); 
 foreach($ files as $ ind_file){
 $ result [] = array(
“data”=> $ ind_file 
); 
 sendResponse(200  ,json_encode($ result)); 
返回true; 
} // ifisset 
sendResponse(400,'无效请求'); 
return false; 
  code>  pre> 
 
 

注意 strong>:本地和服务器上都有相同的文件夹。 p>

谢谢。 p> div>

See this

$files = scandir($dir);

If you haven't files in $dir, you variable $result will not create and will hold nothing, json will return null. On remote server you haven't files and json_encode return null

I'm guessing the PHP install on your server is not up to date. json_encode() only works with PHP 5 >= 5.2.0.