Jump to content

upload works great and displays link but garbaged


sandbudd

Recommended Posts

I have a php mysql that uploads a file to the database.  It uploads great and displays the link but when clicked on it is garbaged.  Here is the link so you can see what I am getting. http://www.sandbudd.com/uploads/download.php and here are the codes.

 

upload.php

<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0 )
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
include 'config.php';
include 'opendb.php';
$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
mysql_query($query) or die('Error, query failed');
include 'library/closedb.php';
echo "<br>File $fileName uploaded<br>";
}
?>

<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspaci ng="1" class="box">
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload
"></td>
</tr>
</table>
</form>

 

download.php

 

<?
if(isset($_GET['id']))
{
include 'config.php';
include 'opendb.php';

$id      = $_GET['id'];
$query   = "SELECT name, type, size, content FROM upload WHERE id = '$id'";
$result  = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) = mysql_fetch_array($result);

header("Content-Disposition: attachment; filename=$name");
header("Content-length: $size");
header("Content-type: $type");
echo $content;

include 'library/closedb.php';	
exit;
}

?>
<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?
include 'config.php';
include 'opendb.php';

$query  = "SELECT id, name FROM upload";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "Database is empty <br>";
} 
else
{
while(list($id, $name) = mysql_fetch_array($result))
{
?>
<a href="download.php?id=<?=$id;?>"><?=$name;?></a> <br>
<?		
}
}
include 'library/closedb.php';
?>
</body>
</html>

 

Link to comment
Share on other sites

DarkWater I am sorry I am new at php mysql.  Here is a copy of the database as close as I can copy and past it.

 

  row(s) starting from record #
in mode and repeat headers after cells
   
Sort by key:
Full Texts 	id 	name 	type 	size 	content
Edit 	Delete 	2 	Hawaii2008 002.jpg 	image/pjpeg 	236490 	[bLOB - 230.9 KB]
Edit 	Delete 	3 	blue_box_03.gif 	image/gif 	157 	[bLOB - 157 Bytes]
Edit 	Delete 	4 	author.jpg 	image/jpeg 	20379 	[bLOB - 19.9 KB]
Edit 	Delete 	5 	color_fade.jpg 	image/jpeg 	13063 	[bLOB - 12.8 KB]
Edit 	Delete 	6 	killawatt.pdf 	application/pdf 	696243 	[bLOB - 679.9 KB]
Edit 	Delete 	7 	Hawaii2008 039.jpg 	image/jpeg 	1315558 	[bLOB - 1.3 MB]
With selected: Check All / Uncheck All With selected: Change Delete Export

    	
row(s) starting from record #
in mode and repeat headers after cells
   

Link to comment
Share on other sites

I am sorry but the same results.  I changed to this

 

<?php
if(isset($_GET['id']))
{
include 'config.php';
include 'opendb.php';

$id      = $_GET['id'];
$query   = "SELECT name, type, size, content FROM upload WHERE id = '$id'";
$result  = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) = mysql_fetch_array($result);

    header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
header("Content-length: $size");

echo $content;

include 'library/closedb.php';	
exit;
}

?>
<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?
include 'config.php';
include 'opendb.php';

$query  = "SELECT id, name FROM upload";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "Database is empty <br>";
} 
else
{
while(list($id, $name) = mysql_fetch_array($result))
{
?>
<a href="download.php?id=<?=$id;?>"><?=$name;?></a> <br>
<?		
}
}
include 'library/closedb.php';
?>
</body>
</html>

 

Link to comment
Share on other sites

still not working here is what I did and thanks for all the help

 

<?php
if(isset($_GET['id']))
{
include 'config.php';
include 'opendb.php';

$id      = $_GET['id'];
$query   = "SELECT name, type, size, content FROM upload WHERE id = '$id'";
$result  = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) = mysql_fetch_array($result);
$content = stripslashes($content);
    $name = stripslashes($name);

    header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
header("Content-length: $size");

echo $content;

include 'library/closedb.php';	
exit;
}

?>
<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?
include 'config.php';
include 'opendb.php';

$query  = "SELECT id, name FROM upload";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "Database is empty <br>";
} 
else
{
while(list($id, $name) = mysql_fetch_array($result))
{
?>
<a href="download.php?id=<?=$id;?>"><?=$name;?></a> <br>
<?		
}
}
include 'library/closedb.php';
?>
</body>
</html>

Link to comment
Share on other sites

I'm not getting your headers at all:

 

http://www.sandbudd.com/uploads/download.php?id=2



GET /uploads/download.php?id=2 HTTP/1.1

Host: www.sandbudd.com

User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9b5) Gecko/2008050509 Firefox/3.0b5

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language: en-us,en;q=0.5

Accept-Encoding: gzip,deflate

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

Keep-Alive: 300

Connection: keep-alive

Cache-Control: max-age=0



HTTP/1.x 200 OK

Server: Microsoft-IIS/5.0

Date: Sat, 21 Jun 2008 16:28:34 GMT

X-Powered-By: ASP.NET, PHP/4.4.7

Connection: close

Content-Type: text/html

----------------------------------------------------------

 

=/  Strange.

Link to comment
Share on other sites

I'm going to make sure your image data is correct. Do me a favor.  Change that stripslashes line to:

 

$content = base64_encode(stripslashes($content));

 

Then I'm going to write up a quick script on my box to verify the file is good.  Okay?

Link to comment
Share on other sites

Here is what I have done

 

<?php
if(isset($_GET['id']))
{
include 'config.php';
include 'opendb.php';

$id      = $_GET['id'];
$query   = "SELECT name, type, size, content FROM upload WHERE id = '$id'";
$result  = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) = mysql_fetch_array($result);
$content = base64_encode(stripslashes($content));
    $name = stripslashes($name);

    header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
header("Content-length: $size");

echo $content;

include 'library/closedb.php';	
exit;
}

?>
<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?
include 'config.php';
include 'opendb.php';

$query  = "SELECT id, name FROM upload";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "Database is empty <br>";
} 
else
{
while(list($id, $name) = mysql_fetch_array($result))
{
?>
<a href="download.php?id=<?=$id;?>"><?=$name;?></a> <br>
<?		
}
}
include 'library/closedb.php';
?>
</body>
</html>

Link to comment
Share on other sites

Okay, put it back to the way you had it before.  Something seems to be going wrong with uploading it.  The image isn't even putting itself together on my box when I save it.  I think you should just upload the image and store a path name in the db. 

 

By the way, you have the field as a BLOB in the database, right?

Link to comment
Share on other sites

I do have it as a blob and how do I store a path name?

 

here is my database structure

 


Field 	Type 	Collation 	Attributes 	Null 	Default 	Extra 	Action
id 	int(11) 			No 		auto_increment 	Browse distinct values 	Change 	Drop 	Primary 	Unique 	Index 	Fulltext
name 	varchar(30) 	latin1_swedish_ci 		No 			Browse distinct values 	Change 	Drop 	Primary 	Unique 	Index 	Fulltext
type 	varchar(30) 	latin1_swedish_ci 		No 			Browse distinct values 	Change 	Drop 	Primary 	Unique 	Index 	Fulltext
size 	int(11) 			No 	0 		Browse distinct values 	Change 	Drop 	Primary 	Unique 	Index 	Fulltext
content 	mediumblob 		BINARY 	No 		

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.