I'm trying via php to pull all images from a directory and print them as < li < img src="url.. etc...
After doing a bit of research on SO, i found that the glob function should serve this purpose well, but i can't get it to work unfortunately. I think the problem is with me trying to echo the ID in the path? but being a bit of a newbie i can't seem to see my error.
<?
$dirname="images/companies/";
echo '$current['id']';
$images = glob($dirname."*.jpg");
foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}
?>
You have no dir separator between $dirname."*.jpg", so unless your $current['id'] ends in a slash, glob won't work.
Try replacing $dirname."*.jpg" with $dirname."/*.jpg".
Also: your $dirname is relative to the current PHP working directory. To make sure that this works regardless of where PHP was called from, use $dirname=dirname(__FILE__)."/images/companies/".$current['id']
Make sure thar $dirname is relative to the current path or use absolute paths, e.g.:
$dirname = $_SERVER['DOCUMENT_ROOT']."/../some/path/images/companies/";
Make sure that your path is not missing a slash, it would if $current['id'] had no trailing slash, that you had to write:
$images = glob($dirname."/*.jpg");
Thaths the problem when oyu just steal code, but dont understand what it does.
If you want to scan a directory, you can use scandir(), which returns an array containing all the files it found, e.g.
$files = scandir("your/path/");
To show them, you simply use print_r(), its ugly and not formatted, but it does its purpose, if you want more, use echo and html/css:
echo "<pre>";
print_r($files);
echo "</pre>";
No finished code, think a bit for yourself.
Related
I need to use $_SERVER['DOCUMENT_ROOT']; to scandir for files and folders to display for my nav menu.
Let's say my root directory is at /home/user/public_html/website/.
Here's how I echo the root directory:
echo $_SERVER['DOCUMENT_ROOT'];
it will display /home/user/public_html/website/
Here's how I echo a folder in the root directory:
echo $_SERVER['DOCUMENT_ROOT'].'about/';
it will display /home/user/public_html/website/about/
Question: How can I strip everything that it displays all the way up to the root folder and/or sub-folder.
Example: If I echo $_SERVER['DOCUMENT_ROOT'], I want it to display as / instead of the entire /home/user/public_html/website/ path.
and if I echo a sub-folder I want it to display as /about/ instead of /home/user/public_html/website/about/.
So, how can I get it to echo just the ending part of the $_SERVER['DOCUMENT_ROOT']; instead of the entire directory path?
I've tried dirname and basename but neither of those does what I need. I've thought of string replace but isn't there something much easier to add onto $_SERVER['DOCUMENT_ROOT'] that will just echo out the root folder or root folder plus sub-folder?
Update:
I ended up using the answer below from anant kumar singh but tweaking it using preg_split instead of explode because preg_split can take multiple string delimiters where as explode can take only 1.
$newArray = preg_split( " (-in/|-ca/|-co/) ", dirname($_SERVER['DOCUMENT_ROOT']) );
if(count($newArray)>1){
echo '/'.$newArray[1].'/';
}else{
echo '/';
}
If you know of a better way to accomplish what I'm trying to do, please post your answers below.
You can go with explode in the following manner:--
$newArray = explode('website/',$_SERVER['DOCUMENT_ROOT']);
if(count($newArray)>1){
echo "/".$newArray[1];
}else{
echo '/';
}
As you already said that preg_split is done the job for you:-
$newArray = preg_split( " (-in/|-ca/|-co/) ", dirname($_SERVER['DOCUMENT_ROOT']) ); if(count($newArray)>1){ echo "/".$newArray[1]."/"; }else{ echo '/'; }
NOTE:- Don't be panic.I am not going to take your credit. I just did it for the future peoples purpose. If you put it as an answer then i will remove it.
I'm assuming the easiest way to do this will be with regex, but I just can't seem to find clear information on regex. I'm a beginner and all the information I'm finding is confusing.
I need to find every image in an HTML file, insert the folder extension, and then overwrite the file. I know how to do everything but the replacement. From my understanding, the code should look something like this:
preg_replace("^\"(.jpg|.jpeg|.gif|.png)$"....)
But I don't understand where to go from there. I need to keep the original value of whatever is between those things and add something to the beginning of it, so for example "image.jpg" would become "images/image.jpg".
$img = "<a href=\"hello.jpg\" /><a href=\"asdf.png\" /><a href=\"xkcd.gif\" />";
$img = preg_replace("/\"(\w+\.(jpg|jpeg|gif|png))\"/","\"images/$1\"",$img);
echo $img;
Output: <a href="images/hello.jpg" /><a href="images/asdf.png" /><a href="images/xkcd.gif" />
The regex can be improved using lookarounds, but I think they are overkill (and will make it more complex).
I've got a small php script that will gather all files in a directory. Futhermore, I'm cleaning through this array of names to skip over the ones I don't want:
$dirname = "./_images/border/";
$border_images = scandir($dirname);
$ignore = Array(".", "..");
foreach($border_images as $border){
if(!in_array($border, $ignore)) echo "TEST".$border;
}
This directory would contain images that I want to find. Amongst these images, there will be a thumbnail version and a full-size version of each image. I'm planning to have each image either labeled *-thumbnail or *-full to more easily sort through.
What I'm trying to find is a way to, preferably with the $ignore array, add a wildcard string that will be recognized by a check condition. For example, adding *-full in my $ignore array would make that files with this tag, anywhere in their filenames, would be ignored. I'm pretty sure the in_array wouldn't accept this. If this isn't possible, would using regular expressions be possible? If so, what would my expression be?
Thanks.
You're probably looking for php's function glob()
$files_full = glob('*-full.*');
There is a better way to do this known as glob().
Take a look at glob function.
glob — Find pathnames matching a pattern
PHP beginner's question.
I need to keep image paths as following in the database for the admin backend.
../../../../assets/images/subfolder/myimage.jpg
However I need image paths as follows for the front-end.
assets/images/subfolder/myimage.jpg
What is the best way to change this by PHP?
I thought about substr(), but I am wondering if there is better ways.
Thanks in advance.
you should save your image path in an application variable and can access from both admin and frontend
If ../../../../ is fixed, then substr will work. If not, try something like this:
newpath=substr(strpos(path, "assets"));
It might seem like an odd choice at first but you could use ltrim. In the following example, all ../'s will be removed from the beginning of $path.
The dots in the second argument have to be escaped because PHP would treat them as a range otherwise.
$path = ltrim('../../../../assets/images/subfolder/myimage.jpg', '\\.\\./');
$path will then be:
assets/images/subfolder/myimage.jpg
I suggest this
$path = "../../../../assets/images/subfolder/myimage.jpg";
$root = "../../../../";
$root_len = strlen($root);
if(substr($path, 0, $root_len) == $root){
echo substr($path, $root_len);
} else {
//not comparable
}
In this way you have a sort of control on which directory to consider as root for your images
I have the following code that selects all the different template files out of a folder... The file names I have are:
template_default.php
template_default_load.php
template_sub.php
template_sub_load.php
I only want to select the ones without the _load in the file name so I used this code:
preg_match('/^template_(.*)[^[_load]]{0}\.php$/i', $layout_file, $layout_name)
The code works fine except it cuts the last character off the result... Instead of returning default or sub when I echo $layout_name[1], it shows defaul and su...
Any ideas what is wrong with my code?
This part is totally up the creek:
[^[_load]]{0}
This is the regex you want:
/^template_(.*)(?<!_load)\.php$/i
You'll have to use negative assertions. (read below why not)
preg_match('/^template_(.*?)(?!_load)\.php$/i', $layout_file, $layout_name)
Edit: come to think of it, that regexp won't actually work because "_load" will be consumed by the .*? part. My advice: don't use preg_match() to capture the name, use it to skip those that end with _load, e.g.
foreach (glob('template_*') as $filepath)
{
if (preg_match('#_load\\.php$', $filepath))
{
// The file ends with _load
}
$filename = basename($filepath);
$layout_name = substr($filename, strlen('template_'));
}