sending a filepath in json string to PHP codeigniter

21,213

Since it's JSON, you have to consider that your directory separators are going to be treated as escapes by the JSON parser. JSON has NO idea that you're passing in a path. It's just going to see a bunch of unnecessary escapes. The JSON must be syntactically valid, which means:

{"path":"D:\\my\\path\\to\\file.txt","foo":"bar"}

Note the doubled-up backslashes. Also note that on Windows, PHP is smart enough to accept forward slashes as the path separators as well, and will auto-convert for your as necessary. d:/my/path/to/file.txt would work just as well.

Share:
21,213
Oldenborg
Author by

Oldenborg

Updated on July 19, 2020

Comments

  • Oldenborg
    Oldenborg almost 4 years

    I have to send a filepath via. a json string to my controller in codeigniter

    I have already modifed codeigniters config to accept a json string as a paramater

    example:

    www.mysite.com/controller/function/{"foo":"bar","hello":"word"}

    This works fine.

    But now I want to pass in a file path like this

    www.mysite.com/controller/function/{"path":"D:\my\path\to\file.txt","foo":"bar"}

    When i urldecode the paramter sent to the function in the controller I get a nice string looking like this

    {"path":"D:\my\path\to\file.txt","foo":"bar"}

    When I try to json_decode this I get

    NULL

    Can anyone help me with this?

  • Oldenborg
    Oldenborg almost 10 years
    Thank you, ofc. this was the problem :) such a simple thing