jarvis Posted October 9, 2009 Share Posted October 9, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/177087-rename-image-on-upload/ Share on other sites More sharing options...
Bricktop Posted October 9, 2009 Share Posted October 9, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/177087-rename-image-on-upload/#findComment-933701 Share on other sites More sharing options...
jarvis Posted October 9, 2009 Author Share Posted October 9, 2009 Wicked! Thanks very much!! Simple when you know how Quote Link to comment https://forums.phpfreaks.com/topic/177087-rename-image-on-upload/#findComment-933703 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.