0

I have a php file to retrieve image file name. the code is like this

<?php
$files = array();

$dir = opendir('upload/');

while ($file = readdir($dir)) {
    if ($file == '.' || $file == '..') {
        continue;
    }
    $data = array("picture"=>"http://my IP server/evie/upload/".$file);
    $fil[] = $data;
}

header('Content-type: application/json');
$files = array("image"=>$fil);
echo json_encode($files);
?>

when I run the code, it look like this

{"image":[{"picture":"http:\/\/my IP server\/evie\/upload\/bangladesh.png"},{"picture":"http:\/\/my IP server\/evie\/upload\/IMG_20130921_074201.jpg"},{"picture":"http:\/\/my IP server\/evie\/upload\/afghanistan.png"},{"picture":"http:\/\/my IP server\/evie\/upload\/india.png"},{"picture":"http:\/\/my IP server\/evie\/upload\/china.png"},{"picture":"http:\/\/my IP server\/evie\/upload\/srilanka.png"},{"picture":"http:\/\/my IP server\/evie\/upload\/nepal.png"},{"picture":"http:\/\/my IP server\/evie\/upload\/skorea.png"},{"picture":"http:\/\/my IP server\/evie\/upload\/Honoka-Miki2-300.jpg"},{"picture":"http:\/\/my IP server\/evie\/upload\/japan.png"},{"picture":"http:\/\/my IP server\/evie\/upload\/nkorea.png"},{"picture":"http:\/\/my IP server\/evie\/upload\/pakistan.png"}]}

It has a backslash '\'. how to replace it? I want the url look like a valid url. anyone can help me?

2
  • Your example output does not include the backslash, where are you having the problem? If it is with $file patching you could just str_replace \\ with /. Commented Jun 7, 2015 at 7:22
  • @Scott'scm6079' thank's for your answer. can you check this url flickr.com/photos/133076409@N07/18364878028 when I run my code, it's look like that photo in url. please help me Commented Jun 7, 2015 at 7:44

1 Answer 1

1

No problem here. json escapes slashes. If you do reverse operation - json_decode, you will see one slash

[0]=>
array(1) {
  ["picture"]=>
  string(44) "http://my IP server/evie/upload/bangladesh.png"
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank's for your answer, I am little bit understand. So I must use json_decode to replace the backslash? did you see my picture at flickr.com/photos/133076409@N07/18364878028 whether my code is correct?
You don't need to remove them. You produce correct json with escaped spashes. What is a problem?

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.