Okay, so I have a database that stores the name of an uploaded file in a column called filename.
I am trying to use PHP to show that file (stored in the file-system, not database) by taking the filename as stored in the database and concatenating it onto the HTML tag, like so:
$stmt = $db->dbh->query("SELECT id, filename FROM images");
$stmt->setFetchMode(PDO::FETCH_BOTH);
$path = '';
while ($row = $stmt->fetch()) {
$path = 'i/' . $row['filename'];
echo "<img src=" . $path . "/>";
}
However, it's not showing anything. What am I doing wrong, or what is a better way to accomplish this?
You are missing quotes around the image file name. Try this:
echo '<img src="' . htmlspecialchars($path) . '"/>";
Related
I need to figure out how to query several columns of data into a table on my webpage using PHP and get one column to display as clickable audio files. In my MySQL table the audio column consists of absolute paths to audio files saved in a folder on the disk of my web host. All attempts so far have only succeeded in querying the file path as text, not as clickable audio.
I've been using a PHP loop statement to try to query several data columns into a table, like this:
<?php
$sql = "SELECT id, word, audio FROM dictionary";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["word"]. " " . $row["audio"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
Here's a photo of my table with one record in it:
enter image description here
But I've also tried the URL in place of the absolute path: https://memoriaelinguagrumentina/dictionaryAudio/a1.com
In either case, the path displays as text.
Any suggestions on how to get the audio to display on my webpage as something clickable?
You have to output the $row["audio"] as a HTML a tag, but the absolute path is not always the path the file is accessible from outside.
The PHP code should look like something like this:
echo "id: ".$row["id"]." - Name: ".$row["word"]." <a href='".$row["audio"]."'>Click here to play audio</a><br>";
But for example if your audio file's path is /var/www/website/files/01.mp3 this isn't the path that can be used as a http link. You should convert it to a useable URL. If in the above example, your domain is website.com and your document root on the server is the /var/www/website you should do something like this:
func FilePathToURL($fp) {
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http";
return $protocol."://website.com".substr($fp, strlen("/var/www/website"));
}
And use this function to echo the correct link like:
echo "<a href='".FilePathToURL($row['audio'])."'>Click here</a>";
You would just need to change it so the echo that you output to the page is wrapped in an anchor tag something like this. You'll need to add in the href where it is going to be going based on your data tho.
echo "<a href=''>" . "id: " . $row["id"]. " - Name: " . $row["word"]. " " . $row["audio"] . "</a>" . "<br>";
Since your audio values begin with "/public_html/...", you need to remove that leading string since presumably your web root already points there:
echo 'id: ' . $row['id'] . ' - Name: ' . $row['word'] . '<br>';
This should result in HTML that looks something like:
id: 1: - Name: word<br>
I'm a beginner to coding and need a little help. I have a code that shows an image according to a specific value in a column of a MySQL table like this:
//Show last 10 added by ID
$sql = "(SELECT * FROM food_tbl ORDER BY food_id DESC LIMIT 10) ORDER BY food_id ASC;";
$result = $conn->query($sql);
$pathImg = "/images/";
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
$pathFile = $pathImg . $row['food_name'] . ".jpg";
echo '<img src="' . $pathFile . '">';
}
}
This outputs, for example, banana.jpg ... The thing is... I would like to show a standard image when files don't actually exist, as in, if I don't have a file called banana.jpg in my "/images/" directory it would show a unknownfood.jpg file instead (which would indeed be inside that directory of course).
Now, here's the thing, I've already found some stuff over here: --- Display specific image depending on specific text in MySQL table using PHP
... and I ended up trying this:
while($row = $result->fetch_assoc()) {
$pathFile = $pathImg . $row['food_name'] . ".jpg";
if (file_exists($pathFile)) {
echo '<img src="' . $pathFile . '">';
} else {
$pathFile = $pathImg . "unknownfood.jpg";
echo '<img src="' . $pathFile . '">';
}
}
Now, what this does is this will completely ignore the files that are actually there. For example, if I try showing banana.jpg it will instead show unknownfood.jpg.
I'm pretty sure it's something really easy that I'm screwing up, or something I'm not using the way it's intended to. Thanks for the help!
When you're in PHP, using file_exists($pathFile) is the path on the server, relative to the document root (which could be /public_html/youruser), while the HTML reads from the base-folder (off the domain, /).
This means your code would have to look something like this
if (file_exists($_SERVER['DOCUMENT_ROOT'].$pathFile))
file_exists will check a filesystem path, for example /home/www/images/banana.jpg.
When you display the img tag, it will pull the image from the doc root, domain.com/images/banana.jpg.
Find the correct file system path and pass it to file_exists, it will return the correct answer.
Right now it will always return false so you will end up in the else part of the if clause.
I am learning PHP & MySQL. Thanks to this great site and some other resources I have learned how to read through a database of mp3s
My goal is to identify songs without lyrics and make a copy of each song in another folder. $row['filename'] returns the full location such as "C:/../../../nameOfArtist/nameOfAlbum/foo.mp3" I do not know how to take that and move just the song into another directory creating an artist sub-directory and album sub-directory in the process.
Something like copy(($row['filename']), (C:/foo . '$row['artist'] . $row['album'] . "foo.mp3");
Here is what I have so far that works
$result = mysqli_query($con,"SELECT id, artist, title, album, filename
FROM songlist
WHERE lyrics IS NULL or lyrics = '' ");
while($row = mysqli_fetch_array($result)) {
echo "Artist: " . $row['artist'] . " Title: " . $row['title'] . " Album: " . $row['album'] . "<br/>;
$filename = $row['filename'];
echo $filename . "<br/>";
http://php.net/manual/en/function.copy.php - Copy() - Copies file
http://php.net/manual/en/function.basename.php - Basename() - Returns trailing name component of path
$filename = $row['filename'];
copy ( $filename , "C:\\foo\\" . basename($filename) );
This is my code in response of this script i get content on page that content is stored in database but please let me know the way to retrieve that content in image form?
$query='SELECT * FROM upload_file';
$result=mysqli_query($con,$query);
while($row = mysqli_fetch_array($result))
{
echo "<img src=".$row['plaint']." width='100' height='100'></img><br>";
}
Try it like this:
echo '<img src="data:image/jpeg;base64,' . base64_encode( $row['plaint'] ) . '" />';
This converts the data to the actual image.
This is the first time I have ever posted a question. I am a newbie, and I hope I get all the formatting correct in this message. I love stack overflow. It has helped me get through many challenging situations. But this one has me stymied.
I would like to replace a MySQL database result with an image. I have a plant database that I'm querying to show lists of plants. My results table consists of the plant type, the botanical name, the common name, and whether it is a native plant.
The result for the native plant comes in from the database as the letter 'y' (for yes). I would like to replace that 'y' with an image of an oak leaf. The code I'm currently using displays the oak leaf on all plants, not just the ones that are native plants.
What to do? I feel like I need to insert an if statement in the while loop, but it never works when I try it. I've tried tons of different solutions for a few days now, and would like to ask for help with this.
Here's the latest code I've got that displays the image for every plant, not just the natives:
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$native_image = $row['native'];
$image = $native_image;
$image = "images/oak_icon.png";
echo '<tr><td>' .
$row['plant_type'] .
'</td><td>' .
$row['botanical_name'] .
$row['common_name'] .
'</td><td>' .
$image .
'</td></table>';
}
I hope I understood your question.
Some of rows in MySQL have $row['native'], that are equals 'y'. If it is so, then you are on the right way - you have to put an if statement in your loop.
Try it
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
if ($row['native'] == "y") {
//we show our image
$image = "images/oak_icon.png";
} else {
//we don't have to show our image
$image = "";
}
echo '<tr><td>' .
$row['plant_type'] .
'</td><td>' .
$row['botanical_name'] .
$row['common_name'] .
'</td><td>' .
$image .
'</td></table>';
}
And where is your IMG tag? =)
For the future: In my Projects, if I want to save the information about image, that belongs to the row in MySQL, I just save the name of the picture, where it has to be, and if not, i save the name of empty 1x1 PNG image (usual "blank.png"). If you put blank.png in an IMG tag, that has fixed height and width, so IMG takes just the place on your page.
I hope I helped you, and this is my first Answer! =)
P.s. sorry for my english...
Explosion Pills is correct. You will need to check if the $row['native'] is 'y' then set the image html to whatever is required. Hope this helps.
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$image='';
if($row['native']=='y'){
$image = '<img src="images/oak_icon.png" />';
}
echo '<tr><td>' .
$row['plant_type'] .
'</td><td>' .
$row['botanical_name'] .
$row['common_name'] .
'</td><td>' .
$image .
'</td></table>';
}