In my web application I need to display the images uploaded when creating in the view. The uploaded images are stored in /images/xxx folder . I want in such a way that as soon as the user uploads the image, it should appear in the final view. I know we should create an array reference pointing to the images/xxx folder in config/main.php . But I dont know how to refer to the individual images in the folder and print them in the final view..
My code for params in config/main.php
'params'=>array(
'adminEmail'=>'webmaster#example.com',
'vegImageLoc'=>'images/xxx',
),
);
I dont know how to refer to the individual array elements in the final view and print them in the final view. Any body help me how to proceed.
I dont know why you think you need to change anything in config, maybe I'm missing something, but if you want to show all images in a folder like images/xxx , I recommend to do this :
foreach (glob("images/xxx/*") as $img) {
echo "<img src ='".Yii::app()->request->baseUrl."/".$img."' />";
}
Or if you want to get the images from database, get it and again make a foreach like above.
$image = $model->getImages(); // function to get image list stored in db. return as array
Pass $image to view file.
In your view file. you can do as below:
foreach ($image as $img) {
echo "<img src ='".Yii::app()->request->baseUrl."/".Yii::app()->params["vegImageLoc"]."/".$img."' />";
}
Related
So I have a few images in the server (public_html/img/profile_pictures/).
This is how I currently set the image:
echo "<img src='img/profile_pictures/main_photo.png'/>";
The main_photo can change each day, but if it changes to main_photo.jpg insted, it wont show (because the extension is hardcoded on that line(.png)). Is it possible to display the photo without knowing the extension for the image file?
If you want a PHP code, then try this. This code will look for main_photo.* inside your folder and automatically set the extension upon finding one.
Remember to set the path properly
<?php
$yourPhotoPath = "img/profile_pictures/";
foreach (glob($yourPhotoPath.'main_photo.*') as $filename) {
$pathInfo = pathinfo($filename);
$extension = $pathInfo['extension'];
$fileName = chop($pathInfo['basename'], $extension);
echo "<img src='".$yourPhotoPath.$fileName.$extension."'/>";
}
?>
if a Photo isn't loaded, it's width and size is null.
Although I would advise you to write a class that checks and loads images, I get a feeling you want a simple solution. so, given by the premise that the photo is either
<img src='img/profile_pictures/main_photo.png'/>
or
<img src='img/profile_pictures/main_photo.jpg'/>
and that neither this path nor this filename ever changes and in the folder is only one picture,
you could simply echo both.
The img of the one that is empty will not be shown.
A better way was to write a class that loads your photo and checks if the photo is really there, like
$path = 'img/profile_pictures/main_photo.png';
if(!file_exists('img/profile_pictures/main_photo.png'))
{
//use the jpg path
$path = 'img/profile_pictures/main_photo.jpg';
}
You can ofc just inline this if case, but it's bad practise to intermix buisinesslogic and format logic, so I advice you to write a class for it.
I have the following shortcode which gets the images of that post (that will be in a folder named as the post's id ) and displays them echoing as many tag images as needed.
session_start();
$postID = $_SESSION['post_ID'];
$upload_dir = wp_upload_dir();
$path = $upload_dir['basedir'].'/'.'gallery/'.$postID;
if(file_exists($path)) {
$pathFile = $path.'/*';
$images = glob($pathFile);
if(!empty($images)) {
foreach($images as $image){
$alt = end(explode('/', $image));
echo '<img src="'.$image.'" alt="'.$alt.'" />';
}
} else echo 'No hay imágenes.';
} else echo 'No hay imágenes.';
?>
The problem is that the images aren't displayed, i get the followin error in the brower's console GET https://path-to-theme/wp-content/uploads/gallery/1658/wwg.jpg 404, per every image and i get this icon:
image
I have no idea why my images aren't displayed the images, the path is correct, the images are there, complitely no idea what I'm missing. It's not a permissions problem as i tried giving 755 permissions to the directories starting from /uploads/.. and so on including the images.
I'm going to assume that the URL you reference in your question is actually https://yourdomain/wp-content/uploads/gallery/1658/wwg.jpg and not https://path-to-theme/wp-content/uploads/gallery/1658/wwg.jpg. Right?
If that's the case, then it should be able to pull open that file. The first thing I'd check is whether you actually have the file located where you think it is: /wp-content/uploads/gallery/1658/
If the file is actually in that folder, and you can access it from your browser at https://yourdomain/wp-content/uploads/gallery/1658/wwg.jpg then my next suggestion would be to check the permission of each of the folders leading up to the file and make sure they are set to 755, and then make sure your file is set to 644.
Here are instructions on changing file permissions.
Let me know if that helps?
I fixed the problem. The issue wa sthat i wasn't uploading the images as wp wants me to, therefore the images, even they were in the folder, couldn't be displayed. So I had to change the way i uploaded them, by using wp functions and specifying the mime_type etc so wp could store their metadata in it's db in order to be able to acknowledge them and display them in a future.
Im new at CodeIgniter and I want to read some data from mysql database using CodeIgniter. Data I have stored is an image.
I have this folder structure in my aplication:
1.ci
application
controllers
views
models
upload_images (Here I have the images stored as jpg files)
When I create this code the image shows up and it is Ok:
echo ' <img src=" http://localhost/ci/'.$row->book_img.' " />'
While when I try to output images from the database in another way like below it outputs the link instead of the image. I want to use html helper and I have loaded it.
echo ' img(' http://localhost/ci/'.$row->book_img.')';
the image doesn't appear. Do I do something wrong in the second way? Thanks!
The uploads folder should be out side of application folder because the htaccess in the application folder blocks access I also would auto load the url helper
config > autoload.php
$autoload['helper'] = array('url');
Folder Structure For Upload Images
application
system
upload_images <-- folder permission 0777
index.php
then make sure you are getting the image correct on controller var_dump($row->book_img)
Option : 1
$data['img'] = $row->book_img;
$this->load->view('your_view', $data);
View
<img src="<?php echo base_url('upload_images/' . $img);?>" />
Option: 2
$this->load->helper('html');
$data['img'] = img('upload_images/' . $row->book_img); ;
$this->load->view('your_view', $data);
View
<?php echo $img;?>
And set your base url
$config['base_url'] = 'http://localhost/yourproject/';
It all depends on what what $row->book_img is returning. If it is only returning the image name, you must first make sure that is has the extension at the end whether it be name.jpg or name.png, etc.
if it is correct you simply use:
$img_src = base_url () . '/PATH_TO_IMAGE_FOLDER/' . $row->book_img;
echo "<img src='$img_src'/>";
If it is returning the full path then:
$img_src = base_url () . $row->book_img;
echo "<img src='$img_src'/>";
Try calling it like:
echo img('http://localhost/ci/'.$row->book_img);
Source
You have syntax errors in your second code snippet and you probably don't have error reporting enabled (which is a bad idea during development). Check here for help with setting that up.
I'm making a simple file listing function in PHP, just to recursively list all the files in a directory.
The function I have right now looks something like this:
<?php
function listFiles($dir){
echo "<ul class=\"filebrowser\">";
$dirCont = scandir($dir); // scan the whole directory
sortFoldersFirst($dirCont,$dir); // sort alphabetically, folders first
foreach ($dirCont as $value) {
if (!in_array($value,array(".","..",".deleted"))){ // don't list the parent folder, current folder, or the (hidden) .deleted folder
if (is_dir($dir . DIRECTORY_SEPARATOR . $value)){ // if it's a folder
echo "<h3 class=\"filebrowser\">" . $value . "</h3>"; // show the folder name
listFiles($dir . DIRECTORY_SEPARATOR . $value); // do the same again for the child folder
} else {
echo "<li>".$value."</li>"; // a link to the file
}
}
}
echo "</ul>";
}
?>
This gives me something like this:
This technique works just fine, however, I'd like to store a little more information about the files, like the author, comments, maybe a link to a thumbnail etc.
I'd like to get rid of the 'real' directory structure, and store everything in a database. (Just all files in 1 big folder, and links to the files in the database, not the actual files)
How can I store this information in the database, and how can I get it back out, without changing the result in the browser.
In other words: how can I get the tree structure in and out of the database, and how do I recursively loop through it.
I found this answer.
It shows how to make a database tree structure by saving the parent folder's id for every file/folder. This makes it easy to get the full path of a file, but it seems a bit less convenient to list all contents of a folder.
Is there a better solution to this problem?
Thanks in advance,
Pieter
I'm trying to generate images on my website by pulling a thumbnail from one folder and the actual image from another. I have no idea what I am doing. This is what I have so far:
<?php
$thumbdirname = "images/thumbs/";
$imgdirname = "images/";
$mainimages = glob($imgdirname."*.{jpg,png,gif}", GLOB_BRACE);
$imagethumbs = glob($thumbdirname."*.{jpg,png,gif}", GLOB_BRACE);
foreach($imagethumbs as $image) {
echo '<a class="imageLink" href="$mainimages" data-lightbox="logo" data-title="Vivid Logo"><img src="'.$image.'"</a> ';
}
?>
I know that it will not work with just "$mainimages" for href, but I haven't been able to figure out what to put there. As is, it will pull up the right thumbnail, but not link to the full associated image.
I tried to put another foreach statement in, but I get four results from my two pictures (and their thumbnails). Which makes sense sense it is showing one of each combination:
(img1,thumb1),(img1,thumb2),(img2,thumb1),(img2,thumb2)
How should I change this to get it playing nicely?
You don't seem to be outputting from $mainimages so I'm going to ignore this for now.
You PHP code seems to be OK from initial glance. Only thing that is a bit iffy is that you are not closing you image tag.
If that does not help try counting the image array/object.
If that returns 0 try the below code.
// Find all files in that folder
$files = glob('images/gallery1/*');
// Display images
foreach($files as $file) {
echo '<img src="' . $file . '" />';
}
Hope that helps