Pandareen Posted January 5, 2012 Share Posted January 5, 2012 Ok, so i've managed to upload images, but now i'm trying to rename them to avoid replacement and i've done something like this: mysql_query( "INSERT INTO `softmarket`.`internet_securitate` (`internet_securitateID`, `nume_produs`, `descriere`, `versiune`,`poza`, `pret`, `disponibilitate_produs`) VALUES ('', '$_POST[titlu]', '$_POST[descriere]', '$_POST[versiune]', '$foto', '$_POST[pret]', '$_POST[disponibilitate]');"); function findexts ($filename) { $filename = strtolower($filename) ; $exts = split("[/\\.]", $filename) ; $n = count($exts)-1; $exts = $exts[$n]; return $exts; } $ext = findexts ($_FILES['foto']['name']) ; $ran2 = $foto."."; $target = "../images/"; $target = $target . $ran2.$ext; while (file_exists($target)) { $ran2 = 'copyof'.$ran2; $target = $target . $ran2.$ext; } if(move_uploaded_file($_FILES['foto']['tmp_name'], $target)) { echo "The file has been uploaded as ".$ran2.$ext; } else { echo "Sorry, there was a problem uploading your file."; } header('Location: succes.php'); but it renames them in a wierd way...what is wrong? Quote Link to comment https://forums.phpfreaks.com/topic/254402-rename-images/ Share on other sites More sharing options...
dharmeshpat Posted January 5, 2012 Share Posted January 5, 2012 First can u tell us that wat is $foto variable holding i want to know what does it holds And i you don't mind can i suggest u that why don't u change the name of the file on every upload means give new name to the file whenever user uploads a file use date function like this $filename = date('dmYHis'); once u got he file name append the extension and store it in the folder, using this u will get a unique name for every upload that u do Quote Link to comment https://forums.phpfreaks.com/topic/254402-rename-images/#findComment-1304441 Share on other sites More sharing options...
Pandareen Posted January 5, 2012 Author Share Posted January 5, 2012 well $foto is my picture ( poza ) and in my data base is BLOB type...is this correct? mysql_query( "INSERT INTO `softmarket`.`internet_securitate` (`internet_securitateID`, `nume_produs`, `descriere`, `versiune`,`poza`, `pret`, `disponibilitate_produs`) VALUES ('', '$_POST[titlu]', '$_POST[descriere]', '$_POST[versiune]', '$foto', '$_POST[pret]', '$_POST[disponibilitate]');"); function findexts ($filename) { $filename = strtolower($filename) ; $exts = split("[/\\.]", $filename) ; $n = count($exts)-1; $exts = $exts[$n]; return $exts; } $ext = findexts ($_FILES['foto']['name']) ; $ran2 = $foto."."; $target = "../images/"; $target = $target . $ran2.$ext; while (file_exists($target)) { $ran2 = 'copyof'.$ran2; $target = $target . $ran2.$ext; } if(move_uploaded_file($_FILES['foto']['tmp_name'], $target)) { echo "The file has been uploaded as ".$ran2.$ext; } else { echo "Sorry, there was a problem uploading your file."; } Quote Link to comment https://forums.phpfreaks.com/topic/254402-rename-images/#findComment-1304445 Share on other sites More sharing options...
dharmeshpat Posted January 5, 2012 Share Posted January 5, 2012 If i m not mistaken i think the problem is that the name that u r storing in database is different and and file name is different because u r executing insert statement before the move_upload statement Execute insert statement after the upload statement and instead of using $foto us $ran2 Quote Link to comment https://forums.phpfreaks.com/topic/254402-rename-images/#findComment-1304448 Share on other sites More sharing options...
Pandareen Posted January 5, 2012 Author Share Posted January 5, 2012 dharmeshpat, i've sent u a PM Quote Link to comment https://forums.phpfreaks.com/topic/254402-rename-images/#findComment-1304463 Share on other sites More sharing options...
dharmeshpat Posted January 5, 2012 Share Posted January 5, 2012 i got one can u just send me detail description abt ur problem Quote Link to comment https://forums.phpfreaks.com/topic/254402-rename-images/#findComment-1304467 Share on other sites More sharing options...
Adam Posted January 5, 2012 Share Posted January 5, 2012 You should post any additional information in this thread rather than a PM, if you want more assistance. Quote Link to comment https://forums.phpfreaks.com/topic/254402-rename-images/#findComment-1304475 Share on other sites More sharing options...
Pandareen Posted January 5, 2012 Author Share Posted January 5, 2012 Ok!So i have a table users: userID, name, surname, photo and with a html form i insert data in that table.It works fine with the first row...but when i want to upload another picture, it replaces the first one, and this is my problem....i want to upload images with diferent names and i don't know how to fix that code: mysql_query( "INSERT INTO `softmarket`.`internet_securitate` (`internet_securitateID`, `nume_produs`, `descriere`, `versiune`,`poza`, `pret`, `disponibilitate_produs`) VALUES ('', '$_POST[titlu]', '$_POST[descriere]', '$_POST[versiune]', '$foto', '$_POST[pret]', '$_POST[disponibilitate]');"); function findexts ($filename) { $filename = strtolower($filename) ; $exts = split("[/\\.]", $filename) ; $n = count($exts)-1; $exts = $exts[$n]; return $exts; } $ext = findexts ($_FILES['foto']['name']) ; $ran2 = $foto."."; $target = "../images/"; $target = $target . $ran2.$ext; while (file_exists($target)) { $ran2 = 'copyof'.$ran2; $target = $target . $ran2.$ext; } if(move_uploaded_file($_FILES['foto']['tmp_name'], $target)) { echo "The file has been uploaded as ".$ran2.$ext; } else { echo "Sorry, there was a problem uploading your file."; } header('Location: succes.php'); so, with this i take the image (foto) and i move it in "images" (EX: 1.jpg ) and now if i upload another one, the scrit saves it with a wierd name like 1.jpgcopy1.jpg and it can't be echo... sorry for my english :-s and yes i know that i am a noob Quote Link to comment https://forums.phpfreaks.com/topic/254402-rename-images/#findComment-1304478 Share on other sites More sharing options...
dharmeshpat Posted January 5, 2012 Share Posted January 5, 2012 Can you try the code the way i have written don't change it place it has it is and try it out function findexts ($filename) { $filename = strtolower($filename) ; $exts = split("[/\\.]", $filename) ; $n = count($exts)-1; $exts = $exts[$n]; return $exts; } $ext = findexts ($_FILES['foto']['name']) ; $ran2 = $foto."."; $target = "../images/"; $target = $target . $ran2.$ext; while (file_exists($target)) { $ran2 = 'copyof'.$ran2; $target = $target . $ran2.$ext; } if(move_uploaded_file($_FILES['foto']['tmp_name'], $target)) { $newFilename = $ran2 . "." . $ext; mysql_query( "INSERT INTO `softmarket`.`internet_securitate` (`internet_securitateID`, `nume_produs`, `descriere`, `versiune`,`poza`, `pret`, `disponibilitate_produs`) VALUES ('', '$_POST[titlu]', '$_POST[descriere]', '$_POST[versiune]', '$newFilename', '$_POST[pret]', '$_POST[disponibilitate]');"); echo "The file has been uploaded as ".$newFilename; } else { echo "Sorry, there was a problem uploading your file."; } header('Location: succes.php'); Quote Link to comment https://forums.phpfreaks.com/topic/254402-rename-images/#findComment-1304481 Share on other sites More sharing options...
Pandareen Posted January 5, 2012 Author Share Posted January 5, 2012 well i used your code and i got the some problema: 1.jpg became 1jpg.copyof1.jpg :-s Quote Link to comment https://forums.phpfreaks.com/topic/254402-rename-images/#findComment-1304483 Share on other sites More sharing options...
Pandareen Posted January 5, 2012 Author Share Posted January 5, 2012 ok, i've posted aditional info and now nobody ? Quote Link to comment https://forums.phpfreaks.com/topic/254402-rename-images/#findComment-1304500 Share on other sites More sharing options...
Adam Posted January 5, 2012 Share Posted January 5, 2012 Are you trying to increment a number on the end of the file if there's a matching name? So original is "foo.jpg", next uploaded with the same name would be "foocopy1.jpg", next "foocopy2.jpg", etc. If so your code is all over the place, and you're inserting the file name into the database before actually knowing what it will be called. Personally I would just append a timestamp and pass the filename through md5(), as opposed to keep incrementing a number on the end. If you would rather do it this way though, I've quickly thrown together this function which should do the trick. Please read the comments and analyse the code (researching online if needed) before asking questions on something. function getNthFilename($filename, $target_dir) { // Make sure the filename actually does already exists if (!file_exists($target_dir . $filename)) { return $filename; } // Define the copy filename text $copy_text = $filename . 'copy%d'; // Define the number to increment $copy_num = 1; // Define the first copied version of the filename to test $copied_filename = sprintf($copy_text, $copy_num); // Loop through until we find an available name while (file_exists($target_dir . $copy_file)) { $copied_filename = sprintf($copy_text, ++$copy_num); } // At this point we should have a valid name return $copied_filename; } You can use it like: // Get the filename $filename = $_FILES['foto']['name']; // Get an absolute path for the target directory $target = realpath('../images') . '/'; // Get the valid name for the file $valid_filename = getNthFilename($filename, $target); After then successfully uploading the image, and only then, you want to insert the $valid_filename into the database. Edit I've corrected a quick typo. Might be more, I haven't tested this.. Quote Link to comment https://forums.phpfreaks.com/topic/254402-rename-images/#findComment-1304505 Share on other sites More sharing options...
Pandareen Posted January 5, 2012 Author Share Posted January 5, 2012 but, if a store the image with userID name, shouldn't be easier? like userID.jpg? :? Quote Link to comment https://forums.phpfreaks.com/topic/254402-rename-images/#findComment-1304513 Share on other sites More sharing options...
Adam Posted January 5, 2012 Share Posted January 5, 2012 Possibly? Can they have more than one image? This is your site. Quote Link to comment https://forums.phpfreaks.com/topic/254402-rename-images/#findComment-1304519 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.