remove file extensions from a file directory in php - 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.

Related

Broken Image placeholder for uploading images

Good day everyone, so I have a code here for my site for uploading images to customer profile photos, but if they haven't uploaded yet it shows a broken image, how do I put a placeholder instead of a broken image.
<div class="panel-body">
<a data-target="#myModal" data-toggle="modal" href=
""><img class="img-hover" src="<?php echo web_root. "customer/".$res->CUSPHOTO; ?>"
style="width:100%; height:100%;text-align:center" title=
"profile image"></a>
</div>
This is my code for uploading the image
function doupdateimage(){
$errofile = $_FILES['photo']['error'];
$type = $_FILES['photo']['type'];
$temp = $_FILES['photo']['tmp_name'];
$myfile =$_FILES['photo']['name'];
$location="customer_image/".$myfile;
if ( $errofile > 0) {
message("No Image Selected!", "error");
redirect(web_root. "index.php?q=profile");
}else{
#$file=$_FILES['photo']['tmp_name'];
#$image= addslashes(file_get_contents($_FILES['photo']['tmp_name']));
#$image_name= addslashes($_FILES['photo']['name']);
#$image_size= getimagesize($_FILES['photo']['tmp_name']);
if ($image_size==FALSE ) {
message(web_root. "Uploaded file is not an image!", "error");
redirect(web_root. "index.php?q=profile");
}else{
//uploading the file
move_uploaded_file($temp,"customer_image/" . $myfile);
$customer = New Customer();
$customer->CUSPHOTO = $location;
$customer->update($_SESSION['CUSID']);
redirect(web_root. "index.php?q=profile");
Assuming that "basically has no value" means NULL you could check for that value:
<?php
if (is_null($res->CUSPHOTO)) {
$url = web_root. "customer/anonymous.jpg";
}
else {
$url = web_root. "customer/".$res->CUSPHOTO;
}
?>
<div class="panel-body">
<a data-target="#myModal" data-toggle="modal" href="">
<img class="img-hover"
src=<?php echo '"'.$url.'"' ?>
style="width:100%; height:100%;text-align:center"
title="profile image">
</a>
</div>
Here anonymous.jpg is the image to be displayed when no photo has been uploaded.
Opinion: Personally I don't like mixing the PHP tags into HTML like that. You never know exactly what will happen. I always write either HTML or PHP, not mixed, but I know it can be done. That's the reason I do the strange thing with the quotes around $url, I really don't like a PHP tag inside a HTML string.

How to Display image that coming from PHP file with id?

I have Image path like http://sits.in/imageDemo/image.php?image=R171-1.jpg in data base.
I can not load image in php by simply putting image link http://site.in/imageDemo/image.php?image=R171-1.jpg as src.
My code look like this
<?php
$count = 1;
while ($row = mysqli_fetch_array($result)) {
?>
<br>
<br>
<div class="card " style="width: 800px; margin-bottom: 25px;">
<div class="card-body">
<!-- <h5 class="card-title"><b>Product Number : <?php echo $count; ?> </b></h5> -->
<p>Order Id : <?php echo $row['orders_id']; ?></p>
<p>Product Name : <?php echo $row['product_name']; ?></p>
<img src="<?php echo $row['image1']; ?>" class="card-img-bottom" alt="Product Image"
style="width: 100px; height:100px">
</div>
<?php } ?>
I think you need to store path like that in your database
http://sits.in/imageDemo/R171-1.jpg
Also, create folder imageDemo in your local and store image as R171-1.jpg
If you want to use php file as image you need to make that php file to be shown as image file. Use GD libary. More info about how to use it for jpg here: https://www.php.net/manual/en/function.imagecreatefromjpeg.php

Getting Images With PHP And Display In Script

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

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.

Categories