Jump to content

[SOLVED] need a way to upload and view images inserted to database


contra10

Recommended Posts

Im trying to modify this a little and cant seem to figure it out.

I have a table named Sponsor_Spn with a field Spn_Name I added fields Upload and logo I want the uploaded image to be stored in the logo field and be renamed from the Spn_Name field. then the view.php file needs to pull up the right image according to the Spn_Name

Maybe theres a better script some where but this is what I found that seemed like a way to go ???

These are the files I tried, the organals are in this post up a little ways

<?php

// Make sure the user actually 
// selected and uploaded a file
if (isset($_FILES['logo']) && $_FILES['logo']['size'] > 0) { 
  
  // Temporary file name stored on the server
      $tmpName  = $_FILES['upload']['tmp_name']; 
  $logo = $_FILES['upload']['type'];
      
  
  // 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 `Sponsor_Spn` (`Upload`, `logo`) VALUES ('" . $data . "', '" . $logo . "')";
      $results = mysql_query($query) or die(mysql_error());
      
      // Print results
      print "Thank you, your file has been uploaded.";
      
  // GET LINK
  $data_query = mysql_query('SELECT `id` FROM `Sponsor_Spn` WHERE `Upload` = "' . $data . '" LIMIT 20') or die(mysql_error());
  $data = mysql_fetch_assoc($data_query);
  
  echo "<p>Click to view the logo: <a href='view.php?Spn_PK=" . $data['Spn_PK'] . "'>logo " . $data['Spn_PK'] . "</a>";
}
else {
   print "No logo selected/uploaded";
}

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

 

<?php

$Spn_PK = $_GET['Spn_PK'];

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

$query = mysql_query('SELECT * FROM `Sponsor_Spn` WHERE `Spn_PK`= "'.$Spn_PK.'"');
$row = mysql_fetch_assoc($query);
$content = $row['Upload'];

header('Content-type: ' . $row['type'] .'');
print $content;

}

?>

I get these error with this

Warning: fread(): supplied argument is not a valid stream resource in /home/install/insert.php on line 14

 

Warning: fclose(): supplied argument is not a valid stream resource in /home/install/insert.php on line 16

Thank you, your file has been uploaded.Unknown column 'id' in 'field list'

Link to comment
Share on other sites

I have already done the searching for contra10 here.  I have pieced together this code from others that have tried.  This will work for you, but I won't be able to get to your specific  issue until this evening something.  Hang in there and I will give you a hand with this.

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.