Jump to content

renaming uploaded image fie


IG88

Recommended Posts

I have this function to upload User images, but I need it to either rename or simply add a time() or date() stamp to the file name.

 

Any suggestions?

 

	function CreateUserImage()
{
	global $modx;

	$imageAttributes = str_replace(', ', ',', $this->UserImageSettings);
	$imageAttributes = explode(',', $imageAttributes);

	if ($_FILES['photo']['size'] >= $imageAttributes[0])
	{
		$sizeInKb = round($imageAttributes[0] / 1024);
		$sizeError = str_replace('[+000+]', $sizeInKb, $this->LanguageArray[28]);
		return $this->FormatMessage($sizeError);
	}

	$userImage = $modx->config['base_path'].strtolower(str_replace(' ', '-', basename( $_FILES['photo']['name'])));
	if (!move_uploaded_file($_FILES['photo']['tmp_name'], $userImage))
	{
		return $this->FormatMessage($this->LanguageArray[29]);
	}

	// License and registration ma'am. I need to se an ID!
	if ($modx->getLoginUserID())
	{
		$currentWebUser = $modx->getWebUserInfo($modx->getLoginUserID());
		if ($this->Type == 'manager')
		{
			$currentWebUser['username'] = $_POST['username'];
		}
	}
	else
	{
		$currentWebUser['username'] = $this->Username;
		if ($this->Username == '' || empty($this->Username))
		{
			$currentWebUser['username'] = $_POST['username'];
		}
	}

Link to comment
Share on other sites

I have added

echo $userImage = $modx->config['base_path'].strtolower(str_replace(' ', '-', basename( $_FILES['photo']['name'])));

 

and got nothing. I also tried echo htmlentities(var_dump($userImage));

 

I think I am just echoing wrong.

 

Link to comment
Share on other sites

Yes that is exactly what I need to do. Adding the server time would look ugly but serve the purpose. There will be numerous seperate members uploading so it needs to be something random that will not repeat.

 

I am just not sure why I can not get the time() to work.

 

Thank you.

Link to comment
Share on other sites

It is not working.

 

Very interesting though, when I right click the image and look at the properties the location shows this really long string with a hash at the end.

 

http://www.site.com/image.php?src=/assets/snippets/webloginpe/userimages/bride_of_bob.jpg&w=100&h=100&aoe=1&hash=35dd829cf2b4ac5f79e8

 

Here is the code I was using.

		$userImage = $modx->config['base_path'].'assets/snippets/webloginpe/userimages/'.strtolower(str_replace(' ', '-', basename($_FILES['photo']['name'])));
	$temp = explode(".", $userImage);
               $temp[0] .= time();
               $userImage = implode(".", $temp);


	if (!move_uploaded_file($_FILES['photo']['tmp_name'], $userImage))
	{
		return $this->FormatMessage($this->LanguageArray[29]);
	}

Link to comment
Share on other sites

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.