it works but some time opens in dreamweaver.i just used the same code
I have used this tutorial to save and display images. Works great, thanks.
Just a quick note for anyone that is using this to store PDF images.
Change the header in show.php to:
header('Content-type: application/pdf');
Great tutorial!
Jon
You're welcome.. but yes, but if you wanna host lot's of different files in db.. then store their mime type into db also.. and then automatically take it from db so you don't have to change this header thing.. just take mime type from db and add it to header like this:
Code:$mime_type = "MimeTypeTakenFromDb";
header("Content-type: $mime_type");
Yes, thanks. I have added a field in the DB for file type.
Jon
Hi Jaan, I've just had a quick google, before I ask the question. As headers have to be sent as, well, headers - Is it okay to include multiple Content-Types ?
Thanks,
Phill.
Why do you need multiple Content-Types ?
What is the general layout for that "mix"?
You can just write a PHP file that handles images/files depending on the URL parameters.
Let's just assume the PHP file is named getfile.php:
Remember that you can always add more conditionals for any file types you might need.Code:<?php
// Assuming $_GET["type"] is the file type,
// $_GET["id"] is the file id in the database,
// and the URL to call this file is something like
// getfile.php?type=img&id=8
// or getfile.php?type=pdf&id=10
if (isset($_GET["type"]) && isset($_GET["id"])) {
if ($_GET["type"] == "img" && is_numeric($_GET["id"])) {
// Put the code required for pulling the image from the database.
// It's better to also have the mime-type stored in the database.
// Put that mime-type into a variable called $imgMime.
// And assume the image blob is $imgData.
header("Content-type: $imgMime");
echo $imgData;
mysql_close();
}
else if ($_GET["type"] == "pdf" && is_numeric($_GET["id"])) {
// Now, assuming you want the user to download the PDF file,
// the header setting would be a bit different.
// This is assuming the mime-type is $fileMime,
// you stored the file size in the db and is being put into $fileSize,
// you stored the file name in the db and is being put into $fileName,
// and the data blob is $fileData.
header("Content-Type: $fileMime");
header("Content-Length: $fileSize");
header("Content-Disposition: attachment; filename=$fileName");
echo $fileData;
mysql_close();
}
}
?>
Now, the HTML file should be something like this if you want to make the PHP file works:
Of course, the ideal way is to generate the HTML and then the file types and IDs iteratively using another PHP file.HTML Code:<html> <head> <title>Getting some files from MySQL blobs</title> </head> <body> <table> <tr> <td><img src="getfile.php?type=img&id=1" alt=""></td> <td><a href="getfile.php?type=pdf&id=1">Click here to download the PDF!</a></td> </tr> </table> </body> </html>
Hope that helps.
...oh, and please correct me if I'm wrong.![]()
Thanks ....
hmmmm.... Things have gotten a bit more complicated, but, as I declare in a php script my HTML headers, I can have a 'play' with it and see if it passes XHTML (Strict).
What I was asking was, if I have JPG / PNG stuff and PDF stuff being built by my php script to display in strictly mode, how do I achieve it - I can only send the header once.
I'm glad most of the pages require jpg's being transferred - the header file for MIME type is growing
but, i do reduce the code for each page as I transfer control over the php routine
The last fully commented mime-type module I did is here Mime types with dynamic web-sites - VPOLink
Since then, I'm introducing Rule508 into the system - fortunately xhtml-strict is pretty darn compliant with it.
Phill.
Last edited by phillw; 10-30-2009 at 07:46 PM.
Actually, it doesn't matter if the PHP file can only send the header once, since you always "re-run" the "file fetching PHP file" every time you "call" it using the bog standard <img src="" alt="" /> tag.
That's why I said earlier in my previous post that you should use the PHP file just to fetch the files you wanted along with their proper mime-types, which means it's not to also be used to generate the XHTML page.
Basically, you use one PHP file to generate the XHTML file and another just to fetch the files.
That file fetching PHP will always be run multiple times according to how much, for example, images that you want to show in the page, regardless of its mime-types.
This file fetching PHP is also the one that uses the header() function, not the XHTML generating one.
It'd be easier to have a table that's, for example, named fileStore that at least contains these fields:
fileId, fileName, fileMime, fileSize, fileData
Keep in mind that fileSize is in bytes, so you might want to use BIGINT for it just to be safe, unless you can be more stringent in limiting the upload size.
And then use another PHP file to upload and automatically generate the mime-types to be inserted to the database so that you won't have to insert them manually every time for each uploaded file.
This is what I personally use (let's just name it upload.php):
As for the XHTML file to upload the files, just use a plain form for it.Code:<?php
if (isset($_FILES["uploads"])) {
// Takes all the necessary data from the uploaded image.
$fileName = mysql_real_escape_string($_FILES["uploads"]["name"]);
$fileMime = mysql_real_escape_string($_FILES["uploads"]["type"]);
$fileSize = $_FILES["uploads"]["size"];
$fileData = mysql_real_escape_string(file_get_contents($_FILES["uploads"]["tmp_name"]));
if ($_FILES["uploads"]["error"] == 0) {
// As usual, place the db connection code here.
$sql = "INSERT INTO `fileStore` VALUES ".
"(null,'$fileName','$fileMime','$fileSize','$fileData')";
mysql_query($sql);
mysql_close();
}
}
?>
Yes, it lacks the DTD and xmlns part, but that's caused by the forum's limitation to members with less than 10 posts.HTML Code:<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"> <html> <head> <title>File Uploader</title> </head> <body> <form action="upload.php" method="post" enctype="multipart/form-data"> <table> <tbody> <tr> <td><input type="file" name="uploads" /></td> </tr> <tr> <td> </td> </tr> <tr> <td align="center"><input type="submit" id="submit" value="Upload" /></td> </tr> <tr> <td> </td> </tr> </tbody> </table> </form> </body> </html>
After that, you can just make a PHP file to generate the HTML file that will show the images stored.
Remember that this example uses a slightly modified getfile.php I mentioned in the previous post; I'll put the modified version after this one.
imgshow.php
getfile.phpCode:<?php
echo <<<HEADER
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN">
<html>
<head>
<title>Image Displayer</title>
</head>
<body>
HEADER;
// Put the recursive call to generate the images here.
// Maybe something like this:
// Place db connection code here.
$sql = "SELECT `fileId` FROM `fileStore`";
$result = mysql_query($sql);
for ($row = mysql_fetch_assoc($result)) {
echo "<img src=\"getfile.php?&id=".
$row["fileId"].
"\" /><br /><br />\n";
}
mysql_close();
echo <<<FOOTER
</body>
</html>
FOOTER;
?>
Well, since the file uploading part can handle any file types already and their respective mime-types are being stored in the db, it depends on how much you could tweak getfile.php to get the file types you wanted to be fetched.Code:<?php
if (isset($_GET["id"])) {
if (is_numeric($_GET["id"])) {
// Place db connection code here.
$id = $_GET["id"];
$sql = "SELECT * FROM `fileStore` WHERE `fileId`=$id";
$result = mysql_query($sql);
if ($row = mysql_fetch_assoc($result)) {
$mime = $row["fileMime"];
header("Content-type: $mime");
echo $row["fileData"];
}
mysql_close();
}
}
?>
I hope I didn't misunderstand on how you want to generate your pages.
Otherwise, my examples above may just be used as a template for anyone else interested.![]()
Last edited by k1net1cs; 03-16-2010 at 05:05 AM.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks