Jump to content

Image upload/ download


Shazbot!

Recommended Posts

Greetings,

I need some help in with an image upload/download.

I am working on a site for my wife and she needs to upload an image to a database and then download the image.

 

php 5 with mysql 4

 

Problem is that when I try to display the image it shows up as garbled text (which I know is normal but I am not sure how to display it properly) The attached image is how it looks.

 

 

 

Here is the upload code that I got from the net for uploading an image, this appears to be working.

in MyPHP admin it shows the file size of the test image which looks right.

 

mysql_select_db($database_OTH) or die( "Unable to select database");
//echo "<br />";
//echo "something else";
$fileName = $_FILES['imagefile']['name'];
$tmpName  = $_FILES['imagefile']['tmp_name'];
$fileSize = $_FILES['imagefile']['size'];
$fileType = $_FILES['imagefile']['type'];

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}

require_once('DBConnect');

$File_SongName="Unknown";
if(isset($_POST['File_SongName'])) {
$File_SongName=$_POST['File_SongName'];
}

$File_SongVersion="1";
if(isset($_POST['File_SongVersion'])) {
$File_SongVersion=$_POST['File_SongVersion'];
}

$File_Uploaded_By=$_SESSION['FULLNAME'];
$TimeStamp=date('M-d-Y')." ".date('h:i:s A');
echo $_SESSION['FULLNAME'];
$query = "INSERT INTO  TableName(File_Name, File_Size , File_Type , File_Contents, File_Uploaded_By, File_SongName, File_SongVersion, File_Date_Uploaded) ".
"VALUES ('$fileName', $fileSize, '$fileType', '$content', '".$_SESSION['FULLNAME']."', '$File_SongName', '$File_SongVersion', '$TimeStamp')";

mysql_query($query) or die('Error, query failed');
echo "<br>File $fileName uploaded<br>";

 

 

Here is the code to display the data through a download page:

mysql_select_db($database_OTH) or die( "Unable to select database");
	$query="SELECT File_ID, File_Name, File_Size FROM  TableName WHERE File_ID=32";//.$_GET['FileID'];
	$SQL_Query=mysql_query($query) or die('Error, query failed');

	while($Results=mysql_fetch_array($SQL_Query)) {
		//echo $Results['File_Name'];
		//	echo "<br />";
		//echo $Results['File_Size'];
		//echo "<br />";

		header ("Content-Type: ".$Results['File_Type']."\n");
		header ("Content-disposition: attachment; filename=\"".$Results['File_Name']."\"\n");
		header ("Content-Length: ".$Results['File_Size']."\n");
		readfile ($Results['File_Contents']);



 

Thank you in advance for anyone that can help me.

 

 

 

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/72059-image-upload-download/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.