Jump to content

[SOLVED] Images and Database


phatgreenbuds

Recommended Posts

Not sure I am approaching this from the right angle. I have this sweet little flash/actionscript design that I am using for my family site. problem is its dependant on an XML file for the thumbnails and actual images that it displays.

 

So I got the idea that I want to be able to have the inlaws and others upload images that would then dynamically update. So naturally I thought PHP and MySQL would be great for this.

 

I fiugured out how to create the XML file from PHP everytime a page loads. That was the easy part. I have the code below that I used for a simple upload page. Its far from complete and I will put some security in it so no bashing yet. The point is it works for now. Using phpmyadmin I see the entry in the database and I thought I was nearly there.

 

Now that I think about this I am not sure I did the right thing. I would actually need to upload the images to a directory on my server and instead of the image being placed in the database itself I should actually just put the path to the image.  That way I can use that path to write back to the XML file that references the images...right? Am I thinking clearly here?

 

The remaining issues I have are trying to duplicate the image uploaded and then resize it into a thumbnail. Not sure where to begin on that part...one step at a time. I have yet to figure out how to read the original image back into the web page so as a side note if someone could point me in the right direction there I would appreciate it.

 

<?php require_once("../includes2/dbopen.php"); ?>
<?php include("../functions2/functions.php"); ?>

<html>
<head>
<title>Upload Your Pic</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link type="text/css" rel="stylesheet" href="../stylesheets/styles.css" />
</head>

<body>
<strong><h3><a href="upload.php">Back to Main Menu</a><br></h3>
<h4>Upload your pics here. Please keep the size reasonable. The bigger the file the longer it takes to upload and the slower it will be for others to view.</h4></strong>

<hr>

<form method="post" enctype="multipart/form-data">
  Submitted By: 
  <br>  <input type="text" name="picsub" init value="">
  <br><br>
  Description: 
  <br>  <textarea name="picdesc" cols="20" rows="5" init value=""></textarea>
    <input type="hidden" name="MAX_FILE_SIZE" value="20000000">
  <br><br>
  File Name: 
  <br>  <input name="picname" type="file" id="picname">
  <br><br>
    <input name="upload" type="submit" class="box" id="upload" value="Upload">

</form>

<?php
if(isset($_POST['upload']) && $_FILES['picname']['size'] > 0)
{
$fileName = $_FILES['picname']['name'];
$tmpName  = $_FILES['picname']['tmp_name'];
$fileSize = $_FILES['picname']['size'];
$fileType = $_FILES['picname']['type'];
$sub = $_POST['picsub'];
$desc = $_POST['picdesc'];

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

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

$query="INSERT INTO images (picname, picsize, pictype, picfile, picsubmit, picdesc)
VALUES
('$fileName','$fileSize','$fileType','$content','$sub','$desc')";
$addpics = mysql_query($query);
confirm_query($addpics); //calls to a function in the functions file.

echo "<br>";
echo "<h4>$fileName was successfully added.</h4>";
echo "<hr>";
}
?>

</body>
</html>
<?php include("../includes2/dbclose.php"); ?>

Link to comment
Share on other sites

instead of the image being placed in the database itself I should actually just put the path to the image.  That way I can use that path to write back to the XML file that references the images...right?

 

yes.

 

The remaining issues I have are trying to duplicate the image uploaded and then resize it into a thumbnail. Not sure where to begin on that part...one step at a time.

 

there are a number of threads on this topic on the first page of the forums now. just gotta poke around.

Link to comment
Share on other sites

Thanks...I thought I knew but wanted to make sure.  This will actually solve the issue of reading the image back but will keep me dependant on that actionscript.  Is it possible or advisable to store images in the table and then read them back into a web page? I would think you have to so something with headers otherwise you end up with a bunch of jibberish.

Link to comment
Share on other sites

if you store the image in a directory, all you have to do is use a plain old HTML <IMG> tag:

 

<IMG SRC='/image_directory/image_name.gif'>

 

To answer your question more fully: It is NOT advisable to store images in a database except for rare, specific cases. And yes, when reading images from a database and sending them to a browser you do typically have to prefix the binary data with header information so the browser can correctly interpret the image data.

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.