PDA

View Full Version : Users can upload their own profile photo to separated folder(HELP)



Steve87
24 Feb 2012, 09:05 PM
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 //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
//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.

usedearplugs
12 Mar 2012, 09:39 PM
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.