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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.