I have a form that submits, Both text and of course, my image file to my database along with moving to image to a permanent directory.
The issue I am having is when I pull and display the content from the database everything shows apart from the image file.
Images for better context
Database entry, End result
HTML Form
<?php
include("inc/dbconfig.php");
error_reporting(0);
if (isset($_POST['btn-signup'])){
//Text Data Input
$cardName = $_POST['cardName'];
$cardSet = $_POST['cardSet'];
$cardRarity = $_POST['cardRarity'];
$cardImg = $_FILES['cardImage']["name"];
move_uploaded_file($_FILES["cardImage"]["tmp_name"],"../".$_FILES["cardImage"]["name"]);
$cardImgPath = "media/images/userUpload/".$_FILES["cardImage"]["name"];
$mysqlQ =("INSERT INTO cards (cardName, cardSet, cardRarity, cardImage) VALUES ('$cardName', '$cardSet', '$cardRarity', '$cardImgPath')");
mysqli_query($conn,$mysqlQ);
header('Location: directory.php');
}
exit();
?>
Displaying the data
<?php function itemCard (array $row) { ?>
<div class="card" style="width: 18rem;">
<img class="card-img-top" src="<?php echo '../'. $row['cardImage']?>" alt="Card fsa cap">
<div class="card-body">
<h5 class="card-title"><?= $row["cardName"]?></h5>
<p class="card-text">Card Set: <b><?= $row["cardSet"]?></b></p>
<p class="card-text">Card Rarity: <b><?= $row["cardRarity"]?></p>
</div>
</div>
<?php } ?>
can you confirm me your uploaded image stored in folder else follow the steps
use $_SERVER['DOCUMENT_ROOT'] path instead of '../'.
that image moved folder have write permission (777)?
Related
trying to make this code render images in rows all to no avail. I initially tried to use boostrap's row and column classes and it didn't work. Then I tried the table element still no result. could you spot the problrm?
// Get images from the database
$query = $db->query("SELECT * FROM images ORDER BY id DESC LIMIT 5");
if($query->num_rows > 0){
while($row = $query->fetch_assoc()){
$imageURL = 'uploads/'.$row["file_name"];
?>
<!-- begin post -->
<div class="container recent-posts">
<table>
<tr>
<td class="card" style="width: 18rem;">
<img src="<?php echo $imageURL; ?>" alt="" />
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
Go somewhere
</div>
</td>
<?php }
}
else{ ?>
<p>No image(s) found...</p>
<?php } ?>
</tr>
</table>
</div>
I've swapped in your db code for an array and foreach, but it is in the same spirit.
Use indentation, to help you get your loops and tags in order.
<?php
$images = [
['file_name' => 'foo.jpg'],
['file_name' => 'bar.jpg'],
['file_name' => 'baz.jpg'],
]
?>
<html>
<?php if(!empty($images)) { ?>
<table>
<tr>
<?php foreach($images as $row) { $imageURL = 'uploads/'.$row["file_name"]; ?>
<td class="card" style="width: 18rem;">
<img src="<?php echo $imageURL; ?>" alt="" />
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
Go somewhere
</div>
</td>
<?php } ?>
</tr>
</table>
<?php } else { ?>
<p>No image(s) found...</p>
<?php } ?>
</html>
As others have said, there isn't anything really to be gained here by using a table.
Does it show you the rest of the html elements or does it show you the message that there is no picture?
Check with the inspect tool what location the src attribute of the img element is.
You can add a backslash in the $imageURL variable
$imageURL = '/uploads/' . $row["file_name"];
Show us the structure and a record of the images table.
Give us more details about what happens when the page is loaded. Like what it loading in your page.
I suggest you ditch the tables and use boostrap instead.
Example here: https://getbootstrap.com/docs/4.0/components/card/
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
Managed to upload images successfully, am now trying to view the image but am failing to. Am not getting an error. the rest of the data is outputted except the image.
My model
function mycon(){
$client_id = $this->session->userdata('client_id');
$area = $this->session->userdata('area');
$this->db->select('*'); // the select statement
$this->db->where('client_id',$client_id);
$this->db->or_where('consignment.status',0);
$this->db->or_where('consignment.status',1);
$this->db->or_where('consignment.status',2);
$q = $this->db->get('consignment'); // the table
return $q;
}
Part of my controller
$data['v']=$this->Clientaccount_model->mycon();
My View
<?php
foreach ($v->result() as $row) { ?>
<section class="invoice">
<div class="row invoice-info">
<div class="col-sm-3 invoice-col">
<img src="<?php echo base_url()?>/uploads/" width="100">
</div><!-- /.col -->
<div class="col-sm-5 invoice-col">
<address>
LuggageID: <?php echo $row->luggage_id;?><br>
Lagguage Location: <?php echo $row->l_area;?><br>
Lagguage Destination: <?php echo $row->d_area;?><br>
Lagguage Description: <?php echo $row->description;?><br>
Date: <?php echo $row->dom;?><br>
</address>
<?php } ?>
My Table structure
Columns
luggage_id,
client_id,
l_area,
l_address,
d_area,
preferred,
dom,
tom,
description,
mass,
record_id,
filename,
size,
upload time,
status,
Image folder is uploads and its on the root
I suppose that your filename row in your db is about your image, but in your view, you only have <img src="<?php echo base_url()?>/uploads/" width="100"> , never give the name of image like <img src="<?php echo base_url()?>/uploads/<?= $row->filename?>" width="100"> , isn`t it?
[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.
This is my PHP script which displays a radio station's schedule:
<div class="divider"></div>
<div class="main" style="width:552px;">
<img src="$image" width=115 height=60>
<div class="time">$airtime</div>
<div class="show"><h3><a href="$link><b>$presenter</b></a></h3>
<p>$showdesc</p></div>
<div class="footer"></div>
</div>
</div>
<div class="footer"></div>
<div class="bottom"></div>
</div>
The values with the dollar sign represent the field names in my database, which is radiopresenters.
How would I get this to work as a PHP script, and display the values from the database?
All values in the fields are stored in TEXT format, apart from the image field which is stored in BLOB format.
Airtime is stored in a separate database entitled as radioschedule which has all 4 fields ib, and I intend to link these together via some relational means.
What's the best way to get it to display as the above, especially the BLOB part?
What you want to do is set up an show_image.php script, Example
show_image.php
<?php
$id = (isset($_GET['blid']) && is_numeric($_GET['blid'])) (int)$_GET['blid'] : false;
if($id)
{
if(false !==($res = mysql_query('SELECT blob_data FROM table WHERE blob_id = ' . $id))
{
$data = mysql_fetch_assoc($res);
header("Content-type: image/jpg"); //Send the content Type here.
print $data['blob_data'];
exit;
}
}
?>
Then within your html files you would do the follwing.
<div class="divider"></div>
<div class="main" style="width:552px;">
<img src="show_image.php?id=<?php echo (int)$id?>" width=115 height=60>
<div class="time">$airtime</div>
<div class="show">
<h3><a href="$link><b>$presenter</b></a></h3>
<p>$showdesc</p></div>
<div class="footer"></div>
</div>
</div>
<div class="footer"></div>
<div class="bottom"></div>
That's roughly how its done, another pointer is when your selecting your data via your main page, you should not select the blob data as it will slow your application down, and show_image.php may require more work as its for example purposes only
Peace.
You could write the image stored as a blob in the database out to a file. You could then use a url as your image source.
Assuming $presenter doesn't have any file/url reserved characters in it, and $image is stored as an accurate binary jpg (or png or gif etc.) , you could do:
<?php
if($fh = fopen("/webroot/images/{$presenter}.jpg", "wb")) {
fwrite($fh, $image) ;
fclose($fh) ;
}
?>
<img src="/images/<? echo $presenter ; ?>.jpg" width="115" height="60">
I would suggest that you work out some way of caching the file, so it doesn't have to be written out for every page load though.
Are you asking how to put those values into your file?
As for the text ones you could just use
<?=$airtime?>
or if your server setup doesn't support short tags just use
<?php echo $airtime?>
I would just do this inline with your php.
Example:
<div class="time"><?=$airtime?></div>
As for the BLOB, I would advise not doing this and just storing the image on your server and store the image file's name in the db. BUT if you are intent on doing this I think the only way you can do this is by having a separate script that returns an image file. Something like this:
<?php
// Do all your db stuff here, grab the blob file. Probably from a $_GET paramater
$image = $row['image'];
header("Content-type: image/jpeg"); // or gif, etc...
echo $image;
die();
?>
Then in your other file you would do something like:
<img src="imagescript.php?person_id=<?=$person_id?>" width=115 height=60>