I've been having trouble properly outputting the directory for images in a slider using Custom Fields. The code I'm using is following:
<div class="container">
<?php
//path or directory where the images are stored
$directory = "echo get_post_meta($post->ID, 'directory', true)";
//read all files with a .jpg extension
$images = glob($directory . "*.jpg");
//output the required HTML code to display the images in the gallery
foreach($images as $image)
{
echo '<div class="content"><div><img src="'.$image.'" width="120" height="80" alt="this is a test" class="thumb" /></div></div>'."\n";
}
?>
</div>
The value I want to dynamically output is the $directory = "", where it would normally be something like $directory = "images/product1/". I have my custom field 'directory' set as images/product1/. Any ideas? Thanks for the help!
It looks like the issue is with this line:
$directory = "echo get_post_meta($post->ID, 'directory', true)";
will result in trying to glob like this:
glob("echo get_post_meta($post->ID, 'directory', true)*.jpg");
which is obviously invalid.
Instead you should actually be calling the get_post_meta() function
$directory = get_post_meta($post->ID, 'directory', true);
Related
The code I have should output a jpg from a list of files in a directory however it is not. I have trawled this site and tried different methods but not helped. I am a relative beginner at php so looking for any help at all.
I have tried using img src in the php code but I am trying to get the image to display within a Wordpress post so I cannot echo the img src within the script. I have tried file_get_contents and read file as well but it may be my lack of knowledge holding me back.
<?php
$imagepath = htmlspecialchars($_GET["image"]);
$imagenum = htmlspecialchars($_GET["num"]);
define('LOCALHOST', 'localhost' === $_SERVER['SERVER_NAME'] );
If(LOCALHOST){
define('PATH_IMAGES', 'this_path');
}else{
define('PATH_IMAGES', '../../../Images/');
}
$arrnum = $GLOBALS[imagenum] - 1;
$dirname = PATH_IMAGES . $GLOBALS[imagepath]."/";
$images = scandir($dirname);
rsort($images);
$ignore = Array(".", "..");
foreach($images as $curimg){
if(!in_array($curimg, $ignore)) {
header('Content-type: image/jpeg');
file_get_contents('$dirname$images[$arrnum]');
}
}
?>
Have you tried readfile(...); should read and output the file. In your example you are not outputting the image data
http://php.net/manual/en/function.readfile.php
I have been making a blog, but I have a problem. I can't display image from folders images. I need to display one image every time when name and comment put on the page. Every time another image and need to stay on page.
Here is link
http://slimhamdi.net/lina/demos/blog-post-dark.html?name=hhhhhhh&email=hh%40hotmail.com&comment=h&send=
I need same like this blog, I would like to avoid MySQL. Any help will be appreciated.
<?php
$images = array("user1.jpg", "user2.jpg", "user1.jpg");
$images = glob('images/*');
shuffle($images );
foreach($images as $image ) {
break;
}
?>
<img src="<?php echo "$image"; ?>" />
You are overwriting variables:
<?php
$images = array("user1.jpg", "user2.jpg", "user1.jpg");
$images = glob('images/*'); // Overwrites the previous $images
That way the $images in the foreach is something else.
There is also no need to use a break in the foreach loop
<?php
$images = array("user1.jpg", "user2.jpg", "user1.jpg");
shuffle($images );
foreach($images as $image ) {
echo '<img src="'.$image.'" />';
//echo '<img src="images/'.$image.'">';// With file path
}
why do you use break in foreach?
you should echo the img tag in foreach
if your images are in images folder, you shouldn't you glob, and instead you can concat it to path.
<?php
$images = array("user1.jpg", "user2.jpg", "user1.jpg");
shuffle($images);
foreach($images as $image ) {
echo "<img src='images/{$image}' />";
}
?>
This is the code I'm using to get and display images from a folder on my server:
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
$dirname = "$root/Folder/".$row['city']."/";
$images = glob($dirname."".$row['id']."#*.jpg");
foreach($images as $image) {
echo "<img src=\"".$image."\">";
}
The "normal" path of a picture is /Folder/Berlin/1#1.jpg. In the rendered HTML source code I can see PHP makes this link: /var/www/user_name/html/Folder/Berlin/1#1.jpg
But unfortunately the image doesn't get loaded.
What am I doing wrong?
Good day! I am trying to retrieve from image path with the name stored from database but I am struggling to display that image from folder and I'll explain you that later... here is my code.
in my controller
public function save()
{
$url = $this->do_upload();
$title = $_POST["title"];
$this->main_m->save($title, $url);
}
public function do_upload()
{
$type = explode('.', $_FILES["pic"] ["name"]);
$type = $type[count($type)-1];
$url = "./images/".uniqid(rand()).'.'.$type;
if(in_array($type, array("jpg","jpeg","gif","png")))
if(is_uploaded_file($_FILES["pic"]["tmp_name"]))
if (move_uploaded_file($_FILES["pic"]["tmp_name"], $url))
return $url;
}
in my view im trying to retrieve like this.
<?php foreach ($this->b->getalldata() as $row) {
echo '<li>'.
'<a class="ns-img" href="'.base_url("images/".$row->image).'".></a>'.
'<div class="caption">'.$row->title.'</div>'.
'</li>';
} ?>
in my model
public function save($title, $url)
{
$this->db->set('title', $title);
$this->db->set('image', $url);
$this->db->insert('slider');
}
the image stored to path folder and also the new name of the image stored to database but displaying the image like that is not working for me. my image path folder is "images" and the image name stored to database is "./images/new_name.jpg" not the same name as image that stored to the folder path. the image name that stored to folder path has no ./images/ only new_name.jpg...
how to display that image? someone tried that? help!
You need to explode image name from "./images/new_name.jpg".
$image_arr = explode("/", "./images/new_name.jpg");
echo $image_name = end($image_arr);
we used codeigniter upload library
$this->load->library('image_lib');
$config = array(
'upload_path' => APPPATH."upload/folder",
'allowed_types' => "gif|jpg|png|jpeg",
'overwrite' => TRUE,
'max_size' => "2048000"
);
$this->load->library('upload', $config);
$this->upload->do_upload('pic');
You need img HTML tag:
<img src="<?php echo base_url('images/' . $row->image);?>">
Try this
On view
<?php foreach ($this->b->getalldata() as $row) {
echo '<li>'.
'<a class="ns-img" href="#">'.base_url().'images/'.$row->image.'</a>'.
'<div class="caption">'.$row->title.'</div>'.
'</li>';
} ?>
You have error here .base_url("images/".$row->image).'n try to use site_url('images').'/'.$row->image
i hope you will get the proper path of your file.
I am trying to generate some HTML code to list some images for a slide show.
I arrived at the following idea:
function galGetTopPhotos()
{
//path to directory to scan
$directory = SITE_ROOT_PATH."/gallery/best/";
//get all files
$images = glob($directory . "*.*");
//print each file name
$ret = "";
$ret .= '<div id="myslides">';
foreach($images as $image)
{
$ret .= '<img src="'.$image.'" />';
}
$ret .= '</div>';
return $ret;
}
The problem is that it only works when I use root path for $directory...if I use URL it will not work. And it causes the images to not load. Here is what this code generates:
<div id="myslides">
<img src="D:/xampp/htdocs/mrasti/gallery/best/1.jpg" />
<img src="D:/xampp/htdocs/mrasti/gallery/best/10.jpg" />
</div>
So the question is, how to get the list of files so it generates img sources in http://127.0.0.1/.... format?
What I mean if I use the code like this it returns no file!
$directory ="http://127.0.0.1/mrasti/gallery/best/";
This looks like a job for PHP function basename. This takes a file path and returns only the final element of the path - in this case the actual name of the jpeg image.
You could amend your code so that it looks something like this:
$urlPath = "http://127.0.0.1/mrasti/gallery/best/";
...
...
foreach($images as $image)
{
$relative_path = $urlPath.basename($image);
$ret .= '<img src="'.$relative_path.'" />';
}
The above takes the path and appends the filename "example.jpg" to your image directory url
glob does only work for local files and not on remote files. Have a look here:
http://php.net/manual/en/function.glob.php
For remote files have a look here:
http://www.php.net/manual/en/features.remote-files.php
But i do not think that you need remote files. It seems like you want to go through a local directory and display this images.
Try something like this
...
$ret .= '<img src="http://127.0.0.1/my/path/'.basename($image).'" />';
...
You need to have some functionality to translate the file path on disk to the correct URI so that your browser can understand it.
In your specific case as outlined and with the exact data given in your question, the following could work:
foreach($images as $image)
{
$src = '/mrasti/gallery/best/'.substr($image, strlen($directory));
$ret .= '<img src="'.$src.'" />';
}