Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: PHP MySQL
I am devolving a profile based website, where profiles will be filled up in HTML form and through PHP it will be store by MYSQL.
 
My problem is, the Member will upload their profile picture. What I want is to
 
1. upload the profile picture to the server
2. rename the file to memberID
3. Store the link of the file(i.e. upload\memberid.jpg) to be stored by MySql.
 
Please help me to solve the issue.
I used the following code
 
<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  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"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?> 
but want to rename the file to memberid[some text field value].
I don't want the error message file already exist.
 
Please please help me.
Posted 6-Sep-12 12:54pm
Comments
enhzflep at 6-Sep-12 19:33pm
   
A couple of points that come to mind
■ You could download phpBB (free forum software) and look at the source
■ When uploading files, it (a) renames them (b) keeps track of the original name and the renamed name
■ using time() would give you a number you could convert to a string and use as a filename - though you would be limited to 1 upload per second or millisecond, whatever the resolution of time is (I forget)
■ You can just comment-out these 3 lines to remove the 'already exists' msg:
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
■ You could make the following change to rename the file:
$newFilename = calcNewNameForFile();
...
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$newFilename);
...
Mohibur Rashid at 7-Sep-12 2:41am
   
You can post as an answer
Sourav Sarkar-SS at 7-Sep-12 2:56am
   
is the answer.
thanks
post as answer
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

As per the request from Sourav, I've reposted my comments as a solution.
 
A couple of points that come to mind
■ You could download phpBB (free forum software) and look at the source
■ When uploading files, it (a) renames them (b) keeps track of the original name and the renamed name
■ using time() would give you a number you could convert to a string and use as a filename - though you would be limited to 1 upload per second or millisecond, whatever the resolution of time is (I forget)
■ You can just comment-out these 3 lines to remove the 'already exists' msg:
if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
■ You could make the following change to rename the file:
$newFilename = calcNewNameForFile();
 ...
 move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$newFilename);
 ...
  Permalink  
v2
Comments
Sourav Sarkar-SS at 8-Sep-12 2:12am
   
+5,
but I am not accepting the answer,
but I will do.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

$photo=$_FILES['photo']['name'];
			$size=$_FILES['photo']['size'];
			$type=$_FILES['photo']['type'];
			if($size > 11120000)
			{
				$err="File size is big";
			}
			else if($type!="image/jpg" && $type!="image/gif" && $type!="image/png" && $type!="image/jpeg")
			{
				$err="File size is big";
			}
			else
			{
				$t="images/";
				$target=$t.basename($_FILES["photo"]["name"]);
				$ext = pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION);
				while (file_exists($target))
				{
					$photo=uniqid() . '.'.$ext;
					$target=$t.$photo;	
				}
				move_uploaded_file($_FILES["photo"]["tmp_name"],$target);
			}
		}
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 430
1 Gihan Liyanage 347
2 ClimerChinna 222
3 vikinghunter 176
4 OriginalGriff 151
0 Sergey Alexandrovich Kryukov 8,373
1 OriginalGriff 7,112
2 CPallini 2,598
3 Richard MacCutchan 2,025
4 Abhinav S 1,788


Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 12 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100