Jump to content

Upload Images??


skatermike21988

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?
Link to comment
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.
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.