I have a PHP site where I have uploaded 10 pictures locally. The pictures are saved in the ./images folder and also resampled to a ./thumbnails folder. I use this query to extract 7 photo file names from the database.
$imgQuery = "SELECT FileName, Title, Description FROM PICTURE WHERE OwnerID='$id' LIMIT 0,7";
The database saves PictureID(PK), OwnerID(UNQ, my id is 2), FileName(stores the file name) and Title for the picture and Description for the picture. I use this method of transferring 7 photo filenames, title and description to an array. But how can I extract them from my ./thumbnails folder and display them on my PHP page?
if($imgResult = mysqli_query($link, $imgQuery))
{
while($imgRow = mysqli_fetch_row($imgResult))
{
$filename[] = $imgRow[0];
$title[] = $imgRow[1];
$description[] = $imgRow[2];
}
}
Here is where I am displaying the thumbnails t1 in the body. I would like to know how can assign the files retrieved by my database to these variables. The description changes based on which name i
$num = count($filename);
while($i < $num)
{
$i = 0;
print <<<photo
<body>
<form action='MyAlbum.php' method='post'>
<table>
<tr><td colspan='7' ><h2 align='center'><?php echo $name;?>'s Album</h2></td>
</tr>
<tr><td colspan='7' ><?php echo $title[$i];?></td>
</tr>
<tr><td colspan='5' ><?php echo $filename[$i]; ?></td><td colspan='2'><?php echo $description[$i];?> </td>
</tr>
<tr>
<td><?php echo $filename[0];?></td> <td><?php echo $filename[1];?></td> <td><?php echo $filename[2];?></td>
<td><?php echo $filename[3]; ?></td><td><?php echo $filename[4]; ?></td> <td><?php echo $filename[5]; ?></td>
<td><?php echo $filename[6]; ?></td>
</tr>
</table>
</form>
$i++;
</body>
</html>
photo;
}
It all depends on two things you don't say: the filename that in full resolution is saved in "FileName", what name has assigned inside thumbnails? And does FileName contain the full path, or only the "bare" file name?
// If
// FileName = "/images/LenaSjooblom.jpg", thumbnail is "./thumbnails/LenaSjooblom.jpg"
// Then
$Thumbnail = './thumbnails/' . basename($FileName);
// If
// FileName = "LenaSjooblom.jpg", thumbnail is "./thumbnails/LenaSjooblom.jpg"
// Then
$Thumbnail = './thumbnails/' . $FileName;
// If
// FileName = "/images/LenaSjooblom.jpg", thumbnail is "./thumbnails/12.jpg"
// Then
$Thumbnail = './thumbnails/' . $PictureID . '.jpg';
The code above you put into the same loop, e.g:
$filename[] = $imgRow[0];
$title[] = $imgRow[1];
$description[] = $imgRow[2];
// ADDED THUMBNAIL - hypothesis 1
$thumbnail[] = "./thumbnails/".basename($imgRow[0]);
and could display with
<img src="$thumbnail[$i]" />
Update
If your thumbnail is the duplicate of filename, then you need nothing else - you can just change the HTML, and add:
<img src="./thumbnails/$filename[$i]" />
and it will instruct the browser to fetch a filename with the same name of the image, but from the thumbnails directory. (If it doesn't work at first, check the path; in a pinch, use an absolute path, such as "/thumbnails/$filename[$i]" ).
You could do
<img src="<?php echo $filePath;?>" alt="some_text">
Just make sure that the variable you have for $filePath is the url to the actual image in the folder
Related
I have a folder on a NAS containing several images.
I would like to make a local copy of the images in a folder, display the images and then delete them so that they don't take over the local on the server.
How can I delete the photos after viewing them?
I made this code but it deletes the following image, so it can't be displayed :
<?php
$file = '//Alcyons/it/PhotoShoot/Photos_Outil/A1111_0070_1.jpg';
$newfile = 'A1111_0070_1.jpg';
if (!copy($file, $newfile)) {
echo "La copie $file du fichier a échoué...\n";
}
echo "</td><td valign=top align=center><img src=\"$newfile\" border=0 width=180px></a></td><td width=10></td>";
unlink($newfile);
?>
What do you solve by copying the file?
How does it make sense if you delete it instantly?
According to your code structure, that one will be better:
<?php
$file = '//Alcyons/it/PhotoShoot/Photos_Outil/A1111_0070_1.jpg';
$type = pathinfo($file, PATHINFO_EXTENSION);
?>
</td>
<td valign=top align=center>
<img src="<?php echo "data:image/$type;base64,",
base64_encode(file_get_contents($file)) ?>" border=0 width="180"></a>
</td>
<td width=10></td>
Thank you all in advance....
I am doing a project in which I have a field to store the image. In that form, the image uploaded is completed with croppie plugin. and I stored the data in base 64 encodings. But I cannot fetch it to a page called view.php but the images are fetched to the form after cropping.
Please help me to figure it out the mistake that I have done
<tbody>
<?php
$no = 1;
$data = mysqli_query($con, "SELECT * FROM `register` WHERE app_registration IS NULL ORDER BY `app_id` DESC ");
while ($row = mysqli_fetch_assoc($data)) {
?>
<tr>
<td><?php echo $row['app_id'] ?></td>
<?php if (!empty($row['image_reference_id'])) {
$data1 = mysqli_query($con, "SELECT * FROM `photo_table` WHERE image_unique_id = '" . $row['image_reference_id'] . "'");
$row1 = mysqli_fetch_assoc($data1)
?>
<td><img src="data:image/jpg;base64,'.base64_encode($row['images']).'"/></td>
<?php } else {
?>
<td><img src="../images/<?php echo $row['app_image'] ?>" style="width: 100px; height: 100px;"></td>
<?php } ?>
<td><?php echo $row['app_name'] ?></td>
<td><?php echo $row['app_mobile_no_1'] ?></td>
</tr>
<?php } ?>
</tbody>
Note: In db the images are in Long Blob
You will need to change the following:
<img src="data:image/jpg;base64,'.base64_encode($row['images']).'" />
to
<img src="data:image/jpg;base64, <?php base64_encode($row['images']);?>" />
Im trying to retrieve images from my database by using php, my code is connected to the database, all the other information is being retrieved ok, however it is just showing my image path when trying to retrieve images! Anyone know what im doing wrong so i can get my images to show up?
$query = "SELECT p.name, p.image, p.price, p.description, p.category_id, ci.quantity
FROM products p
LEFT JOIN cart_items ci
ON p.id=ci.product_id
WHERE p.id=?
LIMIT 0,1";
$stmt = $con->prepare( $query );
$stmt->bindParam(1, $id);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$name = $row['name'];
$image = $row['image'];
$price = $row['price'];
$description = $row['description'];
$category_id = $row['category_id'];
$quantity = $row['quantity'];
?>
<!-- HTML form for updating a product -->
<tr>
<td style='width:30%;'>Name<?php echo "<div class='product-id' style='display:none;'>{$id}</div>"; ?></td>
<td class='product-name' style='width:70%;'><?php echo $name; ?></td>
</tr>
<tr>
<td>Image</td>
<td><?php echo $image; ?></td>
</tr>
As now you have you image path, you can show up the image in html something like: <img src="<?php echo $row['image']; ?>" >. Your are doing so because this is the convenient way store/retrieve an image form Database by using their path. Instead of storing the path of the image, storing the whole binary image file in database make it more complex and less efficient.
I am working on a basic ecommerce website using PHP/MYSQL. I just need to know how I can upload multiple images for a product and then display them in the products page.
as for uploading multiple images, I don't want to use uploadify or open source codes like that. i rather have 3-4 extra fileupload fields if possible at all!
And I cannot get my head around the displaying the images (multiple images for 1 product). I really don't understand how it should work! so any advice on simple terms would be appreciated.
Currently I can only upload 1 image per product.
Here is what I have so far, please ignore the mysql queries in the first file as this is a not going live yet until I have converted the mysql to mysqli. Just need to get functions sorted first:
upload.php
<?php
// Parse the form data and add inventory item to the system
if (isset($_POST['product_name'])) {
$product_name = mysql_real_escape_string($_POST['product_name']);
$price = mysql_real_escape_string($_POST['price']);
$quantity = mysql_real_escape_string($_POST['quantity']);
$category = mysql_real_escape_string($_POST['category']);
$details = mysql_real_escape_string($_POST['details']);
// See if that product name is an identical match to another product in the system
$sql = mysql_query("SELECT id FROM products WHERE product_name='$product_name' LIMIT 1");
$productMatch = mysql_num_rows($sql); // count the output amount
if ($productMatch > 0) {
echo 'Sorry you tried to place a duplicate "Product Name" into the system, click here';
exit();
}
// Add this product into the database now
$sql = mysql_query("INSERT INTO products (product_name, price, quantity, details, category, date_added)
VALUES('$product_name','$price','$quantity','$details','$category',now())") or die (mysql_error());
$pid = mysql_insert_id();
// Place image in the folder
$newname = "$pid.jpg";
move_uploaded_file( $_FILES['fileField']['tmp_name'], "../inventory_images/$newname");
header("location: add.php");
exit();
}
?>
product.php <<< this is the page that displays the product details and image.
<?php
// Check to see the URL variable is set and that it exists in the database
if (isset($_GET['id'])) {
// Connect to the MySQL database
include "config/connect.php";
$id = preg_replace('#[^0-9]#i', '', $_GET['id']);
// Use this var to check to see if this ID exists, if yes then get the product
// details, if no then exit this script and give message why
$sql = "SELECT * FROM products WHERE id='$id' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
$productCount = mysqli_num_rows($query); // count the output amount
if ($productCount > 0) {
// get all the product details
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$product_name = $row["product_name"];
$price = $row["price"];
$details = $row["details"];
$quantity = $row["quantity"];
$category = $row["category"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
}
} else {
echo "That item does not exist.";
exit();
}
} else {
echo "Data to render this page is missing.";
exit();
}
?>
<table width="900" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="300" rowspan="5" align="right" valign="top" style="padding-top:10px;"><img src="inventory_images/<?php echo $id; ?>.jpg" width="300" height="450" alt="<?php echo $product_name; ?>" /></td>
<td width="126" height="106"> </td>
<td width="274"><h3 style="font-family:Times New Roman; font-size:1.8em;"><?php echo $product_name; ?></h3></td>
</tr>
<tr>
<td height="120"> </td>
<td><?php echo $details; ?></td>
</tr>
<tr>
<td height="110"> </td>
<td style="font-family:Times New Roman; font-size:1.8em;">Price: £<?php echo $price; ?></td>
</tr>
<tr>
<td height="50"> </td>
<td style="font-family:Times New Roman; font-size:1.8em;">Quantity Left: <?php echo $quantity; ?></td>
</tr>
</table>
Thanks
Well the way you are currently doing it isn't really setup for multiple photos since you aren't storing a reference to the photo in the database. You are simply renaming the image to the primary key of the product. So you will need to either do something like 1_1.jpg 1_2.jpg or you will need to create a database table that stores the filename and the product id so you can have a one to many relationship.
As for uploading more images just add more file inputs to your form.
And for displaying you will need to either pull records from the photo db table or use glob() to find all the files that start with the primary key + '_'.
Also FYI mysql functions should no longer be used as they are deprecated.
I am unexperienced in ways of PHP and MySQL, but I am trying to build an image upload website following a tutorial of Kevin Skoglund. Everything was fine up to a point where my images are supposed to show on a page in a html table.
I got everything from my database (image size, type, name, caption), but instead of image thumbnail I get broken link.
Here is relevant html
<tr>
<td><img src="../<?php echo $photo->image_path(); ?>" width="100" /></td>
<td><?php echo $photo->filename; ?></td>
<td><?php echo $photo->caption; ?></td>
<td><?php echo $photo->size_as_text(); ?></td>
<td><?php echo $photo->type; ?></td>
</tr>
Here is php that defines image path
protected $upload_dir="images";
public function image_path() {
return $this->upload_dir.DS.$this->filename;
}
Site root defined:
defined('SITE_ROOT') ? null :
define('SITE_ROOT', DS.'wamp'.DS.'www'.DS.'projekat');
this is folder structure
this is end result
Thanks for taking time to read and help with my problem in advance.
EDIT:
return $photo->image_path() as
projekat/public/images/filename.jpg
and display
<td><img src="/<?php echo $photo->image_path(); ?>"
In html, you can use beginning '/' to indicate root. So if you have stored your relative path in database as
$path = relative/path/to/file
you can use
src = "/<?php echo $path?>"