www.webdeveloper.com
Results 1 to 4 of 4

Thread: Attaching an image to a users profile

  1. #1
    Join Date
    Jun 2008
    Posts
    35

    Attaching an image to a users profile

    I currently have a site with the ability to register and have a user profile. I would like to have the ability for the users to have an avatar. I currently have a working image uploader:

    PHP Code:
    <?php

    echo('<font color="#FFFFFF">Upload images:


    <FORM ENCTYPE="multipart/form-data" ACTION="' 
    $PHP_SELF '" METHOD="POST">

    The file: <INPUT TYPE="file" NAME="userfile">

    <INPUT TYPE="submit" VALUE="Upload">

    </FORM>'
    );



    $path "images/";

    $max_size 20000;



    if (!isset(
    $HTTP_POST_FILES['userfile'])) exit;



    if (
    is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) {



    if (
    $HTTP_POST_FILES['userfile']['size']>$max_size) { echo "The file is too big<br>n"; exit; }

    if ((
    $HTTP_POST_FILES['userfile']['type']=="image/gif") || ($HTTP_POST_FILES['userfile']['type']=="image/pjpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/jpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/png")) {



    if (
    file_exists($path $HTTP_POST_FILES['userfile']['name'])) { echo "The file already exists<br>"; exit; }



    $res copy($HTTP_POST_FILES['userfile']['tmp_name'], $path .

    $HTTP_POST_FILES['userfile']['name']);

    if (!
    $res) { echo "upload failed!<br>"; exit; } else { echo "upload sucessful<br>"; }



    echo 
    "File Name: ".$HTTP_POST_FILES['userfile']['name']."<br>";

    echo 
    "File Size: ".$HTTP_POST_FILES['userfile']['size']." bytes<br>";

    echo 
    "File Type: ".$HTTP_POST_FILES['userfile']['type']."<br>";

    } else { echo 
    "Wrong file type<br></font>"; exit; }

    }



    $my_file $HTTP_POST_FILES['userfile']['name'];

    ?>
    Once the user has uploaded their image, how do i get that image to attach to the users profile?

    Cheers

    Dan

  2. #2
    Join Date
    Oct 2005
    Posts
    38
    i assume you are using a database? and have a users table?

    if not, i dont know how to do it.

    but, if you are using a DB then create a field for the location of the avatar.

    when you want to display the avatar use the user id to get the field and echo out the result in an <img /> tag

    hope that wasnt too confusing. early morning here :P

  3. #3
    Join Date
    Jun 2008
    Posts
    35
    I certainly am.

    cheers for that ill give it a go, so basically i want the image uploader to upload the image and also update the table on the database with the path to the image?

    cheers

    Dan

  4. #4
    Join Date
    Oct 2005
    Posts
    38
    yep sounds about right.

    i would also recommend you create a random filename during the upload or append a random 10 digit number to the end of the file name just prevent files being overwritten

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles