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

 

 

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.