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
https://forums.phpfreaks.com/topic/231939-image-upload-with-different-name/
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:

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.