Jump to content

Archived

This topic is now archived and is closed to further replies.

skatermike21988

Upload Images??

Recommended Posts

ok i am working on a new members thing on my page and i want users to be able to upload an image to my site and the directory be stored in the database

so i can do something like this
<img src='$image'>

Now How Would I Do This So It Uploads It and Then Pulls It From The Databse To Be Displayed?

Share this post


Link to post
Share on other sites
[!--quoteo(post=356873:date=Mar 21 2006, 06:28 AM:name=Skater Mike)--][div class=\'quotetop\']QUOTE(Skater Mike @ Mar 21 2006, 06:28 AM) [snapback]356873[/snapback][/div][div class=\'quotemain\'][!--quotec--]
ok i am working on a new members thing on my page and i want users to be able to upload an image to my site and the directory be stored in the database

so i can do something like this
<img src='$image'>

Now How Would I Do This So It Uploads It and Then Pulls It From The Databse To Be Displayed?
[/quote]

without literally going through the entire code, there are 3 things to deal with. first is to set up the <FORM> with the necessary elements:

[code]
<form action="" method="post" enctype="multipart/form-data" name="form1">

...stuff here

<input name="imagefile" type="file" id="my_image_file">

...more stuff here including a submit button
[/code]

once the form is submitted, the file is automatically uploaded to the servers temporary directory. to deal with it from their, you need to use the $_FILES superglobal array to get the info you need about the file, and move_uploaded_file to move the file from its temporary location to where you want it to be permanently

[code]
$result = move_uploaded_file($_FILES['my_image_file']['tmp_name'], $newfilepath);
[/code]

all you need to do then is use some of the info from the $_FILES['my_image_file'] array to store info in the database for later.
- the file size, $_FILES['my_image_file']['size']
- the file type, $_FILES['my_image_file']['type']
- the original file name, $_FILES['my_image_file']['name']

and for error trapping, use $_FILES['my_image_file']['error']

in terms of database retrieval, you just pull the filename from the database and put it within the IMG's src.

hope that helps.

Share this post


Link to post
Share on other sites
Currently, my image upload pulls the image out from the db. Heres a sample.

[code]<?php

$imageid= $_GET["id"];
$result = mysql_query("SELECT * FROM files WHERE id=$imageid");
$myrow = mysql_fetch_array($result); ?>
<html>
<img src="<?php echo $myrow["idpath"];
</html>
[/code]

Of course, you need to connect to MySQL first. Also, if you don't like the html tags, you can just simple do

[code]echo '<img src="<?php echo $myrow["idpath"]';[/code]

Share this post


Link to post
Share on other sites

×

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.