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?
Related
I'm using the below function:
class Image {
static function url($id, $type = 'maps') {
$path = UPLOAD_DIRECTORY . '/' . $type . '/';
$files = glob($path . $id . '.*');
if (count($files)) {
$ext = substr($files[0], strpos($files[0], '.') - strlen($files[5]));
return SERVER_URL . 'img/' . $type . '/' . $id . $ext;
} else {
return '';
}
}
}
It works fine when hosted on WAMP but when using on Unix running on nginx it doesn't display the image correctly because of the file path. It seems to add the physical path of the files in the URL so it thinks the patch of the file is http://localhost/demo/img/maps/20/public_html/demo/img/maps/20.png
On WAMP it displays correctly ie the URL is http://localhost/demo/img/maps/20.png
These are the defined variables:
define('SERVER_URL', 'http://localhost/demo/');
define('UPLOAD_DIRECTORY', dirname(__FILE__) . '/img');
id=20;
Physical location of the images are in /public_html/demo/img/maps/
How can I fix this on a unix OS.
Managed to work out a solution (btw I'm not a programmer).
Basically on the UNIX server the directory where the images were located had a dot in the path whereas on wamp it didn't.
So what I had to do is use the strrpost instead of strpost - this finds the position of the last occurrence of a substring in a string instead of the first occurance.
I'm writing a build/deploy script using a CLI php script.
Say I have a directory /environment and in it there are simply two broken symlinks.
I'm running glob(/environment/{,.}*). When I foreach over the glob, all I see are . and ... The symlinks never show up in the list.
How can you loop over a directory, detect broken symlinks, and unlink() them using PHP?
On a broken symlink is_link() returns true and file_exists() returns false.
Since glob() does not list broken symlinks, you have to list the contents in a different way.
Here is an example using scandir()
foreach(scandir($dir) as $entry) {
$path = $dir . DIRECTORY_SEPARATOR . $entry;
if (is_link($path) && !file_exists($path)) {
#unlink($path);
}
}
use realpath function:
foreach(scandir($dir) as $entry) {
$path = $dir . DIRECTORY_SEPARATOR . $entry;
if (!realpath($path)) {
#unlink($path);
}
}
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);
});
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;
We have a script, /scripts/ourscript.php and a file, /texts/elvis.txt.
How can we change contents of this file, when we run ourscript.php?
Use file_put_contents() method to set the contents of a file.
If you need just to save new data, you can do:
$elvis = 'Contents here';
$fileName = '..' . PATH_SEPARATOR . 'texts' . PATH_SEPARATOR . 'elvis.txt';
if (file_put_contents($fileName, $elvis) === false)
{
// Handle error here.
}
If, instead of saving data, you need to change existing data, do:
$fileName = '..' . PATH_SEPARATOR . 'texts' . PATH_SEPARATOR . 'elvis.txt';
$elvis = file_get_contents($fileName);
// Do changes to $elvis here.
if (file_put_contents($fileName, $elvis) === false)
{
// Handle error here.
}
Finally, if you need to append something new to existing contents, use:
$elvis = PHP_EOL . 'Contents to append to existing stuff here';
$fileName = '..' . PATH_SEPARATOR . 'texts' . PATH_SEPARATOR . 'elvis.txt';
// Noticed FILE_APPEND as third argument?
if (file_put_contents($fileName, $elvis, FILE_APPEND) === false)
{
// Handle error here.
}
While MainMa has given you a direct answer, I'll point you to:
http://php.net/manual/en/function.file.php
Since it seems that you might have more of these questions, which could have been easily answered by looking at the documentation.
Also by figuring things out with the help of the documentation you'll learn how to solve such problems on your own, you know independence is a nice thing to have :)