Jump to content

Image upload with different name


Yeux

Recommended Posts

Hi everybody, I have similar problem like member vikaspa in this thread:

 

http://www.phpfreaks.com/forums/index.php?topic=279531.msg1323820#msg1323820

 

Have great site, but this image rewriting makes me crazy...today someone upload  in some thread 1.jpg for example...after 3 days someone uploads diferent image and also called it 1.jpg, and overwrites this earlier one. Nonsense.

 

Please help me with my code, what to add where, to prevent rewriting and instead that, to rename upload internally (without notifying the user)...for example 1_1.jpg.

 

Here is my code:

 

if ($fdata['forum_attach'] && checkgroup($fdata['forum_attach'])) {
				$attach = $_FILES['attach'];
				if ($attach['name'] != "" && !empty($attach['name']) && is_uploaded_file($attach['tmp_name'])) {
					$attachname = stripfilename(substr($attach['name'], 0, strrpos($attach['name'], ".")));
					$attachext = strtolower(strrchr($attach['name'],"."));
					if (preg_match("/^[-0-9A-Z_\[\]]+$/i", $attachname) && $attach['size'] <= $settings['attachmax']) {
						$attachtypes = explode(",", $settings['attachtypes']);
						if (in_array($attachext, $attachtypes)) {
							$attachname .= $attachext;
							move_uploaded_file($attach['tmp_name'], FORUM."attachments/".$attachname);
							chmod(FORUM."attachments/".$attachname,0644);
							if (in_array($attachext, $imagetypes) && (!@getimagesize(FORUM."attachments/".$attachname) || !@verify_image(FORUM."attachments/".$attachname))) {
								unlink(FORUM."attachments/".$attachname);
								$error = 1;
							}
							if (!$error) { $result = dbquery("INSERT INTO ".DB_FORUM_ATTACHMENTS." (thread_id, post_id, attach_name, attach_ext, attach_size) VALUES ('".$thread_id."', '".$post_id."', '$attachname', '$attachext', '".$attach['size']."')"); }
						} else {
							@unlink($attach['tmp_name']);
							$error = 1;
						}
					} else {
						@unlink($attach['tmp_name']);
						$error = 2;
					}

 

 

 

Member Little guy wrote:

 

$tmp_name = $_FILES["pictures"]["tmp_name"];

$tmp_new_name = $_FILES["pictures"]["name"];

$path_parts = pathinfo($tmp_new_name);

$new_name = $path_parts['filename'] . time() . $path_parts['extension'];

$uploads_dir = '/uploads';

move_uploaded_file($tmp_name, "$uploads_dir/$new_name");

 

But I'm completely noob for coding and don't know where to put that in my forum code. Please help.

 

Thanx in advance, sorry for longer thread, and for my english. Regards.  ;)

 

 

Link to comment
Share on other sites

Would this make a trick?

 

Instead of :

 

if (in_array($attachext, $attachtypes)) {

$attachname .= $attachext;

move_uploaded_file($attach['tmp_name'], FORUM."attachments/".$attachname);

 

to put this:

 

if (in_array($attachext, $attachtypes)) {

$attachname .= $attachext;

$count = 1;

while(file_exists(FORUM."attachments/".$attach_name)) {

$attach_name = $attachname_without_ext . "(". $count .")". $attachext;

$count++;

}

move_uploaded_file($attach['tmp_name'], FORUM."attachments/".$attachname);

 

 

Please help... :confused:

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.