Jump to content


Photo

Need help storing image links in db


  • Please log in to reply
13 replies to this topic

#1 Stevis2002

Stevis2002
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 23 April 2006 - 02:18 PM

Hi all,

Please could somebody help me to store an image link in mysql database instead of storing the actual image.

EG. users uploads file which goes into images folder on webserver, and at same time, a link to it gets saved in the db for future reference.

Thanks

#2 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 23 April 2006 - 02:25 PM

[!--quoteo(post=367683:date=Apr 23 2006, 03:18 PM:name=Amanda Griffiths)--][div class=\'quotetop\']QUOTE(Amanda Griffiths @ Apr 23 2006, 03:18 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Hi all,

Please could somebody help me to store an image link in mysql database instead of storing the actual image.

EG. users uploads file which goes into images folder on webserver, and at same time, a link to it gets saved in the db for future reference.

Thanks
[/quote]

Hi
Which part are you stuck on? are you able to upload the file to the correct place on the web server?

if so, then it's simply a case of saving the filename you used within 'move_uploaded_file' in your database, along with any other information from the $_FILES array you want (such as size, type, original filename, etc).

if you can give a better idea of which part you're having trouble with, it maybe easier to help you out.

cheers
Mark
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#3 Stevis2002

Stevis2002
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 23 April 2006 - 02:28 PM

I can store the uploaded file into the images directory on the webserver, but then i don't know how to store the link to the image into the database

Many Thanks

#4 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 23 April 2006 - 02:33 PM

[!--quoteo(post=367686:date=Apr 23 2006, 03:28 PM:name=Amanda Griffiths)--][div class=\'quotetop\']QUOTE(Amanda Griffiths @ Apr 23 2006, 03:28 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I can store the uploaded file into the images directory on the webserver, but then i don't know how to store the link to the image into the database

Many Thanks
[/quote]

hi
if you've used move_uploaded_file to do the job (which it sounds like you have), then youre half way there.
the second parameter you use (ie, the destination path/filename) is all you need to store and retrieve when you need it.
generally when i'm storing images on my server (from uploads) i keep the upload path the same (and set by a variable in an include file, such as $upload_path), so then all i need to store in my database is the destination filename and not any path information. when i wanna retrieve them, i can either reference them as a url (ie, [a href=\"http://www.mysite.com/uploads/\" target=\"_blank\"]http://www.mysite.com/uploads/[/a]myimage.jpg) or as a full path (/myserverpath/public_html/uploads/myimage.jpg).

is it the actual mysql stuff youre having trouble with?

cheers
Mark
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#5 Stevis2002

Stevis2002
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 23 April 2006 - 02:49 PM

Thanks for the help mark

I am using dreamweaver, and when i try to upload the picture, i get the error...here is my code

<form action="/admin/upload_script.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
Select Image
<label>
<input type="file" name="file" />
<br />
<input type="submit" name="Submit" value="Submit" />
</label>
</form>

and the handling script...

<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.

$uploaddir = 'c:/domains/xxxxxxxxx/wwwroot/images/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}

echo 'Here is some more debugging info:';
print_r($_FILES);

print "</pre>";

?>

Now i'm stuck before i even get to the part of adding the link into the database :(

Thanks again

#6 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 23 April 2006 - 02:59 PM

hi,
not sure what error youre getting, but i'm guessing it's gonna be a permissions thing. have you made sure that the c:/...blahblah.../images directory is 'writable' by the script? by default, it wont be, so you'll need to change this so that you can write to the images directory.

try that first, see how it goes, then onto round two...

cheers
Mark
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#7 Stevis2002

Stevis2002
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 23 April 2006 - 03:41 PM

[!--quoteo(post=367693:date=Apr 23 2006, 03:59 PM:name=redbullmarky)--][div class=\'quotetop\']QUOTE(redbullmarky @ Apr 23 2006, 03:59 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
hi,
not sure what error youre getting, but i'm guessing it's gonna be a permissions thing. have you made sure that the c:/...blahblah.../images directory is 'writable' by the script? by default, it wont be, so you'll need to change this so that you can write to the images directory.

try that first, see how it goes, then onto round two...

cheers
Mark
[/quote]


Thanks again Mark,

I know have images uploading into the correct directory on the web server, and i have got the path saved in the db.

Any ideas what is wrong here though...it doesn't want to upload the other information which goes with the images..

if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fileArtname = $_FILES['userfile']['artist_name'];
$fileAlbtitle = $_FILES['userfile']['album_title'];
$fileAlbinfo = $_FILES['userfile']['album_info'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}


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

$query = "INSERT INTO upload2 (name, size, type, path, artist_name, album_title, album_info) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath', '$fileArtname', '$fileAlbtitle', '$fileAlbinfo')";

It inserts the image, the path and the filename, but none of the rest.

Thanks

#8 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 23 April 2006 - 03:45 PM

i don't get this bit.
$fileArtname = $_FILES['userfile']['artist_name'];
$fileAlbtitle = $_FILES['userfile']['album_title'];
$fileAlbinfo = $_FILES['userfile']['album_info'];
artist_name, album_title, etc, arent proper values for the $_FILES array. maybe you should be using $_POST['album_title'], $_POST['album_info'] , etc, instead...

cheers
Mark
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#9 Stevis2002

Stevis2002
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 23 April 2006 - 04:51 PM

Thanks Mark,

The post statements did the trick.

I just need to figure out the best way of calling the info back and displaying it on screen.

I don't suppose you have a line or 2 of code to set me in the right direction, as i'm not sure about how to disp[lay the uploaded image via the link which is stored in the db.

Many Thanks again

#10 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 23 April 2006 - 05:09 PM

[!--quoteo(post=367717:date=Apr 23 2006, 05:51 PM:name=Amanda Griffiths)--][div class=\'quotetop\']QUOTE(Amanda Griffiths @ Apr 23 2006, 05:51 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Thanks Mark,

The post statements did the trick.

I just need to figure out the best way of calling the info back and displaying it on screen.

I don't suppose you have a line or 2 of code to set me in the right direction, as i'm not sure about how to disp[lay the uploaded image via the link which is stored in the db.

Many Thanks again
[/quote]

Hi
Basically:
(ignore the syntax i've used - its purely to give an idea...)

the php bit:

<?php
$query = "select * from the_database where id = '$the_image_id_i_want'";
$result = mysql_query($query);
$picture = mysql_fetch_assoc($result);
?>

the html bit:
<img src="<?php echo $picture['path']; ?>" />


the only thing to watch in the above example is how the 'path' was stored in the DB in the first place. i.e, is it a URL (http://blahblah.com/images/blah.jpg) or a full path (i.e, /myserver/wwwroot/blah.jpg). if it's the former, your fine.
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#11 Stevis2002

Stevis2002
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 23 April 2006 - 05:23 PM

It's the full path...

C:/domains/xxxxxxx/wwwroot/images/givea....jpg

So far i have this to output the data from every record...


$sql = "SELECT * FROM upload2";
$data = mysql_query($sql);



while($my_data = mysql_fetch_array($data))
{

printf("%s,<br> %s,<br> %s,<br> %s, <br />",
$my_data['artist_name'], $my_data['album_title'], $my_data['album_info'], $my_data['path']);
}


This is fine, although a bit nasty looking, but i will sort that after i have everything listing ok....probably use tables to lay it out.

I can't seem to find a way of turning the image path, into the actual image though.

Thanks

I have managed to change the stored url to ../images/erg4tr.jpg now, but if i change it to [a href=\"http://www.sgrt.co.uk/images/\" target=\"_blank\"]http://www.sgrt.co.uk/images/[/a], then it doesn't work

#12 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 23 April 2006 - 05:27 PM

well the way you've stored your paths is relative to the server root, so is not a URL. the <img> tag needs a URL or a path relative to the site/file. eg: <img src="http://site.com/images/image.jpg" /> or <img src="/images/image.jpg" /> or <img src="../images/image.jpg" />

the 'basename' function you used should do the trick. just do:

<?php
// following line could also be http://mysite.com/images/
$upload_path = "/images/";

... get the database stuff here ...

$filename = $upload_path.basename($my_data['path']);
?>


<img src="<?php echo $filename; ?>" />



that should give you a better idea

edit: also, i wouldnt recommend you store anything other than the filename in the DB anyway. if you ever change your server or directory names, all the DB filenames will be broken links. if you store the path in variables (for example, in a config.php file which you include into your pages as required), then all you need to store in the DB is just the filename (eg, myimage.jpg and not c:/blahblah/blah/wwwroot/images/myimage.jpg)
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#13 Stevis2002

Stevis2002
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 23 April 2006 - 05:42 PM


edit: also, i wouldnt recommend you store anything other than the filename in the DB anyway. if you ever change your server or directory names, all the DB filenames will be broken links. if you store the path in variables (for example, in a config.php file which you include into your pages as required), then all you need to store in the DB is just the filename (eg, myimage.jpg and not c:/blahblah/blah/wwwroot/images/myimage.jpg)
[/quote]


Ok, guess i will start again then as i cant get it working properly anyway.

Just get the image holder, and when i right click it just tells me that the image location should be admin, when it shouldn't.

I'll look aroubnd and try and find out what you are going on about with the config.php as i dont understand, and i should imagine your patience is wearing a bit thin with me know :)

Thanks for all the help

#14 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 23 April 2006 - 05:50 PM

[!--quoteo(post=367728:date=Apr 23 2006, 06:42 PM:name=Amanda Griffiths)--][div class=\'quotetop\']QUOTE(Amanda Griffiths @ Apr 23 2006, 06:42 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]

I'll look aroubnd and try and find out what you are going on about with the config.php as i dont understand, and i should imagine your patience is wearing a bit thin with me know :)

[/quote]

sorry i may have not been clear on this. its just something i do personally when dealing with things like this. basically i have a config.php which i store stuff that's used sitewide. bit like having a CSS stylesheet that stores all your styles - well config.php stores all my settings, including paths. for example:

config.php
$upload_path = "c:/domains/xxxxxxxxx/wwwroot/images/";
$upload_path_url = "http://www.mysite.com/images/";

in your file that deals with the file upload/saving/retrieval, etc, you would just have:
<?php
include_once('/path/to/config.php');

...rest of code here
?>

all it does is just keeps things consistent and in one place, and would remove the need to store full paths in your database. if you moved servers or changed directory names or anything else, all you would need to do is to change the settings in config.php file, rather than having to manually change every record in your database.
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users