Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,972 members, 7,817,859 topics. Date: Saturday, 04 May 2024 at 09:26 PM

Php Question - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / Php Question (1145 Views)

Lil Php Question (2) (3) (4)

(1) (Reply) (Go Down)

Php Question by worldbest(m): 2:35pm On Mar 28, 2008
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
Re: Php Question by my2cents(m): 4:08pm On Mar 28, 2008
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 b4 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.
Re: Php Question by worldbest(m): 4:22pm On Mar 28, 2008
Ok thanks alot,so for a social networking site or dating site with gallery and all,which is adviceable to use?
Re: Php Question by Cactus(m): 4:24pm On Mar 28, 2008
I recommend zenphoto, it is light easy simple
Re: Php Question by worldbest(m): 4:31pm On Mar 28, 2008
Ok thanks alot,so for a social networking site or dating site with gallery and all,which is adviceable to use?
Re: Php Question by my2cents(m): 4:35pm On Mar 28, 2008
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.
Re: Php Question by mambenanje(m): 7:29pm On Mar 28, 2008
@poster
if its not bad dont 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
Re: Php Question by my2cents(m): 7:39pm On Mar 28, 2008
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.
Re: Php Question by worldbest(m): 2:03am On Mar 29, 2008
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 dont 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.Pls dont ask me 2 go use google,u can help here so other can learn.Thanks again
Re: Php Question by my2cents(m): 12:03pm On Mar 29, 2008
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.
Re: Php Question by Nobody: 1:09pm On Mar 29, 2008
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

<?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", ""wink
OR DIE (mysql_error());
@mysql_select_db ("databaseName", $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"wink;
if (is_uploaded_file ($_FILES['userfile']['tmp_name'])) {
$userfile = addslashes (fread
(fopen ($_FILES["userfile"]["tmp_name"], "r"wink,
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"wink;
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"wink;
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

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 dont 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, cos it make it easier to change directory names later on.)
Re: Php Question by mambenanje(m): 1:27pm On Mar 29, 2008
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
Re: Php Question by worldbest(m): 2:51pm On Mar 29, 2008
@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.
Re: Php Question by worldbest(m): 2:57pm On Mar 29, 2008
@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.
Re: Php Question by Nobody: 3:07pm On Mar 29, 2008
do you have mysql running on a different server from the one hosting your files?
Re: Php Question by worldbest(m): 5:08pm On Mar 29, 2008
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?
Re: Php Question by worldbest(m): 5:21pm On Mar 29, 2008
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?
Re: Php Question by my2cents(m): 10:45pm On Mar 29, 2008
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.
Re: Php Question by worldbest(m): 9:28am On Mar 30, 2008
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.
Re: Php Question by worldbest(m): 9:56am On Mar 30, 2008
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.
Re: Php Question by Nobody: 11:47am On Mar 30, 2008
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

(1) (Reply)

Do You Know Of Any Nigerian Ecommerce Store With Interswitch Or Etransact? / I Am Giving Out Inical.com Marketing Management Script - Php / Is Google Down?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 39
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.