Jump to content

Image Upload Renaming


Rustage
 Share

Recommended Posts

Hi, im trying to make an image upload script currently and using the w3schools tutorial ive made a working script but I was wondering if anyone could help me with trying to force it to rename all images using 'uniqueid'. I've looked around but can't seem to find a tutorial thats working for me, my current script is

<?phpif ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/png")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/pjpeg"))&& ($_FILES["file"]["size"] < 3200000))  {  if ($_FILES["file"]["error"] > 0)	{	echo "Return Code: " . $_FILES["file"]["error"] . "<br />";	}  else	{	echo "Upload: " . $_FILES["file"]["name"] . "<br />";	echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";	if (file_exists("uploads/" . $_FILES["file"]["name"]))	  {	  echo $_FILES["file"]["name"] . " already exists. ";	  }	else	  {	  move_uploaded_file($_FILES["file"]["tmp_name"],	  "uploads/" . $_FILES["file"]["name"]);	  echo "Image URL: " . "http://www.dv8-we.com/uploads/" . $_FILES["file"]["name"];	  }	}  }else  {  echo "Invalid file";  }?>

Thanks very much, Rustage.

Edited by Rustage
Link to comment
Share on other sites

Where is this 'uniqueid'? Wherever it is, you can apply it by using it in the move_uploaded_file() function. For example, if it was in a variable called $uniqueid, you can do it like:

move_uploaded_file($_FILES["file"]["tmp_name"],	  "uploads/" . $uniqueid);

Link to comment
Share on other sites

Sorry yeh i copied the wrong code in, that was a copy I had just incase it went terribly wrong haha. Well using your example and the code I had there before i've come up with this..

<?phpif ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/png")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/pjpeg"))&& ($_FILES["file"]["size"] < 3200000))$randFilename = uniqid("img_");$newFilename = $randFilename.".".$oldFileExt;  {  if ($_FILES["file"]["error"] > 0)	{	echo "Return Code: " . $_FILES["file"]["error"] . "<br />";	}  else	{	echo "Upload: " . $_FILES["file"]["name"] . "<br />";	echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";	if (file_exists("uploads/" . $_FILES["file"]["name"]))	  {	  echo $_FILES["file"]["name"] . " already exists. ";	  }	else	  {	  move_uploaded_file($_FILES["file"]["tmp_name"],	  "uploads/" . $newFilename);	  echo "Image URL: " . "http://www.dv8-we.com/uploads/" . $_FILES["file"]["name"];	  }	}  }else  {  echo "Invalid file";  }?>

But it just shows a blank page with no errors, sorry about this.. been a while since I did any proper PHP :)Thanks, Rustage

Link to comment
Share on other sites

It's invalid. That's why there isn't anything. Replace:

$randFilename = uniqid("img_");$newFilename = $randFilename.".".$oldFileExt;  {

with

  {$randFilename = uniqid("img_");$newFilename = $randFilename.".".$oldFileExt;

(i.e. insert the variables into the condition's TRUE evaluation path, not between the condition and the TRUE body)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...