0

I am using a filemangement system and to protect the url. Manipulation of the url string is not allowed and leads to killing the page. I want to make an exception for that when I delete files.

When deleting files, the url string always looks like this:

example.com/sfm?delete=uploads/sfm/c4ca4238a0b923820dcc509a6f75849b/folder1 

or

example.com/sfm?delete=uploads/sfm/c4ca4238a0b923820dcc509a6f75849b/file.jpg 

or

example.com/sfm?delete=uploads/sfm/c4ca4238a0b923820dcc509a6f75849b/folder1/file.jpg

So the minimum of the string contains always:

example.com/sfm?delete=uploads/sfm/c4ca4238a0b923820dcc509a6f75849b

( the hash is a md5 hash of a userid)

So how can I grab that minimum url string?

2
  • 1. Is the hash always the same size? 2. Does uploads/sfm/ always come? Commented May 5, 2016 at 14:32
  • yes the hash has always the same number of characters and also uploads/sfm is always in the string Commented May 5, 2016 at 14:34

2 Answers 2

1

Use implode, array_slice, explode.

Explode will extract the string to array, array slice cut the array upto 4th item, and implode will grab the array items to a new string.

$str = 'example.com/sfm?delete=uploads/sfm/c4ca4238a0b923820dcc509a6f75849b/folder1 ';

echo $output = implode('/', array_slice(explode("/", $str), 0, 4)); //example.com/sfm?delete=uploads/sfm/c4ca4238a0b923820dcc509a6f75849b
Sign up to request clarification or add additional context in comments.

4 Comments

What does array_slice do here?
array_slice takes the selected parts of an array. here takes the index from 0 to 4.
Already you are limiting the explode to 4 right? I mean, I just noticed. You can actually limit it to 4 using explode("/", $str, 4).
may be, but i did it this way. i will try that later.
1

I would just use explode(). Even if the hash is not of the same size, if you follow the same structure, I would just do this:

$delete = explode("/", $_GET["delete"]);
print_r($delete);

Outputs to:

Array
(
    [0] => uploads
    [1] => sfm
    [2] => c4ca4238a0b923820dcc509a6f75849b
    [3] => folder1
)

The $delete[2] will give me the hash. I guess this would be the best way, even if the URL changes.

Comments

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.