Getting Images With PHP And Display In Script - php

I have a blog and I want to be able to show images that are stored in a folder using a simple PHP script. Here is the PHP:
<?php
$folder = "$segment_url/files/photos/$post_year/$post_id/";
$filetype = ".jpg";
$files = glob($folder.$filetype, GLOB_BRACE);
foreach ($files as $file)
{
echo "
<img class=\"galleryPhoto\" src=\"$file\" /> // PROBLEM 1
";
}
?>
<?php echo $folder; ?>
<?php
$display_content = "
<div class=\"pageSection text\">
$content_intro
</div>
<div class=\"contentSpace\"></div>
<div class=\"pageSection text\">
// PROBLEM 2
</div>
<div class=\"contentSpace\"></div>
<div class=\"pageSection text\">
$content_conclusion
</div>
";
?>
In reference to the above code:
Problem 1: Even though the path to the images folder is correct, no images are being displayed.
Problem 2: How do I get the echoed content to be displayed here rather than at the top of the page?

For Problem 1 : You are missing to use full image path, otherwise search will search in current directory. Try like this.
<img class=\"galleryPhoto\" src=\"$folder/$file\" /> // PROBLEM 1

Related

Image src with php variable does not display the image on the web page

I wrote a code which is retrive image paths and display on the website in bootstrap grid style.But it does not showing the image. Code is working fine, Please help me. here is my code
<div class="row">
<?while ($row = mysql_fetch_assoc($query)) {?>
<div class = "col-md-3">
<?php echo $row['keywords'];?>
<?php $imagePath = $row['video_url'];?>
<?php echo $imagePath;?>
<div id="video_thumbnail">
<a href="#" class="thumbnail">
<?php echo '<img src="' . $imagePath . '">'; ?>
<img src="<?php echo file_dir . '/' . $imagePath; ?>" height="100" width="100"/>
</a>
</div>
</div>
<?php } ?>
</div>
unless file_dir is a constant using DEFINE, I suspect it's because you didn't put a $ in front of it $file_dir
You are also displaying two files. One with the file path and one without.
Chances are, the mysql query is returning a path which is not linked ....
ie <image src="myImage.jpg" /> is not the same as <image src="images/myImage.jpg" />
As #pmahomme said, right click the element and check the pat and if need be add the additional requirements

Image doesnt show up in php

In this little snippet of code ,i show how i take the "foto1" column of my database and transfer the value of it to a variable in c# named $foto.
The $foto contains the path of the image corresponding to the product that is showing up. Ive tried to copy and paste the path and ditch out the php part and it works. But when i put it in img src it gives me like the broken image thing.And i cant figure out why it does that.
All help is aprecciated .
Have a nice day :)
<div class="row shop_box-top">
<?php
$ligaBD=odbc_connect('basededadospap','','');
$sql="SELECT * FROM produto WHERE nome_produto LIKE '%ADIDAS%'";
$resultado=odbc_exec($ligaBD,$sql);
?>
<div class="row shop_box-top">
<?php
while (odbc_fetch_row($resultado))
{
$nome = odbc_result($resultado,2);
$preco= odbc_result($resultado,4);
$foto = odbc_result($resultado,9);
?>
<div class="col-md-3 shop_box"><a href="stansmithflatwhite.html">
<img src="<?php echo $foto; ?>" class="img-responsive" alt=""/>
<span class="new-box">
<span class="new-label">Novo</span>
</span>
<div class="shop_desc">
<h3><?php echo $nome; ?></h3>
<span class="actual"><?php echo $preco; ?></span><br>
</div>
</a></div>
<?php }?>
depends of what path contains the $foto var. If is the absolute path, you have to retrive the relative path.
Try also to append an / or an http[s] in front of the path
<img src="/<?php echo $foto;?>">
So it would be : //path/to/photo
As I can see it in your comment, your image paths contain spaces, so a possible solution can be to use urlencode() before echoing them.
Try passing full path to img tag like http://localhost/xyz/images/Cal�ado/Adidas/Homem/Stan Smith/ADIDAS STAN SMITH - RED/ch-adidas-stan-smith-red-5.jpg.
Replace "localhost/xyz" with your website directory path.

bootstrap image row out of place

[SOLVED]
I'm have written a horizontal row of 4 images 500x300 using placeholders, this works perfectly fine, but when I try to replace those images with images from a folder (using php) of the same dimensions, the first 2 image containes are knocked out of place while the last 2 are where they belong.. I've attached a screenshot and the code below is what I'm using to get the row of images.
<div class="row" style="margin-top:1px">
<?php
$directory = 'images';
if (! is_dir($directory)) {
exit('Invalid diretory path');
}
$files = array();
foreach (scandir($directory) as $file) {
?>
<div class="col-sm-3 col-xs-6" >
<a href="#" >
<img class="img-responsive portfolio-item" src="<?php echo "images/$file"; ?>" alt="">
</a>
</div>
<?php
}
?>
</div>
this is what i get in the browser
Using glob instead of an array fixed the problem. PHP is including the . and double dot of the directory structure and therefore creating 2 empty images.

remove file extensions from a file directory in php

I am doing a gallery and using php to get images to show up without hard-coding for each image. I had used the following method for obtaining the images:
<?php
$dir="image/";
//open dir
if ($opendir=opendir($dir))
{
//readdir
while($file=readdir($opendir))
{
if ($file!="."&&$file!="..")
echo "
<div id='item' class='grid-item'>
<a href='$dir/$file'>
<div id='masks' class='mask'></div>
<div id='title' class='text'>$file</div>
<img src='$dir/$file'>
</a>
</div>
";
}
}
?>
The gallery uses jquery for the masonry gallery plugin.
However the file extension is showing up along with the file name. I don't want the extension. I have tried many methods, but they're not working for this case. How can I hide it and what changes can be done to the code in order for it to happen?
You can use pathinfo:
<?php
$dir="image/";
//open dir
if ($opendir=opendir($dir))
{
//readdir
while($file=readdir($opendir))
{
if ($file!="."&&$file!="..")
{
$filename = pathinfo($file, PATHINFO_FILENAME);
echo "
<div id='item' class='grid-item'>
<a href='$dir/$file'>
<div id='masks' class='mask'></div>
<div id='title' class='text'>$filename</div>
<img src='$dir/$file'>
</a>
</div>
";
}
}
}
To get the filename via this method, you must use PHP > 5.2, which you probably should anyway.

Condition put on images

I want to put a condition on images come from directory that if the images count is 1 then show this div. But the condition is not running properly after applying the condition on it. Please help me to resolve this problem: My code is given below:
<?php
$id = 115;
$path_image = "pageimage/".$id."/";
if(file_exists($path_image)){
$dir = dir($path_image);
//List files in images directory
while (($file = $dir->read()) !== false){
if($file>=1){
?>
<div id="middleimg" style="background:url(<?php echo $path_image.$file; ?>); background-repeat:no-repeat;">
<div id="nav">
<div id="navinner">
<ul>
<?php
foreach($pageMenu as $pmenu){
$url = generateURL($pmenu->custom_url, $pmenu->page_friendlyname, $pmenu->page_title, $pmenu->page_id);?>
<li><?php echo $pmenu->page_name; ?> </li>
<?php } ?>
<!--<li>HOME</li>
<li>ABOUT</li>
<li>MENU</li>
<li>LOCATION</li>
<li>DELIVERY</li>
<li>SPECIAL OFFER</li>
<li>CONTACT</li>-->
</ul>
</div>
</div>
<div id="banner-containerhome">
<div id="promoBanner">
<!--<div id="pizza">-->
<div id="wowslider-container1">
<div class="ws_images">
<span><img src="data1/images/banner.jpg" alt="" title="" id="wows0"/></span>
<span><img src="data1/images/banner.jpg" alt="" title="" id="wows0"/></span>
</div>
<div class="ws_bullets"><div>
<img src="data1/tooltips/banner.jpg" alt=""/>1
<img src="data1/tooltips/chrysanthemum.jpg" alt=""/>2
</div></div>
<div class="ws_shadow"></div>
</div>
<script type="text/javascript" src="engine1/script.js"></script>
<!-- </div>-->
</div>
<div class="rightcontents">
<?php
echo $page_content[1]->page_content;
?>
</div>
</div>
</div>
<?php }}} ?>
There are a few things...
1.- read() reads the directory as an iterator, returning the file name, have you tried to echo $file; ?
if($file >= 1) will never be true, as $file is always a String, never an integer, you are comparing there: if("myfilename.ext" >= 1).
To count the total files you should do a while through the full directory and use a counter, increasing it for each file.
Or you can use scandir("myPath") function, that get all the files in the path specified as an array. for example:
$dir = scandir("myDir");
$total = count($dir); // this gives you the lenght of the array, therefore the files.
While counting, remember that the parent, and root directory are counted as "files" as well, and with some methods also the thumbs.db(windows) or DS_store(osx) are hidden files that store information about how you sort the folder, folder icons and stuff, will be counted as well, so remember to add an If statement to skip them.

Categories