Displaying image from path returns about:Blank - php

I have a bunch of jpg images in a folder that I need to call up one at a time. The user looks up a number, the number pulls up a filename from a database, and the filename is appended to a path to make a valid image path. That path is then placed in the src tag of an html element. The problem: nothing shows up except the little 16x16 image error thing. When I open it in a new tab, it says "about:Blank". I've echoed the path on its own to make sure it's showing up properly, and I've tried calling up the image in an independent html element with the full path placed in the src tag. What's going on here?
<?php //Location
function Location($JobID) {
$db = mysqli_connect('localhost','root','******','testdb1')
or die('Error connecting to MySQL server.');
$JobID = $_POST['JobID'];
$getinfo = "SELECT Location FROM Jobs WHERE JobID = '$JobID'";
$query = mysqli_query($db, $getinfo);
$row = mysqli_fetch_array($query);
$Location = $row['Location'];
echo "<img src=C:/wamp64/www/Floormaps/".$Location.">";
}
?>

If you're doing this on a web page that resides in the root of your web directory try changing your image tag as shown below. The web server only needs to know where the image is in relationship to the page where the source tag is.
<?php //Location
function Location($JobID) {
$db = mysqli_connect('localhost','root','******','testdb1')
or die('Error connecting to MySQL server.');
$JobID = $_POST['JobID'];
$getinfo = "SELECT Location FROM Jobs WHERE JobID = '$JobID'";
$query = mysqli_query($db, $getinfo);
$row = mysqli_fetch_array($query);
$Location = $row['Location'];
echo "<img src='Floormap/$Location'>";
}
?>

Related

Retrieve video file from mysql database using php and save it to local folder

I have been trying to upload video files to mysql database and retrieve those files and save it in a local folder. My table simply consists of id, a title of video, and size of video.
I thought both of retrieving from and uploading to database would be easy. I don't see any error except that undefined variable thing for $_GET["download"] once I run the code but the folder where all the videos should be saved is always empty.
PHP code saving a video file that I retrieve from database to a local folder:
if($_GET["download"] == 1)
{
$query = "select id, videoName, videoFile from video where ID = 5";
$result = $dab->sql_query($query);
$row = mysqli_fetch_assoc($result);
$_FILES["size"] = $row["videoFile"];
$_FILES["tmp_name"] = $row["videoName"];
move_uploaded_file($_FILES["tmp_name"], "download/" . "sample.mp4");
}
HTML a tag:
<html lang = "en">
<body>
<a id = "download" href = "main2.php?download=1">download</a>
</body>
</html>
move_uploaded_file($_FILES["tmp_name"], "download/" . "sample.mp4");
make sure about this path and Check if the download/ dir is writeable.

Display an image that is stored in a directory in browser

This seems like such a simple thing to do but for some reason, I can't get this to work.
I have a database with vehicle data stored in it. (Used Cars (I'm developing a car dealership website)).
I successfully display results from the database without images.
The images for each record aren't stored in the database, instead they're dumped on the server in a directory and those images are only referenced in the database.
If I echo the image name out it works fine, and if I echo the actual image out, the path is correct if you look at the image info. But in the info it states that the image is of text. i don't know how to change this.
Please find some of the code below.
<?php
$dbName = "F:/Domains/autodeal/autodeal.co.za/wwwroot/newsite/db/savvyautoweb.mdb";
// Throws an error if the database cannot be found
if (!file_exists($dbName)) {
die("Could not find database file.");
}
// Connects to the database
// Assumes there is no username or password
$conn = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$dbName", '', '');
//this is selecting individual records
$selected_id = intval($_GET['Id']);
//this is the query
$sql = "SELECT Id, Make, Model, Year, Price, SpecialPrice, Branch, StockNO, MainPic FROM Vehicle WHERE Id = $selected_id";
// Runs the query above in the table
$rs = odbc_exec($conn, $sql);
$id = odbc_result($rs, Id);
$make = odbc_result($rs, Make);
$model = odbc_result($rs, Model);
$mainPic = odbc_result($rs, MainPic);
//this is a failsafe for when there are no images for a specific record
$no_image = "<a href='db/Nopic.gif' data-lightbox='nopic' title='No Image Available'><img src='db/Nopic.gif' /></a>";
//This successfully displays the name of the image referenced in the database
$main_pic = "<img src='db/vehicleImages/'" .$mainPic. "/>";
//this is supposed to display the actual
echo $main_pic . "<br />";
echo $no_image;
odbc_free_result($rs);
odbc_close($conn);
// This message is displayed if the query has an error in it
if (!$rs) {
exit("There is an error in the SQL!");
}
?>
Any help in the regard would be greatly appreciated.
Thank you. :)
you should use quotations around the attributes of html objects (it looks like you might be breaking things via your method):
yours:
<a href=db/Nopic.gif data-lightbox=nopic title=No Image Available><img src=db/Nopic.gif /></a>
correct:
<a href='db/Nopic.gif' data-lightbox='nopic' title='No Image Available'><img src='db/Nopic.gif' /></a>
whether or not this fixes your problem will be determined by what you come back with :p
User forward slash in front of your image paths. This way you can be sure the image path always starts from the root folder. The image path is relative to the location of the executed php script.
For example if your script.php file is in the folder /root/some_folder/script.php and you use the image path db/vehicleImages/image.jpg the script is starting the path from the same folder where it is itself which results in a path like this /root/some_folder/db/vehicleImages/image.jpg.
If you use forward slash in front of the path like this /db/vehicleImages/image.jpg it tells the script to start from the root folder like so /root/db/vehicleImages/image.jpg.
I think this is the case with your script - you are giving it the wrong path which results in a file not found.

Retrieve BLOB .bin image to be viewed

I have two files;
image.php
<?php
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
$result = mysql_query("SELECT *FROM products") or die(mysql_error());
//Your code to call database for image id (from _GET['id']), for instance:
$image_id = $_GET['pid'];
$data = mysql_query("Select img where pid = $image_id");
while($data = mysql_fetch_assoc($result)){
$image=$data['img'];
header("content-type: image/png");
echo $image;
}
?>
and ay.html to view the image;
<img src="image.php?pid=IMAGE_ID" />
After many examples I tried, and every thread on this site and elsewhere, I'm still getting this error;
The image"http....../image.php?pid=IMAGE_ID" cannot be displayed because it contains errors.
Any help would be appreciated.
By the way the MySql variable is a LONGBLOB called img
I wonder why there isn't a simple IMG type for MySQL variable!
You should post how you stored the image to see exactly what it's wrong with your code but as the error says the image has some kind of error hence it was not stored correctly to database.
Generally the steps to go from an image to binary data and reverse are as below:
Image to binary conversion:
<? php
$image_data=file_get_contents('test.jpg');
$encoded_image=base64_encode($image_data);
echo $encoded_image;
?>
Binary to image conversion:
<?php
$image_data = file_get_contents('test.jpg');
$encoded_image = base64_encode($image_data);
$decoded_image = base64_decode($encoded_image);
echo $decoded_image;
?>
What you're missing altogether is the base_64 encode to prevent symbols from being incorrectly ttransformed.
/In your html page.../
/* */

Using PHP how can i display images which have been saved in SQL as a image path

I am trying to retrieve images from my SQL database which have been saved using the image path. The images are saved on the server. When i retrieve the images , nothing is returned.
if(isset($_GET))
{
include_once "mysql_connect.php";
$text = $_GET['text'];
if($text=="")
{
$query = mysql_query("SELECT `URL` FROM `gallery` WHERE `img_text` LIKE '1'")
or die(mysql_error());
while( $rows=mysql_fetch_array($query) )
{
$search[] = $rows[0];
}
$result = array();
$result["result"] = 500;
$result["message"] = "Database Read Successfully";
$result["rows"] = $search;
echo json_encode($result);
exit;
The example of code is for a search without the user entering a value. Within the SQL statement 'URL' is the field where the image paths are stored.The images paths values are the complete URL http://example.com/images/test.jpg and stored as a VARCHAR(200) . Any advice would be appreciated
Your code should not work well. You are not closing the if statement.
Also, when $text is not empty you don't have any query to the database...
You would need something like:
if($text==""){
$query = mysql_query("SELECT `URL` FROM `gallery` WHERE `img_text` LIKE '1'")
or die(mysql_error());
}
//when $text is not empty...
else{
$query = mysql_query("SELECT `URL` FROM `gallery` WHERE `img_text` LIKE '".$text."'")
or die(mysql_error());
}
while( $rows=mysql_fetch_array($query)){
$search[] = $rows[0];
}
By the way, try to use PDO instead of mysql_query, this last one is obsolete and is vulnerable to SQL injections.
The images are in the server, so the path only works there.
If you do a:
echo json_encode($result);
I guess you are returning this info to the client, the images path have no value there if you want to display them!
If you want to show the user the image you need to use a HTML img tag and fill the src attribute with the path FROM THE SERVER.

How to display through php an image stored in a folder considering that my image's link is stored in MySQL database

As good practice, I'm only storing my image's link in my database, the questios are:
How should I store my image's link? (let's say it's on c:)
c://image.jpg?
Which peace of PHP code should I use to display that image?
I'm only displaying the path, what should I do to display the image?
Can I use this:
$query = "SELECT ImageURL from WhateverTable";
$result = mysql_query($query) or die(mysql_error());
$Image = mysql_fetch_row($result);
echo "<img src='$Image[0]' alt='This is an image'>";
Thank you all lads
You only want to store the relative path, not the absolute path, as linking to something like
<img src="/var/www/vhosts/website.com/images/file.jpg">
would return a 404 on any real website. store your files in the database via a relative path (/images/file.jpg) or by only the filename if they are all in the same directory.
alternatively, you can learn MongoDB and it allows you to actually store files in the database itself.
I would strongly suggest that you use PDO instead.
Use relative URLs to your image folder in case you need to move them one day.
Here is an example.
// relative to your public webroot
$publicImageUrl = '/images/in/here';
// Pull up some record, maybe of a product
$select = 'SELECT imageFilename FROM products WHERE id = 2332';
$results = mysql_query($select);
if(!$results) {
// issue with query. deal with it here
} else {
if( mysql_num_rows($result) ) {
// record not found. deal with it here
}
$row = mysql_fetch_array($result);
$imageSrc = $publicImageUrl . '/' . $row['imageFilename'];
}
And then your HTML would be as follows
<img src="<?php echo $imageSrc; ?>" />
use PDO for php <-> mysql connection
post mysql query output

Categories