Jump to content

Uploading Images In PHP


graham23s

Recommended Posts

Hi Guys,

 

What im trying to do is make a form with the usual username,password, and email fields but at the bottom before the form is submitted there is an option to upload a photo, i have made the code this:

 

<input name="userfile" type="file" size="50" />

 

theres no upload button as the main button at the bottom of the form is meant to pass all the fields including the upload file one,

 

but im not sure whats the best way to handle the upload, i put the "userfile" into variable on the submitted page but should that go into the MySQL database (the photo i mean) or is there another way to do this.

 

hope that made sense lol

 

cheers

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/41167-uploading-images-in-php/
Share on other sites

You can make it upload the file into a directory, then store the ending name of the file in the database.

 

Say your directories name was "photos" and the file the person just uploaded was named picture.jpg. You store the entire file into your directory, and then store picture.jpg into the database.

 

To display the picture you select the picture.jpg from the database and echo it like this.

 

<?php

$filename = "picture.jpg";
echo "<img src='photos/$filename'>";

?>

Hi mate,

 

I have made it so, when a user registers they are given a folder (called they're username) inside a members folder:

 

members/graham23s

 

That sounds like what i'm after thanks mate, so from the form i take the variable say it's "usersphoto" from the main registration page is that right?

 

cheers

 

Graham

Whatever variable holds the file's name that the user uploaded is what you want to insert into the database. You want to upload the entire file name into the users folder (members/graham23s).

 

Entire name:

www.your-site.com/members/graham23s/file.jpg

 

what you should store in the database:

graham23s/file.jpg

 

I don't know what your "userphoto" variable holds without seeing your code. Hopefully that will clear it up for you though.

 

 

Hi mate,

 

Thanks for your help.

 

all the "userphoto" bit was is this:

 

<input name="userphoto" type="file" id="ufile" size="50" />

 

on the main registration page i changed it to photo to make it easier.

 

when the page was submitted (along with the test .jpg) in MySQL it printed the entire path to the file instead of userspic.jpg,, is there a way to only have the pictures name rather than the full path.

 

C:\Documents and Settings\Graham\Desktop\Testpic.jpg

 

thanks a lot

 

Graham

Okay, you can store the file name in a variable like this.

 

$file = $_FILES["file"]["name"];

 

Then if you don't insert it into the database on the first page, you probably need to store it into a hidden field to get it to your second page where you can add it to your database.

Archived

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

×
×
  • 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.