Need to bulletproof this image parser function
I've got a function below which reads the contents of wp-content/uploads and writes out all images that it finds there.
The problem is that it's reading off the blog title to determine the image path and when the blog title has a dot in it, there's trouble
Blog title is abc123.com
site url is abc123.com
test image filename is abc123-1.jpg
img tag SHOULD become:
<img src='http://abc123.com/wp-content/uploads/abc123-1.jpg' />
actual image tag written from the function below is:
<img src='http://abc123.com/wp-content/uploads/abc123.com-1.jpg' />
My question is, how did the ".com" get inserted into the filename???
Functi开发者_如何学JAVAon follows...
function get_images()
{
global $options;
foreach ($options as $value) {
if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { $$value['id'] = get_settings( $value['id'] ); }
}
if($cb_custom_images !== "")
{
echo $cb_custom_images;
}
else
{
$dir = 'wp-content/uploads/';
$url = get_bloginfo('url').'/wp-content/uploads/';
$imgs = array();
if ($dh = opendir($dir))
{
while (($file = readdir($dh)) !== false)
{
if (!is_dir($file) && preg_match("/\.(bmp|jpeg|gif|png|jpg|)$/i", $file))
{
array_push($imgs, $file);
}
}
closedir($dh);
} else {
die('cannot open ' . $dir);
}
foreach ($imgs as $idx=>$img)
{
$class = ($idx == count($imgs) - 1 ? ' class="xlast"' : '');
echo '<img src="' . $url . $img . '" alt="' .$img . '"' . $class . ' />';
}
}
}
Are you sure your test file is named abc123-1.jpg
? I just copied your code into a file and added dummy scaffolding, including a wp-content/uploads/abc123-1.jpg
file.
I got the correct result when I ran the script:
% php img.php
<img src="http://abc123.com/wp-content/uploads/abc123-1.jpg" alt="abc123-1.jpg" class="xlast" />
`
I think it is preg_match issue try to remove character after jpg
Is there a abc123.com-1.jpg file in that directory?
That could be the problem.
Sorry about this one guys, nothing wrong with the function. I had a legacy file that this file relied on that was incompatible. After updating that file, all is well.
Now I just have to figure this one out (assuming I've got any cred left after this one :-) Do we get mulligans around here?
精彩评论