0

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.

9
  • Do you run php5 or greater on your webserver? Commented Jul 14, 2012 at 15:40
  • What exactly does this sendReponse function do? It's not a standard PHP function. As well, you'll get NULL as json if the path you're generating doesn't exist - you never set $result to be anything UNLESS at least one file is found. Commented Jul 14, 2012 at 15:41
  • The $dir path exists on the server? Verify path. Commented Jul 14, 2012 at 15:43
  • Could be a file permission problem. Can the webserver user read the pictures folder? Commented Jul 14, 2012 at 15:44
  • 3
    And please sanitize your input. What happens if $_POST["placeName"] = '../../../folder_with_all_my_passwords'? Commented Jul 14, 2012 at 15:46

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Yup the $files is empty i should write $ dir ='./Pictures/Attractions/'.$pt.'/'.$pn.'/';
1

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

1 Comment

Others .php files are contains bunch of json_encode and they are working properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.