jarvis Posted December 11, 2009 Share Posted December 11, 2009 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 More sharing options...
jarvis Posted December 11, 2009 Author Share Posted December 11, 2009 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 } Link to comment https://forums.phpfreaks.com/topic/184754-php-update-image-and-description-issue/#findComment-975366 Share on other sites More sharing options...
jarvis Posted December 11, 2009 Author Share Posted December 11, 2009 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! Link to comment https://forums.phpfreaks.com/topic/184754-php-update-image-and-description-issue/#findComment-975391 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.