Trying to turn this:
href="/wp-content/themes/tray/img/celebrity_photos/photo.jpg"
into:
href="/img/celebrity_photos/photo.jpg"
So I'm simply trying to remove /wp-content/themes/tray/ from the url.
Here's the plug in's PHP code that builds a variable for each anchor path:
$this->imageURL = '/' . $this->path . '/' . $this->filename;
So I'd like to say:
$this->imageURL = '/' . $this->path -/wp-content/themes/tray/ . '/' . $this->filename;
PHP substr()? strpos()?
Given that:
$this->imageURL = '/' . $this->path . '/' . $this->filename;
$remove = "/wp-content/themes/tray";
This is how to remove a known prefix, if it exists:
if (strpos($this->imageURL, $remove) === 0) {
$this->imageURL = substr($this->imageURL, strlen($remove));
}
If you are certain that it always exists then you can also lose the if condition.
This is one option:
$h="/wp-content/themes/tray/img/celebrity_photos/photo-on-4-6-12-at-3-23-pm.jpg";
$prefix="/wp-content/themes/tray/";
print str_replace($prefix, "/", $h, 1);
It suffers from one major flaw, which is that it doesn't anchor itself to the left-hand-side of $h. To do this, you'd either need to use a regular expression (which is heavier on processing) or wrap this in something that detects the position of your prefix before running the str_replace().
$h="/wp-content/themes/tray/img/celebrity_photos/photo-on-4-6-12-at-3-23-pm.jpg";
$prefix="/wp-content/themes/tray/";
if (strpos(" ".$h, $prefix) == 1)
$result = str_replace($prefix, "/", $h, 1);
else
$result = $h;
print $result;
Note this important element: the prefix ends in a slash. You don't want to match other themes like "trayn" or "traypse". Beware writing things for just your specific use case. Always try to figure out how code might break, and program around problematic hypothetical use cases.
Try this :
$href = str_replace("/wp-content/themes/tray","",$href);
Or in your specific case, something like this :
$this->imageURL = '/' . str_replace("/wp-content/themes/tray/","",$this->path) . '/' . $this->filename;
Related
So, the problem is in this line
$imageString = file_get_contents($image_url);
with urls that have space character it doesn't work. But if I make
$imageString = file_get_contents(urlencode($image_url));
Nothing works.I keep receiving false in the variable.
the ulr is of the kind:
https://s3-eu-central-1.amazonaws.com/images/12/Screenshot from 2016-04-28 18 15:54:20.png
use this function
function escapefile_url($url){
$parts = parse_url($url);
$path_parts = array_map('rawurldecode', explode('/', $parts['path']));
return
$parts['scheme'] . '://' .
$parts['host'] .
implode('/', array_map('rawurlencode', $path_parts))
;
}
echo escapefile_url("http://example.com/foo/bar bof/some file.jpg") . "\n";
echo escapefile_url("http://example.com/foo/bar+bof/some+file.jpg") . "\n";
echo escapefile_url("http://example.com/foo/bar%20bof/some%20file.jpg") . "\n";
i'v faced the same problem and if you search about it you will see all the people tell you to use urlencode(), but No!! urlencode() wont work in this situation...
i used the #Akram Wahid answer and that work perfectly so i recommend it to use for file_get_contents().
and if you wonder what escapefile_url() does in #Akram Wahid answer here little explain for it:
Simply he take the url apart as array and then he use rawurlencode() to encode all parts that contain special characters without the main domain like (http://example.com).
so what the deference?!! here example uses urlencode() and escapefile_url() to clarify this
echo escapefile_url("http://example.com/foo/bar bof/some file.jpg") . "<br>";
// http://example.com/foo/bar%20bof/some%20file.jpg
echo urlencode("http://example.com/foo/bar bof/some file.jpg") . "<br>";
// http%3A%2F%2Fexample.com%2Ffoo%2Fbar+bof%2Fsome+file.jpg
If you want to apply #Akram Wahid's solution to URLs that may also contain GET arguments then an updated version would be this:
function escapefile_url($url){
$parts = parse_url($url);
$path_parts = array_map('rawurldecode', explode('/', $parts['path']));
return
$parts['scheme'] . '://' .
$parts['host'] .
implode('/', array_map('rawurlencode', $path_parts)) .
(isset($parts['query']) ? '?'.rawurldecode($parts['query']) : '')
;
}
I need to check if $string exists in one of the files in a folder. Below is what I have, but it's obviously not working. What am I missing?
foreach (glob($path . 'foo/bar/*.*') as $file) {
if (strpos(file_get_contents($file), $string) !== false) {
//** found
} else {
//** not found
}
}
Are you sure all your files include an extension? You could try
glob($path . 'foo/bar/*')
and see if that works.
Also, if you're using Windows you should use a backslash (\) instead of a forward slash (/). You could use the DIRECTORY_SEPARATOR constant to let PHP automate it for you.
glob($path . 'foo' . DIRECTORY_SEPARATOR . 'bar' . DIRECTORY_SEPARATOR . '*.*')
Are you also sure that $path ends with a slash?
I am trying to use regex pattern to search for files.
Directory path is:
$css_dir = MY_PLUGIN_DIR.'/source/css/';
Css filename starts with:
$css_prefix = 'hap_css_';
and ends with:
'.css'
With some amount of unknown characters in between.
I dont know how to construct regex pattern with variable and how to construct file exist with regex.
Thank you!
You can use glob():
$files = glob($css_dir . $css_prefix . '*.css');
However, you have to roll your own DirectoryIterator based solution for more complex filtering:
$dir = new DirectoryIterator($css_dir);
$pattern = '/^' . preg_quote($css_prefix) . '.+\\.css$' . '/';
foreach ($dir as $fileInfo) {
if (preg_match($pattern, $fileInfo->getBaseName())) {
// match!
}
}
(One could also integrate a RegexIterator):
The use of scandir() is possible as well:
$pattern = '/^' . preq_quote($css_prefix) . '.+\\.css$' . '/';
$files = array_filter(scandir($css_dir), function ($filename) {
return preg_match($pattern, $filename);
});
I've been trying to create a directory following a specific structure, yet nothing appears to be happening. I've approached this by defining multiple variables as follows:
$rid = '/appicons/';
$sid = '$artistid';
$ssid = '$appid';
$s = '/';
and the function I've been using runs thusly:
$directory = $appid;
if (!is_dir ($directory))
{
mkdir($directory);
}
That works. However, I want to have the following structure in created directories: /appicons/$artistid/$appid/
yet nothing really seems to work. I understand that if I were to add more variables to $directory then I'd have to use quotes around them and concatenate them (which gets confusing).
Does anyone have any solutions?
$directory = "/appicons/$artistid/$appid/";
if (!is_dir ($directory))
{
//file mode
$mode = 0777;
//the third parameter set to true allows the creation of
//nested directories specified in the pathname.
mkdir($directory, $mode, true);
}
This should do what you want:
$rid = '/appicons/';
$sid = $artistid;
$ssid = $appid;
$s = '/';
$directory = $rid . $artistid . '/' . $appid . $s;
if (!is_dir ($directory)) {
mkdir($directory);
}
The reason your current code doesn't work is due to the fact you're trying to use a variable inside a string literal. A string literal in PHP is a string enclosed in single quotes ('). Every character in this string is treated as just a character, so any variables will just be parsed as text. Unquoting the variables so your declarations look like the following fixes your issue:
$rid = '/appicons/';
$sid = $artistid;
$ssid = $appid;
$s = '/';
This next line concatenates (joins) your variables together into a path:
$directory = $rid . $artistid . '/' . $appid . $s;
Concatenation works like this
$directory = $rid.$artistid."/".$appid."/"
When you're assigning one variable to another, you don't need the quotes around it, so the following should be what you're looking for.
$rid = 'appicons';
$sid = $artistid;
$ssid = $appid;
and then...
$dir = '/' . $rid . '/' . $sid . '/' . $ssid . '/';
if (!is_dir($dir)) {
mkdir($dir);
}
I have this PHP case statement
switch ($parts[count($parts) - 1]) {
case 'restaurant_pos':
include($_SERVER['DOCUMENT_ROOT'] . '/pages/restaurant_pos.php');
break;
case 'retail_pos':
include($_SERVER['DOCUMENT_ROOT'] . '/pages/retail_pos.php');
break;
.....
}
Which works great but I have many many files (like 190) and I would love to know if there is a way to make this case statement many work with anything so I dont have to do 190 case conditions. I was thinking I can use the condtion in the case and maybe see if that file is present and if so then display and if not then maybe a 404 page but i was not sure a good way to do this...any ideas would help alot
You can predefine file names in an array and then use in_array in order to check name's existence:
$files = array('restaurant_pos', 'retail_pos', ......);
$file = $parts[count($parts) - 1];
if (in_array($file, $files)) {
include($_SERVER['DOCUMENT_ROOT'] . "/pages/$file.php");
}
If it's not user input, you can do it like
$include = $parts[count($parts) - 1];
if ($include) {
if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/pages/'.$include.'.php')){
include $_SERVER['DOCUMENT_ROOT'] . '/pages/'.$include.'.php';
}
}
repeating, don't do this if $include is being filled from user's input !
This is a simple implementation without security checks:
$file=$_SERVER['DOCUMENT_ROOT']."/pages/".$parts[count($parts) - 1].".php";
if(file_exists($file)) include $file;
else show404();
To make it more secure for example you can remove slashes from $parts[count($parts) - 1]
Check that the file exists, and then include it.
Note that you MUST validate the contents of $page to be sure it doesn't include a path like /../../../../ to attempt to read somewhere else on your filesystem if this is to be user input.
If you know, for example that all your paths will be alphanumeric with underscores, you could do:
$page = $parts[count($parts)] - 1;
if (preg_match('/^[A-Z0-9_]+$/i', $page)) {
// it's okay, so include it.
if (file_exists($_SERVER['DOCUMENT_ROOT'] . "/pages/$page.php") {
include($_SERVER['DOCUMENT_ROOT'] . "/pages/$page.php");
}
}
Why not something like this?
$include_file = $_SERVER['DOCUMENT_ROOT'] . '/pages/' . $parts[count($parts) - 1] . '.php';
if (file_exists( $include_file ))
{
include( $include_file );
}
if (file_exists($path = $_SERVER['DOCUMENT_ROOT'].'/pages/'.$parts[count($parts) - 1].'.php')
{
include $path;
}
Another approach would be to check if the given file really exists in a particular directory:
$file = $_SERVER['DOCUMENT_ROOT'] . '/' . basename($parts[count($parts) - 1]) . '.php';
if (is_file($file)) include($file);