Jump to content

[SOLVED] Trying to upload files to MySql database and cant display.


sandbudd

Recommended Posts

I have three files to upload and then display the results of the uploads.  The file will upload fine but on my display page is blank?  This might be an over kill but here are all three codes and thanks in advance for the help as always.

 

add.html

 

<form enctype="multipart/form-data" action="insert.php" method="post" name="changer">
<input name="MAX_FILE_SIZE" value="102400" type="hidden">
<input name="image" accept="image/jpeg" type="file">
<input value="Submit" type="submit">

 

insert.php

 

<?php
// Create MySQL login values and 
// set them to your login information.
$username = "";
$password = "";
$host = "";
$database = ";

// Make the connect to MySQL or die
// and display an error.
$link = mysql_connect($host, $username, $password);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

// Select your database
mysql_select_db ($database);  
// Make sure the user actually 
// selected and uploaded a file
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 

      // Temporary file name stored on the server
      $tmpName  = $_FILES['image']['tmp_name'];  
       
      // Read the file 
      $fp      = fopen($tmpName, 'r');
      $data = fread($fp, filesize($tmpName));
      $data = addslashes($data);
      fclose($fp);
      

      // Create the query and insert
      // into our database.
      $query = "INSERT INTO tbl_images ";
      $query .= "(image) VALUES ('$data')";
      $results = mysql_query($query, $link);
      
      // Print results
      print "Thank you, your file has been uploaded.";
      
}
else {
   print "No image selected/uploaded";
}

// Close our MySQL Link
mysql_close($link);
?>  

 

display.php

 

<?php

$username = "";
$password = "";
$host = "";
$database = "";

@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());

@mysql_select_db($database) or die("Can not select the database: ".mysql_error());

$id = $_GET['id'];

if(!isset($id) || empty($id)){
die("Please select your image!");
}else{

$query = mysql_query("SELECT * FROM tbl_images WHERE id='".$id."'");
$row = mysql_fetch_array($query);
$content = $row['image'];

header('Content-type: image/jpg');
echo $content;

}

?> 

I'm just going to stop you right now and say:

 

don't do that.

Store the path, not the image in MySQL.

 

Do you not see the foolish futility in storing files in a DB?

 

Connect to Db.

Read Data

Parse out your slashies

Stream the data to the client.

 

 

Or..

 

Pull path of file from DB.

Present path in HTML.

Filesystem/Web Service (MUCH FASTER!) does the hard part.

 

I can only *FACEPALM* every time I see someone storing files in a table.

AWPTI Thanks here is what I have done since the post and it is displaying the path but it is garbaged...here is the code and a link so you can see what I mean and thanks for the help.  http://www.sandbudd.com/uploads/download.php

 

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>

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.