PDA

View Full Version : saving images to a MySQL database


DeMuro1
07-01-2003, 08:01 AM
Is it even possible to save images to a MySQL database?

Basically, if someone E-Mails some fan art to website-staffer_1, I'd like them to be able to upload the image to the website. Is this possible?

If it is can someone let me know how

Thanks

Gavin B.
07-05-2003, 11:37 AM
Yes - this is how the vBulletin image upload features work at the moment. I'm more tempted to upload them to a directory however.

You can setup a simple form with a 'browse' button which allows users to upload images to a specified folder fairly easily which will upload and transfer the images to the folder. You can specify a pointer in mysql like /images/imagename.jpg :)

DeMuro1
07-07-2003, 06:55 AM
how would I go about setting up a directory that I could load into? I thought about just making the MySQL point to a folder but I don'r quite know how to do it.. Any suggestion would be greatlt appreciated

Dean C
07-07-2003, 11:25 AM
I've always wondered how people upload the files directly into the database though. I mean the attachments in vb aren't stored in a temporary directory are they?

I can create the form with a pointer to the file easily enough but uploading directly to the database is out of my league at the moment. Any tutorials or anything?

- miSt

DeMuro1
07-07-2003, 06:59 PM
my question as well. any help here would be greatly appreciated

Gavin B.
07-08-2003, 03:21 AM
Try this:

You'll have to change the "newpath" line to match your own server (usually /home/youraccountusername/public_html/anotherfolder/)

anotherfolder is the folder that you want the imaged uploaded to, and it must be chmod'ed to 777 :)

(only setup to upload jpg files atm though :))


<?
if($HTTP_POST_VARS['upload'])
{
$userfile = $HTTP_POST_FILES['userfile']['tmp_name'];
$userfile_name = $HTTP_POST_FILES['userfile']['name'];
$userfile_size = $HTTP_POST_FILES['userfile']['size'];
$userfile_type = $HTTP_POST_FILES['userfile']['type'];

if($userfile != "" && $userfile_size != 0 && $userfile_type == "image/jpg" && is_uploaded_file($userfile)) {
$newfile = "/home/pathto/public_html/yourfoldername/".$userfile_name;
copy($userfile, $newfile);
echo "Uploaded";
}
}
?>

<html>
<head>
<title>Image Upload</title>
</head>
<body>

<h1>Upload Image</h1>

<form enctype="multipart/form-data" action="<?=$PHP_SELF?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="200000">
Select Your Image: <input name="userfile" type="file"><br />
<input type="submit" name="upload" value="Upload Image">
</form>

</body>
</html>

Dean C
07-08-2003, 10:44 AM
You see i know how to do that nice and easily but can't u upload to an actual mysql database like in vb's attachments?

- miSt

Gavin B.
07-08-2003, 11:16 AM
I prefer it this way since it puts less load on mysql and doesn't mess around.

I think the main reason vB have done it through mysql is for compatability reasons, but I'm not 100% on that.

Brad
07-09-2003, 04:54 AM
IMO its better to put them in a directory because it keeps the DB smaller.

DeMuro1
07-09-2003, 06:17 AM
wow holy smokes that's great. I might have a few questions about this a little later but that's great thanks a million

time for questions now.....I just can't deprioritize this

ok so this I take it stores the info in a directory...Right?

what is the directory name?

what is the file named or saved as...and is it possible to name the file manually, say by adding more form fields

if I wanted to allow the upload of more than one image at a time would it be possible with the use of a loop?

if I wanted to make a script that would upload this in a thumbnail form and then have a clickable thumbnail fo an enlarged image would I do that as I call the image to be displayed on the actual web page?

is there a way do view the directory say through the SmartFTP program I use and if so how do I do that

how can I modify this to upload different types of files?

do I just add a php/MySQL insert staement to insert the name into my database and then use it as a pointer?

Sorry for the 20 questions thing but I'm a little new to this and what you have there is greatly beyond what I know how to do. Can I give you credit by your name here when I open my site? I hate plagerism and theft

Brad
07-09-2003, 07:18 AM
Demuro, I dont have time to answer all your questions but ill get a few.

ok so this I take it stores the info in a directory...Right?

Yes

what is the directory name?

Its defined here:

$newfile = "/home/pathto/public_html/yourfoldername/".$userfile_name;

Just change yourfoldername in the code to whatever you want :)

and is it possible to name the file manually, say by adding more form fields

I would recomend naming them with a md5 hash, its just that much secure. Check out scott's attachments as files hack. Im sure you can get the needed code from it.

is there a way do view the directory say through the SmartFTP program I use and if so how do I do that

of course, its lives on your server just like your forum directory

do I just add a php/MySQL insert staement to insert the name into my database and then use it as a pointer?

Yep :)

DeMuro1
07-09-2003, 07:48 AM
I swear you guys have got to be the smartest lot in the universe. I so from your info anime loo I know how to do everything else I want.

Thank you all so very much
I am extremely greatful

Gavin B.
07-09-2003, 11:02 AM
Today at 09:48 AM DeMuro1 said this in Post #12 (https://vborg.vbsupport.ru/showthread.php?postid=416368#post416368)
I swear you guys have got to be the smartest lot in the universe.

:banana: I try my best :D lol

Lesane
07-09-2003, 11:27 AM
07-07-03 at 02:25 PM Mist said this in Post #4 (https://vborg.vbsupport.ru/showthread.php?postid=415910#post415910)
I've always wondered how people upload the files directly into the database though. I mean the attachments in vb aren't stored in a temporary directory are they?

I can create the form with a pointer to the file easily enough but uploading directly to the database is out of my league at the moment. Any tutorials or anything?

- miSt

Mist, this tutorial will probably help you:
http://www.devarticles.com/art/1/68

Dean C
07-09-2003, 02:39 PM
Excellent Lesane - thankyou :)!

- miSt

Lesane
07-09-2003, 03:22 PM
You're welcome, once you readed that tutorial you probably think 'aah never thought it would be that easy' ;)

DeMuro1
07-10-2003, 08:32 AM
last question I hope

if I modify this line
&& $userfile_type == "image/jpg"
to read
&& $userfile_type == "image/file type I want"
can I upload any type of file?

and if I add succesive strings of this
&& $userfile_type == "image/file type I want"
Then I should be able to upload multiple types of file.

is that right or does this upload only work with jpg images? If it does how can I allow it to upload gif images?

Thanks

and again can I I list you on my site as having written this portion or would you rather remain annonymous?

Gavin B.
07-10-2003, 11:04 AM
Yes, you can change that to be whatever you like. Just be careful you don't allow any sort of files which could possibly be dangerous.

.gif, .png and jpg are the common extensions I would check for. :)

Gavin B.
07-10-2003, 11:07 AM
Today at 10:32 AM DeMuro1 said this in Post #17 (https://vborg.vbsupport.ru/showthread.php?postid=416663#post416663)
and again can I I list you on my site as having written this portion or would you rather remain annonymous?

It's only a snippet, I'm sure there's a zillion variations of it floating around, so personally I don't want credit for it. :) Hope it helped out though.

Dean C
07-10-2003, 03:01 PM
Wow that upload script was easy to use. Only problem was that when uploading images only part of the image was uploaded so there's a little bug there. But apart from that i understand the concept :)

- miSt

DeMuro1
07-13-2003, 04:59 AM
ok so I'm having problems making this work. Any suggestions? Do I perhaps need to enter a password somewhere? ANy help would again be greatly helpful

DeMuro1
07-17-2003, 09:59 AM
ok so I have the script loaded onto my server as a .php file. That part works really well. It even seems to upload the imagebut when I check the desired directory I get nothing. is it possible that I'm not entering the file location properly?

eg.
$newfile = "/home/pathto/public_html/yourfoldername/".$userfile_name;

yourfoldername = http://www.bobsnetplace.net/images

or should I just be putting images

eg.

yourfoldername = images

if I upload the images from my computer harddrive do I need to modify the script/

maybe add a line that looks like this

<input type="text" value="name" name="file_name" maxlength="100" size="100"><br>

to the form and then maybe a line like this

$userfile_file_name = $HTTP_POST_FILES['userfile']['file_name'];

could these things maybe solve my problems?

Please help I'm still struggling with this

Gavin B.
07-17-2003, 12:47 PM
does it give you any errors? :)

You'll need to edit that path, it depends on how the server is set up, but if you use the username you use to login to FTP (replace the red part below) and the folder you wish to upload to (the blue part). Make sure the folder has permissions set to 777 :)

/home/username/public_html/yourfoldername/

DeMuro1
07-17-2003, 07:17 PM
no errors. once I click the upload button it just sort of reloads the page it's really anoying. it also says opening:......some page info here..... at the very bottom of the screen. I'll try what you just gave me I really want this to work

Thanks and sorry for being a pill

cindyd
07-30-2003, 09:53 PM
Hi Gavin: I had been looking for something like this for the past few days!

I am pretty new to hacking, but I am learning. I have a couple of questions about this:

You can setup a simple form with a 'browse' button which allows users to upload images to a specified folder fairly easily which will upload and transfer the images to the folder. You can specify a pointer in mysql like /images/imagename.jpg

1. The php script you wrote on page 1, I know how to upload it, but where do I upload it to? I put it in my forum directory.

2. A simple form with a browse button, OK, I think I can handle that too. I did this, but I have no idea how to incorporate it all so the browse or upload form will come up in a "new post".

When I regularly upload images, I have to use a password, is that figured into the script?

Thanks for the help.