The following is the code. I've the details of the file stored in an object and I retrieve all the file values from the database. The database table stores the file name, file type, file path, file size.
When I had uploaded the file, I copied/moved the file to another location and stored the file path and file details in the database (but not the file itself).
I am retrieving all the file values from the database to be displayed (files are in the types of either HTML, TXT or IMG). I am able to retrieve the files without any problems. I am able to display my HTML file, TXT file and IMG file but for the HTML files that contain a IMG file, that IMG file does not get displayed.
Sometimes - reason unknown: plain IMG files are not getting displayed.. and sometimes they do.. and that confuses me too!
So, I tried to create a program called getPicture.php that created a IMG header and returns the IMG file. I have the HTML file IMG SRC point to IMG SRC=getPicture.php?image1.jpg..
Please can you see where I am wrong.. Thanks for your help..
Amy.
The getPicture.php program is as follows:
<?php
/ About this page -
This page cannot be viewed by any user.
This page returns the image file.
/
if($image)
{
Header( "Content-type: image/jpeg");
//send the binary data
echo $image;
}
?>
The PHP program that displays all the files (retrieved from the database) is as follows::
<?php
//Start the session
session_start();
//Include the following class and php scripts.
include('MyFile.php');
include('MySqlConnection.php');
?>
<html>
<head>
<title>KCTS Events</title>
</head>
<body>
<?php
//Create an instance of the MyFile class
$newFile= new MyFile();
//Creates a database object using the MySqlConnection php script.
$db_connection = mysqlConnect();
//Retrieves all files
@ $result = mysql_query($newFile->selectAllFiles() ,$db_connection);
//If values could not be retrieved, prints error.
if (! $result)
{
echo "<br><b>Error: Values could not be retrieved.</b><br>";
exit;
}
//For every file row from the database, gets the file attributes.
while($row = mysql_fetch_array($result))
{
$newFile->setFileName(htmlspecialchars(stripslashes($row['FILE_NAME'])));
$newFile->setFileType(htmlspecialchars(stripslashes($row['FILE_TYPE'])));
$newFile->setFileSize($row['FILE_SIZE']);
$newFile->setFilePath(htmlspecialchars(stripslashes($row['FILE_PATH'])));
$file = $newFile->getFilePath();
//Prints the file information.
echo $newFile->printFile()."<br>";
if ($newFile->getFileType() == "text/plain")
{
$fp = fopen($file, 'r');
$contents = fread ($fp, filesize ($file));
fclose ($fp);
$contents = nl2br($contents);
echo $contents;
}
elseif ($newFile->getFileType() == "text/html" || $newFile-getFileType() == "text/htm")
{
include ($file);
}
elseif ($newFile->getFileType() == "image/pjpeg" || $newEvent->getFileType() == "image/gif")
{
echo "<img src=getPicture.php?$file ><br>";
}
}
?>
</body>
</html>