PHP & MySQL :: Need tutorial!

Associate
Joined
21 Sep 2007
Posts
453
I am looking for information on how to add a small profile of a person (name, description, email etc) and upload an image that will be saved to a folder on the server

The only tutorials i can find deal with uploading an image into the MySQL DB as a BLOB object, and then it only deals with the image, not the rest of what i want.

(I can do the text part, but i have no idea how to mix the two if i did know the image bit)

The tutorial on W3C works, but doesnt do what i want. It simply uploads the image and echo's the details, no storing any info in the DB, and uses two pages to works, i would like it contained on one page.

Thanks in advance
 
Associate
Joined
16 May 2005
Posts
680
Basics, few security bits in there but you can also check for mime type.

PHP:
// Original filename
$original_filename = $_FILES['image_file']['name'];

// Get the original extension
$extension = strtolower(substr(strrchr($original_filename,"."),1));

// Create a new name for the image file
$image_file = "image-" . $username . "." . $extension;

// The path where the image needs saving
$copy_to = "../_image-folder/" . $image_file;

// Copy the file to your folder as long as it's an ok file type
if($extension == "bmp" || $extension == "gif" || $extension == "jpg")
{
copy($_FILES['image_file']['tmp_name'],$copy_to);
}

// Update the profile
$query = "UPDATE profiles SET image_file='$image_file' WHERE profile_id='$profile_id'";
$result = mysql_query($query) or die(mysql_error());
 
Associate
OP
Joined
21 Sep 2007
Posts
453
Thanks for the code, I can see how it works, yet i'm unsure how i would add this into my page with the form, i.e. how to call the function etc.

Sorry to be a pain!

*not so ninja edit* I got stuff working, sadly not using your code, although i will look into streamlining stuff and your code might prove useful! I spent a good while figuring out the W3C tutorial!
 
Last edited:
Soldato
Joined
18 Oct 2002
Posts
4,655
Location
The Darkside
I use 'move_uploaded_file' instead of 'copy' as I read from several places that it was a better choice. i.e.

This function moves an uploaded file to the specified location. For security reasons, it is a good idea to always use this function on uploaded files. move_uploaded_file() checks that the file is an uploaded file before moving it. If the file was successfully moved, TRUE is returned. Otherwise FALSE is returned.

Not sure how true it is but I have used it in the past.
 

Sic

Sic

Soldato
Joined
9 Nov 2004
Posts
15,365
Location
SO16
if it's images you're uploading, use the GD library as they're more reliable for checking types and whatnot - the part of the $_FILES array that tells you the mime type works off headers sent by the browser that are easy to spoof, so don't rely on them too heavily. You can also control the size of the image which is pretty nice.
 
Back
Top Bottom