Jump to content

rename image on upload


jarvis

Recommended Posts

Hi All,

 

Ok I've the following code to handle my image upload:

		// Check for an image.
		if (is_uploaded_file ($_FILES['image']['tmp_name']))
		{
			// Relocate the image
			if (move_uploaded_file($_FILES['image']['tmp_name'], 'categories/'.$_POST['id'].'/'.$_FILES['image']['name']))
			{
          		saveThumb($_POST['id'],$_FILES['image']['name']);
				#$insert_sql = 'INSERT INTO uploads values("",'.$_POST['id'].', "'.$_FILES['image']['name'].'", "'.$_FILES['image']['size'].'", "'.$_FILES['image']['type'].'", "'.$_POST['image_description'].'", NOW())';
				$insert_sql = 'INSERT INTO uploads values(NULL,'.$_POST['category'].', "'.$_FILES['image']['name'].'", "'.$_FILES['image']['size'].'", "'.$_FILES['image']['type'].'", "'.$_POST['image_description'].'", NOW())';
				echo $insert_sql;
				$insert_rs  = mysql_query($insert_sql) or die(mysql_error());

				echo '<p class="error">The image has been uploaded!</p>';

			} else { // Inform user if problem relocating image

				echo '<p class="error">The file could not be Uploaded!</p>';

			}

		} else {

			echo '<p class="error">You must also select an image to upload!</p>';

		}

How can i stop the same file being uploaded twice? I figure the easiest way is to rename the file when uploading, how can I do this with the above code?

Thanks in advanced!

Link to comment
https://forums.phpfreaks.com/topic/177087-rename-image-on-upload/
Share on other sites

Hi jarvis,

 

Use PHP's file_exists function for this, something like the below should work:

 

// Check for an image.
		if (is_uploaded_file ($_FILES['image']['tmp_name']))
		{
		 	// Check for an existing filename
			if (file_exists('categories/'.$_POST['id'].'/'.$_FILES['image']['name']))
      			{
      			echo '<p class="error">'.$_FILES['image']['name'].' already exists.</p>';
      			}
    			
			else
      			{
			// Relocate the image
			if (move_uploaded_file($_FILES['image']['tmp_name'], 'categories/'.$_POST['id'].'/'.$_FILES['image']['name']))
			{
          		saveThumb($_POST['id'],$_FILES['image']['name']);
				#$insert_sql = 'INSERT INTO uploads values("",'.$_POST['id'].', "'.$_FILES['image']['name'].'", "'.$_FILES['image']['size'].'", "'.$_FILES['image']['type'].'", "'.$_POST['image_description'].'", NOW())';
				$insert_sql = 'INSERT INTO uploads values(NULL,'.$_POST['category'].', "'.$_FILES['image']['name'].'", "'.$_FILES['image']['size'].'", "'.$_FILES['image']['type'].'", "'.$_POST['image_description'].'", NOW())';
				echo $insert_sql;
				$insert_rs  = mysql_query($insert_sql) or die(mysql_error());

				echo '<p class="error">The image has been uploaded!</p>';

			} else { // Inform user if problem relocating image

				echo '<p class="error">The file could not be Uploaded!</p>';

			}

		}
		}
		 else {

			echo '<p class="error">You must also select an image to upload!</p>';

		}

 

Hope this helps.

 

 

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.