I have a very weird issue trying to do a simple str_replace in php in a wordpress application.
I am trying to create a path to an image using wordpress functions to get the site path and the resource path. Below are 3 methods that I tried, all giving results that I don't understand.
Original method: (This worked on my own localhost but when in prod, the admin side gives a different $fullpath to the front end side)
$site_url = get_site_url();
$homepath = get_home_path();
$filepath = str_replace($site_url.'/', '', $attachment->image_src_large[0]);
$fullpath = $homepath.$filepath;
Output on Front End was a $fullpath of
string(114) "/usr/www/users/currentuser/wp-content/uploads/2017/10/image1-2962-1024x684.jpg"
But when in Admin End was (Note the double forward slash)
string(114) "/usr/www/users/currentuser//wp-content/uploads/2017/10/image1-2962-1024x684.jpg"
I noticed that on the production server $attachment->image_src_large[0]
didn't include the domain as it had on the dev server. So with this in mind, and in an attempt to solve the double slash on the Admin End, I tried numerous methods as detailed below - This is when things started to get a bit weirder:
2.
$site_url = get_site_url();
$homepath = get_home_path();
$filepath = $attachment->image_src_large[0];
$fullpath = str_replace("//", "/", $homepath.$filepath);
//var_dumps Give
//FRONT END
//$attachment->image_src_large[0] - string(89) "/wp-content/uploads/2017/10/image1-2962-1024x684.jpg"
//$filepath - string(114) "/usr/www/users/currentuser/https:/www.currentdomain.co.za/wp-content/uploads/2017/10/image1-2962-1024x684.jpg"
//ADMIN (Gave http instead of https)
//$attachment->image_src_large[0] - string(84) "/wp-content/uploads/2017/10/image1-2962-1024x684.jpg"
//$filepath - string(109) "/usr/www/users/currentuser/http:/currentdomain.co.za/wp-content/uploads/2017/10/image1-2962-1024x684.jpg"
$site_url = get_site_url();
$homepath = get_home_path();
$filepath = str_replace('/', '', $attachment->image_src_large[0]);
$fullpath = $homepath.$filepath;
//var_dumps Give
//$attachment->image_src_large[0] - string(51) "/wp-content/uploads/2017/10/image1-1024x684.jpg"
//$filepath - NULL
3.
$site_url = get_site_url();
$homepath = get_home_path();
$filepath = $attachment->image_src_large[0];
$fullpath = $homepath.$filepath;
$finalpath = str_replace("//", "/", $fullpath);
$fullpath - string(115) "/usr/www/users/currentuser//wp-content/uploads/2017/10/image-1-1024x684.jpg"
$finalpath - string(114) "/usr/www/users/currentuser/https:/www.currentdomain.co.za/wp-content/uploads/2017/10/image-1-1024x684.jpg"
The weird thing here was the size of the $finalpath string vs $fullpath sting, it looks like the str_replace works but when php reads the string it replaces / with the domain
4. Tried escaping the forward slash to be inserted
...
$finalpath = str_replace("//", "\/", $fullpath);
//$fullpath - string(110) "/usr/www/users/currentuser//wp-content/uploads/2017/10/image-1-1024x684.jpg"
//$finalpath - string(110) "/usr/www/users/currentuser/http:\/currentdomain.co.za/wp-content/uploads/2017/10/image-1-1024x684.jpg"
The output that I am looking for is this - string(114) "/usr/www/users/currentuser/wp-content/uploads/2017/10/image1-2962-1024x684.jpg"
I can't figure this one out! Anybody able to help, I would greatly appreciate it!