Jogan555's avatar

Filename includes Backslashes also with stripslashes

I have got a the following problem: I have create a simple Image Upload Process like this.

public function postImage($id){
    $filename = date('Y-m-d_H:i:s').".jpg";
    $note=Note::find($id);
    $destinationPath='/srv/www/htdocs/example.com/api/public/projects/'.$note->project_id.'/notes/'.$id."/images/";
    $file = Request::file('image');
    $file->move($destinationPath, $filename);
    $image = new Image;
    $image->filename='projects/'.$note->project_id."/notes/images/".$filename;
    $image->note_id=$id;
    $image->save();
    $result= $this->respondWithItem($image, new ImageTransformer);
    return $result;
}

When returning filename which is in this case path to file beginning from projects plus at the end the filename it always returns something like that projects/1/notes/images/2015-03-27_10:44:291.jpg I of course dont want to have backslashes in my response. I use a Transformer for that like this:

<?php namespace App\Http\Controllers;
    use Image;
    use League\Fractal\TransformerAbstract;
    class ImageTransformer extends TransformerAbstract
    {
        public function transform(Image $image){
            return [
                'id' => (int) $image->id,
                'filename' => $image->filename,
                'note_id' => $image->note_id,
                'updated_at' => strtotime((string)$image->updated_at),
                'created_at' => strtotime((string)$image->created_at)
            ];
        }
    }

I tried everything from str_replace to stripslashes on the filename but it did not work. Am I missing something I am doing wrong? When returning just the filename directly in the controller the response is correct.

0 likes
11 replies
Snapey's avatar

can you edit your post and put three ~ either side of your code block or indent each line by 4 spaces. we will then be able to see your problem better

1 like
Jogan555's avatar

Sorry did this on my phone was a bit hard to do this. I hope it is correct now

Snapey's avatar

sorry, I can't understand your problem? you built the file name with backslashes but then you say you want it with no backslashes?

Jogan555's avatar

No I did build it without backslashes like you can see above. I can also return the filename without backslashes inside the controller directly after the save but when transforming it keeps adding a backslash for every forwardslash. I tried adding stripslashes and str_replace but nothing happened.

bashy's avatar

You returning it in JSON? That will always escape characters. Not really sure what you want to do, you've given code but no examples of what happens.

Jogan555's avatar

Right. I want that the json looks for the filename like this projects/1/notes/images/2015-03-27_10:44:291.jpg and not like this projects/1/notes/images/2015-03-27_10:44:291.jpg I just noticed that it is not displayed here. Actually what it does it adds backslashes in front of every forwardslash.

Snapey's avatar

it doesn't help that the backslashes are being stripped from the forum! Your raw Json might be escaped but when you use it, it should be unescaped again How do you use the Json response?

Jogan555's avatar

I would use it in a AngularJS App to load for example images for a user and also inside an Android App.

bashy's avatar
bashy
Best Answer
Level 65

Yes, once you display it separately, it won't have the escaped forward slashes... Have you tried it in the app to display the image?

Jogan555's avatar

No actually not since I expected that it wont work sorry. I will test it.

Please or to participate in this conversation.