Reputation: 23
I am trying to extract a filepath of an image from a MySQL database and turn it into a URL. However when I pass it through str_replace no changes are been made. I have set up a test document with a path preset and it works without any problems.
My code that extracts the path from the database is
$user_image = get_web_path($row['user_pic_path']);
where
function get_web_path($file_system_path) {
return str_replace(var_dump($_SERVER['DOCUMENT_ROOT']), '', var_dump($file_system_path));
}
editted
My test code is:
<?php
echo "DOCUMENT_ROOT: {$_SERVER['DOCUMENT_ROOT']}";
$image_sample_path = "C:/wamp/www/website/images/image123.jpg";
$web_image_path = str_replace($_SERVER['DOCUMENT_ROOT'], '', $image_sample_path);
echo "<br /><br />CONVERTED PATH: {$web_image_path}";
?>
Which outputs:
/website/images/image123.jpg"
However when extracting the value from the database, C:/wamp/www from the 'user_pic_path' field isn't being replaced.
var_dump('DOCUMENT_ROOT') gives
string 'C:/wamp/www' (length=11)
var_dump($file_system_path)) gives
string 'C:/wamp/www/website/images/image123.jpg' (length=39)
Upvotes: 0
Views: 586
Reputation: 786091
Why do you have curly braces around $_SERVER['DOCUMENT_ROOT']
?
Try replacing that with:
str_replace($_SERVER['DOCUMENT_ROOT'], '', $file_system_path);
Upvotes: 1