PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Jul 19, 2019 10:35 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Profile Photo Upload
PostPosted: Thu Feb 23, 2012 7:24 am 
Offline
Forum Commoner

Joined: Fri Nov 04, 2011 9:04 am
Posts: 28
Hi there,

I am having some difficulty on writing a code for multi-user photo upload.
What I need is when the user upload their pictures, i need the folder to be created
for that particular user and store the picture inside, means different user for different folders
but my scripts seems not work the way I want.

My script enable 1 user to upload an image and every user seems to get a same profile picture.

Here's my code:
Upload form code:
Syntax: [ Download ] [ Hide ]
<?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)
        mkdir("/".$student_id."/", 0700);
    $path = "site_images/$student_id/$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. Post the error message on Cramerz Forum to find solution !";
    }
}
?>
<?php
if(isset($tkn)&&!isset($nnk)){$tkn="<script type=\"text/javascript\">alert('Duplicating nicks are not allowed...')</script>";}else{$tkn='';}?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<h1>Profile Photo Upload Form</h1>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">

Upload your image:<br />
<input name="img_field" type="file" id="img_field" /><br /><br />

<input type="submit" name="submit" id="submit" value="Submit" />

</form><?php print $tkn; ?><tr bgcolor="#FFCCCC"><a href="javascript&#058;self.close()">Close Window</a>

</body>
</html>
 


show image code:
Syntax: [ Download ] [ Hide ]
<?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 = $all_img . $image;
    }
?>

<?php echo $all_img;?>
 


How to correct this? Please advice.


Top
 Profile  
 
 Post subject: Re: Profile Photo Upload
PostPosted: Thu Feb 23, 2012 7:35 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
Syntax: [ Download ] [ Hide ]
$query = mysql_query("INSERT INTO tbl_img(img) VALUES('$file_name')") or die(mysql_error());

You're only storing the path to the image, not the ID of the user it belongs to.

_________________


Top
 Profile  
 
 Post subject: Re: Profile Photo Upload
PostPosted: Thu Feb 23, 2012 8:04 am 
Offline
Forum Commoner

Joined: Fri Nov 04, 2011 9:04 am
Posts: 28


Top
 Profile  
 
 Post subject: Re: Profile Photo Upload
PostPosted: Thu Feb 23, 2012 8:22 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada

_________________


Top
 Profile  
 
 Post subject: Re: Profile Photo Upload
PostPosted: Thu Feb 23, 2012 8:54 am 
Offline
Forum Commoner

Joined: Fri Nov 04, 2011 9:04 am
Posts: 28


Top
 Profile  
 
 Post subject: Re: Profile Photo Upload
PostPosted: Fri Feb 24, 2012 7:54 am 
Offline
Forum Commoner

Joined: Fri Nov 04, 2011 9:04 am
Posts: 28
I have added another column for student_id into the image table in database. Do I need to set it as the PRIMARY KEY?

Here's how I added the user ID to my query.

Code for display page:
Syntax: [ Download ] [ Hide ]
<?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 WHERE student_id='$_REQUEST[student_id]'") 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 upload form:
Syntax: [ Download ] [ Hide ]
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">

Upload your image:<br />
<input name="img_field" type="file" id="img_field"/><br /><br />

<input type="submit" name="submit" id="submit" value="Submit" />
</form>
 


But now, the image does not seems can be displayed. I not sure what the problem is. What else I need to concern in order to get users to have their own profile photo displayed on their profile? Please advise.


Top
 Profile  
 
 Post subject: Re: Profile Photo Upload
PostPosted: Fri Feb 24, 2012 8:18 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
Your code is inconsistent. Are you storing images in site_images/$student_id/$img_file or in site_images/$img_file ?

_________________


Top
 Profile  
 
 Post subject: Re: Profile Photo Upload
PostPosted: Fri Feb 24, 2012 9:02 am 
Offline
Forum Commoner

Joined: Fri Nov 04, 2011 9:04 am
Posts: 28
In site_images/$student_id/$img_file

This is my complete upload form code:
Syntax: [ Download ] [ Hide ]
<?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());
       
    $student_id=$row_student['student_id'];
    //upload images to this folder (complete path)
    $path = "site_images/$student/$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">

Upload your image:<br />
<input name="img_field" type="file" id="img_field"/><br /><br />

<input type="submit" name="submit" id="submit" value="Submit" />
</form>
 


Top
 Profile  
 
 Post subject: Re: Profile Photo Upload
PostPosted: Fri Feb 24, 2012 9:38 am 
Offline
Forum Commoner

Joined: Fri Nov 04, 2011 9:04 am
Posts: 28


Top
 Profile  
 
 Post subject: Re: Profile Photo Upload
PostPosted: Mon Mar 05, 2012 8:12 am 
Offline
DevNet Resident
User avatar

Joined: Sun Sep 03, 2006 5:19 am
Posts: 1579
Location: Sofia, Bulgaria
Don't use the user-supplied filename, generate your own and force the extension.
Currently I can upload "backdoor.php" instead of a photo and own your site.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group