Jump to content

Recommended Posts

Hi All,

 

Hope some kind soul can help, I think I've got myself in a muddle! I've a form which:

If an image exists, you can delete it or update it. The update can be either:

a) The image

b) the description or

c) the category it's in.

 

If no image exists, you can add a new image and description.

 

Currently, the adding a new image works. It's the if existing image update fails.

 

My code:

	if(!empty($_POST['submit_type']) && $_POST['submit_type'] == 'update')
	{
		// Get the category
		if (!empty($_POST['category'])) {
			$category = escape_data($_POST['category']);

		}	

		// Check for an image.
		if (is_uploaded_file ($_FILES['image']['tmp_name']))
		{
			// Relocate the image
			if (move_uploaded_file($_FILES['image']['tmp_name'], '../uploads/'.$_POST['id'].'/'.$_FILES['image']['name']))
			{
        		saveThumb($_POST['id'],$_FILES['image']['name']);
				$update_sql = 'UPDATE uploads SET category_id = "'.escape_data($_POST['category']).'", image_description = "'.escape_data($_POST['image_description']).'", file_type = "'.$_FILES['image']['type'].'", file_size = "'.$_FILES['image']['size'].'", file_name = "'.$_FILES['image']['name'].'" where upload_id = '.$_POST['this_image_id'];

				echo $update_sql;
				$update_rs  = mysql_query($update_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>';

			}	
		}

		//UPDATE IMAGE AND DESCRIPTION ALREADY SET IN DATABASE.
		$update_sql = 'UPDATE uploads SET category_id = "'.escape_data($_POST['category']).'", image_description = "'.escape_data($_POST['image_description']).'" WHERE upload_id = '.$_POST['this_image_id'];

		//RELOCATE THE IMAGE IF REQUIRED
		if (!empty($_POST['image_name'])) {
			$image_name = escape_data($_POST['image_name']);
		}

		$original = 'categories/'.$id.'/'.$image_name;
		$original_t = 'categories/'.$id.'/t_'.$image_name;
		$copied = 'categories/'.$_POST['category'].'/'.$image_name; 
		$copied_t = 'categories/'.$_POST['category'].'/t_'.$image_name; 
		rename($original, $copied); 
		rename($original_t, $copied_t); 

	} else {

	//INSERT NEW IMAGE AND DESCRIPTION INTO DATABASE.
	if(!empty($_POST['submit_type']) && $_POST['submit_type'] == 'upload')
	{
	...code to upload new

Please can someone point out where I've gone wrong.

 

many thanks

 

Link to comment
https://forums.phpfreaks.com/topic/184754-php-update-image-and-description-issue/
Share on other sites

Would I be better off adding another option:

update

relocate <-- new

upload

 

I could then do:

if (!empty($_POST['submit_type']) && $_POST['submit_type'] == 'update') {

#update

} elseif (!empty($_POST['submit_type']) && $_POST['submit_type'] == 'relocate') {

#relocate

} else {

#upload

}

Ok, I've sorted that issue. Problem I've got now, when I update the image, I want to remove the old one.

 

However, it errors as its says

rename(categories/11/window.jpg,categories/11/window.jpg) [function.rename]: No such file or directory

 

Where window.jpg is the old image.

 

I think it's obvious but cannot see for looking. My code is below:

	if(!empty($_POST['submit_type']) && $_POST['submit_type'] == 'update')
	{
		// Get the category
		if (!empty($_POST['category'])) {
			$category = escape_data($_POST['category']);

		}	

		// Check for an image.
		if (is_uploaded_file ($_FILES['image']['tmp_name']))
		{

			//remove old image and replace with new

			$R=mysql_query("SELECT * FROM uploads WHERE upload_id = ".$_POST['this_image_id']);
			echo $R;
			$O=mysql_fetch_object($R);
			$category_id=$O->category_id;
			$fname=$O->file_name;
			$tfname="t_".$fname;
			@unlink("categories/$category_id/$fname");
			@unlink("categories/$category_id/$tfname");		


			// Relocate the image
			if (move_uploaded_file($_FILES['image']['tmp_name'], 'categories/'.$_POST['id'].'/'.$_FILES['image']['name']))
			{

        		saveThumb($_POST['id'],$_FILES['image']['name']);
				$update_sql = 'UPDATE uploads SET category_id = "'.escape_data($_POST['category']).'", image_description = "'.escape_data($_POST['image_description']).'", file_type = "'.$_FILES['image']['type'].'", file_size = "'.$_FILES['image']['size'].'", file_name = "'.$_FILES['image']['name'].'" where upload_id = '.$_POST['this_image_id'];

				#echo $update_sql;
				$update_rs  = mysql_query($update_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>';

			}	
		}

		//RELOCATE THE IMAGE IF REQUIRED
		if (!empty($_POST['image_name'])) {
			$image_name = escape_data($_POST['image_name']);
		}


		//UPDATE IMAGE AND DESCRIPTION ALREADY SET IN DATABASE.
		$update_sql = 'UPDATE uploads SET category_id = "'.escape_data($_POST['category']).'", image_description = "'.escape_data($_POST['image_description']).'" WHERE upload_id = '.$_POST['this_image_id'];
		#echo $update_sql .' update and relocate';

		$update_rs  = mysql_query($update_sql) or die(mysql_error());
		#if ($update_rs){
		$original = 'categories/'.$id.'/'.$image_name;
		$original_t = 'categories/'.$id.'/t_'.$image_name;
		$copied = 'categories/'.$_POST['category'].'/'.$image_name; 
		$copied_t = 'categories/'.$_POST['category'].'/t_'.$image_name; 
		rename($original, $copied); 
		rename($original_t, $copied_t); 	
		#}

	} else {

	//INSERT NEW IMAGE AND DESCRIPTION INTO DATABASE.
	if(!empty($_POST['submit_type']) && $_POST['submit_type'] == 'upload')
	{

 

Thanks again!

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.