Take the 2-minute tour ×
if (($json=file_get_contents($_FILES["file_upload"]["tmp_name"]))) {

I upload a file and then read it in PHP. THe problem I get NULL returned from json_decode, the json is valid though, because when I try to decode the same JSON from a string it decodes fine. Any ideas what might be wrong.

I have figured out that I get three � at the beggining of the string. So I do a substr for now, but what is this?

share|improve this question
do echo $json to verify it's reading the file properly –  Aaron W. Jan 8 '13 at 11:58
are you sure the JSON is properly formatted?, if it's not, json_decode will return null. –  Eoin Murphy Jan 8 '13 at 11:59
It is properly formatted because I try the Json in a string and it works. The file is read properly to. –  Borut Flis Jan 8 '13 at 12:02
Is the json file is in unicode format? –  shiplu.mokadd.im Jan 8 '13 at 12:04
have you checked why json_decode() failed? You can use json_last_error() to find out more information about the error. –  SDC Jan 8 '13 at 12:04
show 4 more comments

2 Answers

If file_get_contents returns FALSE, then there was an error in reading the file. However, it can also return NULL, which would mean that the function is disabled in your ini.

You can check it by checking the set value of the allow_url_fopen in your ini settings.

 if(ini_get('allow_url_fopen') != 1)
  echo "<script>window.alert='getting contents is not allowed'</script>";

If this is the case, then you would have to request your host to enable this.

share|improve this answer
add comment

Add below code on the top of your PHP file, which is generating JSON.

share|improve this answer
add comment

Your Answer


