php file exists returns nothing always - CodeIgniter - codeigniter

I'm wanting to display images. The user can add up to 7 images, but they can also add less than 7 if needed.
I searched around and saw that file_exists() is the function I'm after, but I've tried a couple of ways and it always gives me no image.
Here's one way I tried:
<?php if(file_exists(base_url() . "photos/" . $p['p_id'] . "_5.jpg")):?>
<div class="single">
<a href="<?php echo base_url();?>photos/<?php echo $p['p_id'];?>_5.jpg" rel="lightbox[image]">
<img src="<?php echo base_url();?>photos/<?php echo $p['p_id'];?>_5.jpg" style="width:100px; height:100px;"/>
</a>
</div>
<?php endif; ?>
Then I searched around and found out that its not a good idea to define the path inside the function, so I defined it outside:
<?php $path = base_url() . "photos/" . $p['p_id'] . "_";?>
<?php $img4 = $path . "4.jpg";?>
<?php if(file_exists($img4)):?>
which didn't work either.I'm testing with 4 images being in the folder 'photos' so I know there should be 4 images being displayed. Any ideas on how I should fix it would be great. Thanks

The function file_exists accepts file path as its parameter and not the url like what you're using now which is CodeIgniter's base_url().
You should use the FCPATH constant which points to your codeigniter app's base directory.
This is what you should assign to your path (depending on where your image files are located in your application directory):
<?php $path = FCPATH . "htdocs/images/" . $product[0]['product_id'] . "_";?>
But for some reason if really you need to use the base_url(). You can use get_headers() which checks the headers returned by the url in an associative array and false on failure. From the headers returned you can determine whether the image url is valid or not. More about get_headers().
Hope it helps.
Cheers,
Ardy

File exists requires a directory path not a URL, a URL will always return false.
<?php
for($i=1; $i < 7; $i++):
{
$img = $path . $i . ".jpg";
$url=getimagesize($img);
if(!is_array($url))
{
?>
<div class="single">
<a href="<?php echo base_url();?>images/<?php echo $product[0]['product_id'];?>_<?php echo $i; ?>.jpg" rel="lightbox[productimage]">
<img src="<?php echo base_url();?>images/<?php echo $product[0]['product_id'];?>_<?php echo $i; ?>.jpg" style="width:100px; height:100px;"/>
</a>
</div>
<?php
}
}
?>
Be forewarned this is a touch on the slow side, but it does work with URL's on the server.

Related

How to compare current_url with site_url in codeigniter?

I have used something like that-
<?php if(current_url() == site_url()): ?>
...
...
<?php endif; ?>
But it's not working. I think there's something that I missed.
If you simply want to check if you're on the homepage of your site try this:
<?php if(strcmp(uri_string(),'')==0) : ?>
We are on the home page.
<?php endif; ?>
This solution will still work regardless of whether the base url is set correctly or not, unlike comparing base_url() and current_url().
Ensure that you have loaded the url helper either in your constructor or via autoloading.

show welcome message only on index.php [CI]

I make custom welcome message in codeigniter script, but i want show only on index.php
I have
<div class="welcome">
<?php echo getConfigSetting('config_welcome') ?>
</div>
This message show in header,on all page. I want show only on homepage (index.php)
I already tried this code but give me a blank page
<?php if( $this->uri->segment(1) == 'home' || $this->uri->segment(1) = '' ): ?>
Affordable Business Websites Done Fast…
"Just find a website you like and we will customize it to your business needs, easy as 123!"
Start Search
</div>
</div>
Any sugestion?
Thank you
you could try to use the CI helper URL and grab the first segment of the url to check whta page we are on.
example:
<?php if( $this->uri->segment(1) == 'home' || $this->uri->segment(1) = '' ): ?>
<div class="welcome">
<?php echo getConfigSetting('config_welcome') ?>
</div>
<?php endIf; ?>
of course you would want to change the "home" to the name of your homepage name you see in the url, or the default controller/method name.
You will also want to make sure you autoload the url_helper in the config/autoload.

display no image if doesnt exist codeigniter

All i want to do is display an image if the image exist if not display blank but i cant seem to figue it out.
Im storing my image path file in my database, in the column called, file and path is stored like this:
/images/file_name
now if there isnt an image being posted it stores /images/
if ($news_item['file'] = '/images/11.jpg') { ?>
<img src="<?php echo base_url(); echo $news_item['file'] ?>">
} else {
echo '';
}
?>
If you need anything more of me let me know but just to give you an idea of what I'm achieving.
its a news post, if you create a post you have a title and description and you have the choice of entering in a file/image.. if you leave it blank it will enter in /images/ and if you enter an image in it will be: /images/file_name (this is inserted into a db)
Check like this:
if (file_exists($news_item['file'])) {
I would simply use the alt property of the image to show an alternative text:
<img src="<?php echo base_url(); echo $news_item['file'] ?>" alt="no image">
or, if you want it to be completely blank:
<img src="<?php echo base_url(); echo $news_item['file'] ?>" alt="">
EDIT:
To completely remove the image, you can conditionally generate the image or an alternative text:
if (file_exists($news_item['file'])) {
echo '<img src="'.base_url().$news_item['file'].'">';
} else {
echo "No image";
}
I suggest you to store the default.jpg in DB ( You can set this default in DB ). And store the default.jpg image in image folder. So that you don't need to change anything in coding.

open a modal windows in a server

Hi everyone I have a modal windows in joomla 3.0!
I pass the router to javascript like this
<?php $link =JURI::root().'index.php?option=com_projects&view=proyectos&format=raw&task=todosProyecto&id='. $item->id;?>
<li class="item" data-id="id-<?php echo $item->id ?>" data-type="<?php echo $item->categoria ?>">
<a href="#modal" id="<?php echo $link;?>" role="<?php echo $item->id ?>" class="picture" data-toggle="modal">
<img src="<?php echo JURI::root()?><?php echo $item->imagen_portada; ?>"/></a>
<p class="titulo"><?php echo $item->nombre; ?></p>
<p> <?php echo $item->municipio; ?>(<?php echo $item->pais; ?>)<br><?php echo $item->year; ?>
</p>
In local when I open the modal windows it work well, but in the server the modal windows show me the index.php view of this component.
I think that my problem is here, when I take the request for the model I have this.
public function elegirSeleccionados(){
$this->pagination = $this->get('pagination');
$this->items = $this->get('recientes');
$this->list = $this->get('list');
parent::display();
}
But $this->get('list'); is null so I have to asigned a null value to list.
I changed by that..
$this->list = $this->items;
but dont work to!
Any idea!!!
Where is this code?
First part looks like a Layout (views\proyectos\tmpl\default.php) and the second one like a View (views\proyectos\view.html.php).
If it is so, I'd say you are not really loading items in View from the Model. Try using $this->items = $this->get('Items');
But this doesn't explain different results on server and local host.
Hi everyone I solved the problem... the name of my view hava a Camelcase for example itemId and joomla try to find itemid, so don´t find the view and show the default view in the modal.
So I change the name of file without camelCase and now work.!

Magento upsell url with full category path?

I've got a problem with my Magento installation. I'm using the upsell feature to provide some more interesting products. The output works so far but I've got a big problem with the product url.
The url links to my-shop.com/product.html and not to my-shop.com/category-1/category-2/product.html. That's bad because I'm calling a special sidebar navigation for each top category. And so nothing is shown...
I'm using the standard upsell output:
<img src="<?php echo $this->helper('catalog/image')->init($_link, 'small_image')->resize(115) ?>" width="115" height="115" alt="<?php echo $this->htmlEscape($_link->getName()) ?>" title="<?php echo $this->htmlEscape($_link->getName()) ?>" />
<h3 class="product-name"><?php echo $this->htmlEscape($_link->getName()) ?></h3>
<?php echo $this->getReviewsSummaryHtml($_link) ?>
<?php echo $this->getPriceHtml($_link, true, '-upsell') ?>
(I can't post the whole code, the editor will delete most of it)
I've tried it with $_link->getProductPath(), but the output was empty.
I also checked the settings in System->Config->Catalog->Search Engine Optimization. Use Categories Path for Product URLs set to Yes.
Does anybody has an idea how to get the full path url for the products?
Thank you for your help.
You may use the below logic to get the product url. Update the variables accordingly.
<?php
$_prodcats = $_product->getCategoryIds();
$_cat = Mage::getModel('catalog/category')->load($_prodcats[0]);
$produrl = $this->helper('catalog/output')->productAttribute($_product, $_product->getRequestPath(), 'request_path') ;
if($this->getRequest()->getModuleName() == 'catalogsearch') {
$produrl = '/'. basename($_cat->getUrl()) .'/' . basename($_product->getProductUrl()) ;
}
?>
I found a solution on the web which works for 1.9.2.1.
The upsell products become a link with category link.
$d = $_link->getData();
$id = $d['entity_id'];
$_product = Mage::getModel('catalog/product')->load($id);
$_categories = $_product->getCategoryIds();
$_category = Mage::getModel('catalog/category')->load($_categories[0]);
$cat_url = str_replace(".html","",$_category->getUrlPath());
$_url = Mage::getUrl($cat_url).basename($_link->getProductUrl());
And then use this url
Link
I still have the problem that the created sitemap.xml just added
www.website.com/product instead of
www.website.com/category/product
even if "Use Categories Path for Product URLs" is set to YES

Resources