Jump to content

Recommended Posts

What part are you stuck on?

What you should really do is rename the file once it has been uploaded. Some users tend to upload files with funny characters or spaces in them, or upload a file with the same name that overwrites another. This is bad when it comes to the web so I usually hash the name of the file with a timestamp or something so you get a unique filename. Since you know the name of the file it is easy to store it in your database.

Do you mean you want to save the uploaded file path? Usually they are uploaded to a temporary location and I suggest that you read Handling file uploads in the php manual.

 

Otherwise, depending on your system, maybe you need a more complicated upload system.

The part im stuck on is how to save the path of the image to the database.

 

I have a way of uploading the image and saving it to a folder (process of saving to a temporary folder then moving to final destination.)

 

What I'm trying to do is add a profile picture for each user so I need to store the path of the picture they uploaded.

 

So essentially I need the file path to be saved into the user table of the user logged in, to then later recall this file on the profile page.

 

I'm just not sure how to get the file path and then write the SQL to save it to the database.

 

I guess it will be some kind of Insert into user......

 

Any help is much appreciated.

I'm not real sure how your filing structure is laid out.  You could generate a new folder for each user, then save the image inside of that.  You would know the file path is the users name.  This style is an easy solution, but only if you don't have 100's of users.  As a lot of different users would bloat the file system.

 

My suggestion would be to create a directy for profiles inside of your current image directory, then use the users name as the image name.  This way, if they uploaded a new file, it would overwrite the old file, and in doing so would help with the housekeeping. If you do it this way, you would only need to know the image name, and you have that, it is the users name.  The file path would be the same for every user, so you know what that is to.

 

The sql then becomes very easy to do.

 

<?php
$sql = "UPDATE `user` SET `image` = '$image' WHERE `name` = '$username'";
?>
//example of image tag.
<img src="http://mysite.com/images/profiles/<?php echo $user; ?>.jpg" alt="user profile image" />

the path of the temporary file is stored in the $_FILES array.  This is assuming you had the file input within a form that you submitted. 

 

If you look at $_FILES['fileInputname']['tmp_name'] you'll see the temporary location

Ok thanks for both your comments still quite unsure exactly what I need to do:

 

I have this code to create the upload:

 

if ($_FILES["file"]["error"] > 0)

    {

    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";

    }

  else

    {

    echo "Upload: " . $_FILES["file"]["name"] . "<br />";

    echo "Type: " . $_FILES["file"]["type"] . "<br />";

    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";

    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

 

    if (file_exists("upload/" . $_FILES["file"]["name"]))

      {

      echo $_FILES["file"]["name"] . " already exists. ";

      }

    else

      {

      move_uploaded_file($_FILES["file"]["tmp_name"],

      "upload/" . $_FILES["file"]["name"]);//change to Timp folder

      // "C:/upload/" . $_FILES["file"]["name"]);//change to Timp folder

      //echo "Stored in: " . "C:/xampp/htdocs/Timp/UserLogin/upload/" . $_FILES["file"]["name"];

      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];

      }

    }

 

But how do I then save the file location into a variable( I Guess??) then how to write sql to insert into the database?

 

Any ideas?

 

 

If you would like, I have an upload function that also makes sure the physical image size is correct, and to your settings. It returns the image if everything worked, and an error if it doesn't.

 

I've included it, just in case you might want to use it.  I went ahead and put in a mysql connection, and a query.  You must edit this portion to work with your setup, or take it out if you already have that part.

 

function uploadImage($setWidth, $setHeight) {
	$filepath = 'upload/'; //edit this for your file path.
	$content = NULL;	
	if($_FILES["file"]["size"] > 900000) { $content = "*File is to large, submit a smaller image!"; }
	elseif(empty($_FILES["file"]["name"])) { $content = "*No File Uploaded!"; }
					else
						{
							if ((($_FILES["file"]["type"] == "image/gif")
								|| ($_FILES["file"]["type"] == "image/jpeg")
								|| ($_FILES["file"]["type"] == "image/pjpeg")
								|| ($_FILES["file"]["type"] == "image/png")
								|| ($_FILES["file"]["type"] == "image/jpg")
								|| ($_FILES["file"]["type"] == "image/x-png"))
								&& ($_FILES["file"]["size"] < 900000))
									{
									$image = str_replace(' ', '_', $_FILES['file']['name']);

										if ($_FILES["file"]["error"] > 0)
											{
												$content =  "*Return Code: " . $_FILES["file"]["error"] . "<br />";
											}
										elseif (file_exists($filepath . $image))
											{
											  $content .= '*Image exists: ' . $image ;
											}
										else
											{													
															$uploadedfile = $_FILES["file"]["tmp_name"];
															$size = getimagesize($uploadedfile);
															$type = $size['mime'];
															$width = $size[0];
															$height = $size[1];
															$filename = $filepath.$image;
																	if($height > $setHeight || $width > $setWidth) 
																		{ 
																			$newwidth=$setWidth;
																			$newheight=($height/$width)*$setWidth;
																			$tmp=imagecreatetruecolor($newwidth,$newheight);


																				if($size[2] == IMAGETYPE_GIF)
																					{
																					    $src = imagecreatefromgif($uploadedfile);
																						imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
																						imagegif($tmp,$filename,100);
																					}
																				elseif($size[2] == IMAGETYPE_JPEG)
																					{
																					    $src = imagecreatefromjpeg($uploadedfile);
																						imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
																						imagejpeg($tmp,$filename,100);
																					}
																				elseif($size[2] == IMAGETYPE_PNG) 
																					{
																						$src = imagecreatefrompng($uploadedfile);
																						imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
																						imagepng($tmp,$filename,9);
																					}
																			$content .=  "*Image has been resized to $newwidth X $newheight<br/>";
																		imagedestroy($src);
																		imagedestroy($tmp);

																		}
																	else
																		{      
																		     move_uploaded_file($uploadedfile, $filename);

																		}

												return $filename;
											}

									}
								else { $content .=  "*Invalid file"; }
						}
	return $content;
}
define("DB_SERVER", "localhost");
define("DB_USER", "root");
define("DB_PASS", "");
define("DB_NAME", "test");

mysql_connect(DB_SERVER, DB_USER, DB_PASS);
mysql_select_db(DB_NAME);

if(isset($_POST['submit'])) {	
$image = uploadImage(150,150);
if(substr($image,0,1) != '*') {
	$sql = "UPDATE `table` SET `image` = '$image' WHERE `user` = '$username'";
	if(mysql_query($sql)) {
		echo 'Profile image updated!';
	}
	else {
		echo 'Profile image failed to update!';
	}
}
else {
	echo $image;
}
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.