Php Question

A Member? Please Login  
type your username and password to login
Date: November 22, 2008, 11:40 AM
263860 members and 160913 Topics
Latest Member: frank30fun
Nairaland [Nigerian Forum] Home Help Search Who is currently online? Login Register
Nairaland Forum  |  Technology  |  Webmasters (Moderators: Sam Milla, uspry1)  |  Php Question
Pages: (1) Go Down Send this topic Notify of replies
Author Topic: Php Question  (Read 189 views)
worldbest (m)
Php Question
« on: March 28, 2008, 02:35 PM »

Hello everyone,am currently working on a site which involves image upload,i was wondering if its ok to save the image url/directory in the database rather than the image itself.Although,i have succeeded in savin the image directory and when referencing from the DB it works pretty well and neat and i also mad a function that deletes the image from the DB and in the image directory whenever the user deletes an image.I need to know the right thing to do.Thanks
my2cents (m)
Re: Php Question
« #1 on: March 28, 2008, 04:08 PM »

As with all thingsin life, it depends.

What kind of a site is it?  Do you have dedicated servers or the regular web hosting accounts?  How busy is your site?  These are some of the questions you need to ask yourself before u make that decision.

If you are getting just a few hits per day/month and not dealing with a lot of images, I would say store just the paths to the images, for storage reasons.  Otherwise, keep it as is.

I hope this helps.
worldbest (m)
Re: Php Question
« #2 on: March 28, 2008, 04:22 PM »

Ok thanks alot,so for a social networking site or dating site with gallery and all,which is adviceable to use?
Cactus (m)
Re: Php Question
« #3 on: March 28, 2008, 04:24 PM »

I recommend zenphoto, it is light easy simple
worldbest (m)
Re: Php Question
« #4 on: March 28, 2008, 04:31 PM »

Ok thanks alot,so for a social networking site or dating site with gallery and all,which is adviceable to use?
my2cents (m)
Re: Php Question
« #5 on: March 28, 2008, 04:35 PM »

again, as I stated earlier, it depends on your available bandwidth and DB space.  To answer your question as-is, I would say go with storing the actual images in a DB or paying an external host to store them for you and then via your SQL, you access that DB for your photos.
mambenanje (m)
Re: Php Question
« #6 on: March 28, 2008, 07:29 PM »

@poster
 if its not bad don't fix it, so I guess your current solution is working if so move on, and when it starts giving you trouble now you have a reason of fixing it.
 so if its working and its not costing you much on server and db, then go with it, else look for a better solution
my2cents (m)
Re: Php Question
« #7 on: March 28, 2008, 07:39 PM »

I guess Mambe has pretty much said it all.  Your question would have made more sense if you hadn't started but since you already have something going, stick with it and hope that you have no reason to refactor OR refactor if you have nothing better to do  Cool

In the future, try to think out (and ask questions where applicable) various possible solutions to a problem and determine the best solution before implementing.  Refactoring definitely costs more in the long-run than if you had actually sat down to come up with the best possible solution upfront.
worldbest (m)
Re: Php Question
« #8 on: March 29, 2008, 02:03 AM »

Thanks guys.Actually,am currently working locally,what i want to do is a site that allows users 2 upload images for their profiles and their personal galleries,with this i knw i will be dealing with lots of images,so don't u think savin in DB will be sorta difficult when a time comes to move to another hostin company(am nt so sure of what am sayin though)then,just savin in a file can get too populated and cost more right?I have that of saving the path but as my2cent has said its better 2 do something right at this stage 2 avoid probs later. I tryd savin the image in th DB but when i try 2 retrieve i get the path to the image instead.please don't ask me 2 go use google,u can help here so other can learn.Thanks again
my2cents (m)
Re: Php Question
« #9 on: March 29, 2008, 12:03 PM »

I have never saved images in a DB so I couldn't answer the path part.  I do have a question though?  How are you saving the image in the DB? Just in case, I think you need to save it as a BLOB. 

That is the extent to my knowledge with respect to storing images in a DB, since as I said, I haven't done it myself.  I hereby yield to others to take it up from here.
webdezzi (m)
Re: Php Question
« #10 on: March 29, 2008, 01:09 PM »

to store it in database you need to use blob as 2cents said

when outputting, you need to specify the content-type

here is a tutorial i got for you from codewalkers.com

Code:
<?php
// index.php - by Hermawan Haryanto &lt;hermawan@codewalkers.com&gt;
// Example PHP Script, demonstrating Storing Image in Database
// Detailed Information can be found at http://www.codewalkers.com

// database connection
$conn = mysql_connect("localhost", "root", "")
  OR DIE (
mysql_error());
@
mysql_select_db ("[b]databaseName[/b]", $conn) OR DIE (mysql_error());

// Do this process if user has browse the
// file and click the submit button
if ($_FILES) {
  
$image_types = Array ("image/bmp",
                        
"image/jpeg",
                        
"image/pjpeg",
                        
"image/gif",
                        
"image/x-png");
  if (
is_uploaded_file ($_FILES['userfile']['tmp_name'])) {
    
$userfile  = addslashes (fread
                 
(fopen ($_FILES["userfile"]["tmp_name"], "r"),
                 
filesize ($_FILES["userfile"]["tmp_name"])));
    
$file_name = $_FILES["userfile"]["name"];
    
$file_size = $_FILES["userfile"]["size"];
    
$file_type = $_FILES["userfile"]["type"];

    if (
in_array (strtolower ($file_type), $image_types)) {
      
$sql = "INSERT INTO image "
             
. "(image_type, image, image_size, image_name, image_date) ";
      
$sql.= "VALUES (";
      
$sql.= "'{$file_type}', '{$userfile}', '{$file_size}', "
             
. "'{$file_name}', NOW())";
      @
mysql_query ($sql, $conn);
      
Header("Location:".$_SERVER["PHP_SELF"]);
      exit();
    }
  }
}

// Do this process of user has click
// a file name to view or remove
if ($_GET["act"]) {
  
$iid = $_GET["iid"];
  
$act = $_GET["act"];
  switch (
$act) {
    case
rem:
      
$sql = "DELETE FROM image WHERE image_id=$iid";
      @
mysql_query ($sql, $conn);
      
Header("Location:./index.php");
      exit();
      break;
    default:
      print
"<img src=\"image.php?iid=$iid\">";
      break;
  }
}
if (
$_GET["iid"]) {
$sql    = "SELECT * FROM image WHERE image_id=".$_GET["iid"];
$result = mysql_query ($sql, $conn);
if (
mysql_num_rows ($result)>0) {
  
$row = @mysql_fetch_array ($result);
  
$image_type = $row["image_type"];
  
$image = $row["image"];
  
Header ("Content-type: $image_type");
  print
$image;
}}
?>

<html>
<head>
<title>Storing Images in DB</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
Select Image File:
<input type="file" name="userfile"  size="40">
<input type="submit" value="submit">
</form>
<?php
  $sql
= "SELECT * FROM image ORDER BY image_date DESC";
  
$result = mysql_query ($sql, $conn);
  if (
mysql_num_rows($result)>0) {
    while (
$row = mysql_fetch_array($result, MYSQL_ASSOC)) {
      
$i++;
      
$str .= $i.". ";
      
$str .= "<a href='index.php?iid=".$row["image_id"]."'>"
           
. $row["image_name"]."</a> ";
      
$str .= "[".$row["image_date"]."] ";
      
$str .= "[".$row["image_size"]."] ";
      
$str .= "[<a href='index.php?act=rem&amp;iid=".$row["image_id"]
           .
"'>Remove</a>]<br>";
    }
    print
$str;
  }
  
?>

</body>
</html>

first, you need to run this sql statement
Code:
CREATE TABLE image(
  image_id int(10) unsigned NOT NULL auto_increment,
  image_type varchar(50) NOT NULL default '',
  image longblob NOT NULL,
  image_size bigint(20) NOT NULL default '0',
  image_name varchar(255) NOT NULL default '',
  image_date datetime NOT NULL default '0000-00-00 00:00:00',
  UNIQUE KEY image_id (image_id)


But from my point of view, i don't think its a good idea to store images in databases
i love to be able to move my images around without having to rely on mysql server to interprete the blob for me

Again, I might want to update the images using FTP

another thing is that it is slower reading images from database

what i do is store images in directories and keep the file name in the database(excluding the directory, because it make it easier to change directory names later on.)


mambenanje (m)
Re: Php Question
« #11 on: March 29, 2008, 01:27 PM »

Quote
But from my point of view, i don't think its a good idea to store images in databases
i love to be able to move my images around without having to rely on mysql server to interprete the blob for me

Again, I might want to update the images using FTP

another thing is that it is slower reading images from database

what i do is store images in directories and keep the file name in the database(excluding the directory, because it make it easier to change directory names later on.)

I support that too. I have used it on my sites (www.ubstudents.com) and its easy to maintain but the problem comes only when you run a very big site with millions of users where you have to do virtual servers and use a grid of computers to power the site,
 in a case where you think you will need to use grid of computers then I prefer you go with the other option where you encode the picture and store it as blog like in the tutorial. This way if a user uploads his picture in server A and he logins in again and he is being served from serverB his pictures will come from the database which will be one.
 hey if you don't get this second part then you don't need it.
Just follow the first option store name in db and store picture in folder. easy one Wink
worldbest (m)
Re: Php Question
« #12 on: March 29, 2008, 02:51 PM »

@webdezzi thanks for the code and to everyone.I have decided 2 stick with just savin the path in the DB cus i think it wil be faster when retrieving.Soon,i'll post my works here.
worldbest (m)
Re: Php Question
« #13 on: March 29, 2008, 02:57 PM »

@webdezzi thanks for the code and to everyone.I have decided 2 stick with just savin the path in the DB cus i think it wil be faster when retrieving.Soon,i'll post my works here.
webdezzi (m)
Re: Php Question
« #14 on: March 29, 2008, 03:07 PM »

do you have mysql running on a different server from the one hosting your files?
worldbest (m)
Re: Php Question
« #15 on: March 29, 2008, 05:08 PM »

No.Am still doing the localhost thingy.No hosting involved yet.Is it possible for a webhosting company to create a folder where all my images will be stored,i have included random numbers to the image names to make them sorta unique.What do u think?
worldbest (m)
Re: Php Question
« #16 on: March 29, 2008, 05:21 PM »

No.Am still doing the localhost thingy.No hosting involved yet.Is it possible for a webhosting company to create a folder where all my images will be stored,i have included random numbers to the image names to make them sorta unique.What do u think?
my2cents (m)
Re: Php Question
« #17 on: March 29, 2008, 10:45 PM »

I blv u hv to create the folders yourself.  As for image names with unique numbers, I want to think that when the user uploads their image, it will be uploaded with the name that they chose (that exists on their end).

Since you already have folders which I presume would be named uniquely (maybe the user's username), all you should be concerned with is how to display the images.

I hope this helps.
worldbest (m)
Re: Php Question
« #18 on: March 30, 2008, 09:28 AM »

Yes,the image names are the same with the users name but with random  numbers to avoid overwrittin themselves in d case of the gallery.
worldbest (m)
Re: Php Question
« #19 on: March 30, 2008, 09:56 AM »

Yes,the image names are the same with the users name but with random  numbers to avoid overwrittin themselves in d case of the gallery.
webdezzi (m)
Re: Php Question
« #20 on: March 30, 2008, 11:47 AM »

about creating folders for each user, i will rather have all images in the same folder and have the images in an easy to understand pattern

e.g.
//the username taken from session
$userName=$_SESSION['userSessioN'];

//the time
$time=time();

$imgName=$userName."_".$time

//and if its multiple upload then you can concat some serial numbers to it
//with this pattern, you can fish out each user's image based on the prefix

something like that sha
 Latest Uba Test  Dont Know Which Area Of I.t To Be On?i'm Confused: Experts Give Advice Here  My Blog: In-spiracio  Page 2
Pages: (1) Go Up Send Topic to Friend by E-mail Reply 
Google
 
Web www.nairaland.com
Sections: TV/Movies (2) Music/Radio (2) Celebrities Job Talk Jobs/Vacancies (2) Career Talk Romance Books Politics Sports Fashion Travel
Health Schooling Religion General(2) Business Webmaster Programming Computers Phones Cars & Trucks

Links: Page1 Page2 Page3 Page4 Page5 Page6 Page7 Page8 Page9 Page10

Nairaland is owned by Oluwaseun Osewa
Nairaland Forum | Powered by SMF 1.0.12.
© 2001-2005, Lewis Media. All Rights Reserved.