Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
<?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
Comments
enhzflep 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 7-Sep-12 2:41am    
You can post as an answer
[no name] 7-Sep-12 2:56am    
is the answer.
thanks
post as answer

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:
JavaScript
if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }

■ You could make the following change to rename the file:
JavaScript
$newFilename = calcNewNameForFile();
 ...
 move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$newFilename);
 ...
 
Share this answer
 
v2
Comments
[no name] 8-Sep-12 2:12am    
+5,
but I am not accepting the answer,
but I will do.
PHP
$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);
			}
		}
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900