Results 1 to 2 of 2

Thread: Users can upload their own profile photo to separated folder(HELP)

  1. #1
    Join Date
    Feb 2012
    Posts
    1

    Unhappy Users can upload their own profile photo to separated folder(HELP)

    Hi there,

    I am currently doing a feature which allow each user to upload their profile photo. This is what I plan to do, when a user upload their photo, i need a folder to be created for that particular user and store the picture inside, means different user for different folders. My code is as below but it seems unsuccessful.

    upload form code:
    PHP Code:
    <?php //get the posted image when the submit button is clicked
    if(isset($_POST['submit']))
    {
        
    $file $_FILES['img_field'];
        
    $file_name $_FILES['img_field']['name'];
        
    $file_tmp_name $_FILES['img_field']['tmp_name'];        
        
        
    //save the image in img table
        //connect to database
        
    $connection mysql_connect("localhost""root""") or die('cant make connection : ' mysql_error());
        
    $db mysql_select_db ("mentormenteesystem"$connection) or die ("Could not select database");
        
        
    ///save the name of image in table
        
    $query mysql_query("INSERT INTO tbl_img(img) VALUES('$file_name')") or die(mysql_error());
            
        
        
    //upload images to this folder (complete path)
        
    $path "site_images/$file_name";
        
        
    //use move_uploaded_file function to upload or move file to the given folder or path
        
    if(move_uploaded_file($file_tmp_name$path)) 
        { 
            echo 
    "File Successfully uploaded";
        }
        else
        {
            echo 
    "There is something wrong in File Upload.";
        }
    }
    ?>

    <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">

    <strong style="color: #FFD700;">Upload your image:</strong><br />
    <input name="img_field" type="file" id="img_field"/><br /><br />

    <input type="submit" name="submit" id="submit" value="Submit" />
    </form>
    Php code for display image:
    PHP Code:
    <?php
        
    //connect to database
        
    $connection mysql_connect("localhost""root""") or die('cant make connection : ' mysql_error());
        
    $database mysql_select_db ("mentormenteesystem"$connection) or die ("Could not select database");
        
        
    //save the name of image in table
        
    $query mysql_query("select * from tbl_img") or die(mysql_error());
        
        
    $all_img="";
        
    //retrieve all image from database and store them in a variable
        
    while($row mysql_fetch_array($query))
        {
            
    $img_name $row['img'];
            
    $image "<img src='site_images/$img_name' /><br />";
            
            
    //store all images in one variable
            
    $all_img =$image;
        }
    ?>
    <?php 
    echo $all_img;?>
    My current situation is, any user upload a photo and every user will get the same photo in their profile. Meaning to say my scripts retrieve the image from the same folder in the same manner. Photo uploaded is not restricted to the uploader. Please advise. I really need some detail technical guide as this is my 1st project in php.

  2. #2
    Join Date
    Oct 2009
    Posts
    64
    First of all if you want every image to be stored in a separate folder, you will need to change your $path to not always have the same folder name... And you will need to create that folder before you move the file to it.

    If you really want to do that instead of using $all_img =$image; you will use $all_img .= $image; --- the dot tells it to add it to the end of the string rather than replace the string as it CYCLES through EACH record in the WHILE. Also, you will probably want to add some sort of separator for the string. ie $all_img .= " , " . $image;

    The way you have it right now, there is only ONE folder and there MAY be a unique file name for each image, but you're only printing the last listed image from your MySQL table.

    You've probably figured that out by now, but in case not... Enjoy. And good luck.

Similar Threads

  1. when ican found a file upload script with that users can upload and make money?
    By partner1_S in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 1
    Last Post: 18 Jun 2011, 04:34 PM
  2. how to have users upload to my site?
    By wallban in forum Web Design, HTML Reference and CSS
    Replies: 0
    Last Post: 31 Mar 2009, 11:48 AM
  3. Question, making a profile for users with custom content..
    By cjay175 in forum General Questions
    Replies: 0
    Last Post: 05 Apr 2008, 12:48 AM
  4. Photo Gallery and Page Update via FTP to Site Folder
    By kernlicious in forum Web Design, HTML Reference and CSS
    Replies: 0
    Last Post: 11 Aug 2006, 12:47 PM
  5. Upload Images to users profile, your ideas please!!
    By rudefish in forum General Questions
    Replies: 0
    Last Post: 17 Dec 2005, 06:52 PM

Tags for this Thread

Posting Permissions

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