joshgarrod Posted December 21, 2010 Share Posted December 21, 2010 Hi, I have a script to add images to my tabel and and upload them to my server, I have managed to get the upload script to rename the image with a 5 digit random number on the end to prevent overwriting, the only problem I can't seem to be able to add the same name to my tabel. Any ideas? Thanks in advance. <?php $idir = "uploads/"; // Path To Images Directory if (isset ($_FILES['fupload'])){ //upload the image to tmp directory $url = $_FILES['fupload']['name']; // Set $url To Equal The Filename For Later Use if ($_FILES['fupload']['type'] == "image/jpg" || $_FILES['fupload']['type'] == "image/jpeg" || $_FILES['fupload']['type'] == "image/pjpeg") { $file_ext = strrchr($_FILES['fupload']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php $copy = copy($_FILES['fupload']['tmp_name'], $idir. basename($_FILES['fupload']['name'], $file_ext).rand(10000 , 99999).$file_ext); // Move Image From Temporary Location To Perm } } if (isset ($_FILES['fupload2'])){ //upload the image to tmp directory $url = $_FILES['fupload2']['name']; // Set $url To Equal The Filename For Later Use if ($_FILES['fupload2']['type'] == "image/jpg" || $_FILES['fupload2']['type'] == "image/jpeg" || $_FILES['fupload2']['type'] == "image/pjpeg") { $file_ext = strrchr($_FILES['fupload2']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php $copy = copy($_FILES['fupload2']['tmp_name'], "$idir" . $_FILES['fupload2']['name'] $file_ext).rand(10000 , 99999).$file_ext); // Move Image From Temporary Location To Permanent Location } } if (isset ($_FILES['fupload3'])){ //upload the image to tmp directory $url = $_FILES['fupload3']['name']; // Set $url To Equal The Filename For Later Use if ($_FILES['fupload3']['type'] == "image/jpg" || $_FILES['fupload3']['type'] == "image/jpeg" || $_FILES['fupload3']['type'] == "image/pjpeg") { $file_ext = strrchr($_FILES['fupload3']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php $copy = copy($_FILES['fupload3']['tmp_name'], "$idir" . $_FILES['fupload3']['name'] $file_ext).rand(10000 , 99999).$file_ext); // Move Image From Temporary Location To Permanent Location } } if (isset ($_FILES['fupload4'])){ //upload the image to tmp directory $url = $_FILES['fupload4']['name']; // Set $url To Equal The Filename For Later Use if ($_FILES['fupload4']['type'] == "image/jpg" || $_FILES['fupload4']['type'] == "image/jpeg" || $_FILES['fupload4']['type'] == "image/pjpeg") { $file_ext = strrchr($_FILES['fupload4']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php $copy = copy($_FILES['fupload4']['tmp_name'], "$idir" . $_FILES['fupload4']['name'] $file_ext).rand(10000 , 99999).$file_ext); // Move Image From Temporary Location To Permanent Location } } $usr = "user"; $pwd = "pass"; $db = "db"; $host = "host"; # connect to database $cid = mysql_connect($host,$usr,$pwd); if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); } if ($_POST['submit']) { $logo = mysql_real_escape_string("$idir" . $_FILES['fupload']['name']); $image1 = mysql_real_escape_string("$idir" . $_FILES['fupload2']['name']); $image2 = mysql_real_escape_string("$idir" . $_FILES['fupload3']['name']); $image3 = mysql_real_escape_string("$idir" . $_FILES['fupload4']['name']); $SQL = " INSERT INTO mhhire "; $SQL .= " image1, image2, image3, logo) VALUES "; $SQL .= " ('$image1', '$image2', '$image3', '$logo') "; $result = mysql_db_query($db,$SQL,$cid); $last=mysql_insert_id(); if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); } header("location:thanks.php"); exit(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/222324-random-name-generate-for-images/ Share on other sites More sharing options...
BlueSkyIS Posted December 21, 2010 Share Posted December 21, 2010 set the file name once, then use that variable in copy() and in the SQL update. Quote Link to comment https://forums.phpfreaks.com/topic/222324-random-name-generate-for-images/#findComment-1150014 Share on other sites More sharing options...
nafetski Posted December 21, 2010 Share Posted December 21, 2010 A few things.. When it comes to naming resources like images, it's definitely going to be safer to name them based off of a timestamp rather than a 5 digit random integer. Even tho 99999 seems like a big number, your chance of a naming collision is pretty high. You could go with something like microtime() and probably be a lot safer. The even MORE safe route would be to store the data into a table immediately, and use the auto_inc value (or a convention based off of it. At least that way you KNOW you're talking about a unique resource) What error are you getting with your current script? Quote Link to comment https://forums.phpfreaks.com/topic/222324-random-name-generate-for-images/#findComment-1150015 Share on other sites More sharing options...
joshgarrod Posted December 21, 2010 Author Share Posted December 21, 2010 Okay, I take on board your points. I am getting no errors just that my uploaded image on the server is called "image12345.jpg" and the image source in my table is being recorded as "image.jpg". I could use a variable $account which is defined early on in the script, this is a unique account number that each user will have and this will ensure no collisions. how would I add $account to the end of the image name? Thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/222324-random-name-generate-for-images/#findComment-1150018 Share on other sites More sharing options...
nafetski Posted December 21, 2010 Share Posted December 21, 2010 Ahhh well I can answer your current problem pretty easily You aren't saving the "temp" file name to any variable. When you copy the image to the directory it's getting named, but it's not going to rename the file. Your best bet would be to take the filename and save THAT to a var. Then on your insert use that Quote Link to comment https://forums.phpfreaks.com/topic/222324-random-name-generate-for-images/#findComment-1150020 Share on other sites More sharing options...
joshgarrod Posted December 21, 2010 Author Share Posted December 21, 2010 Sorry, I don't understand... I thought I was saving the image name to a variable in the form of $logo, $image1, $image2 and $image3 which I am then inserting? Quote Link to comment https://forums.phpfreaks.com/topic/222324-random-name-generate-for-images/#findComment-1150023 Share on other sites More sharing options...
Psycho Posted December 21, 2010 Share Posted December 21, 2010 I guess I am reading the problem differently than the last two posters. Looking at the code it seems he is trying to insert the ORIGINAL file name in the database - not the modified name with the random number. First, I agree with nafetski that a timestamp would be prefereable to a random number. But, more importantly - if you are storing the original names but not the modified names, how are you linking the database records to the images? You need to store both in the same record. That way you can display the original name the user uploaded but still link it back to the unique file name you create. However, none of that is your problem. If you are getting a database error when trying to insert an image with the same original name as another image, the problem is that you have set that field in the database to be a unique field. Quote Link to comment https://forums.phpfreaks.com/topic/222324-random-name-generate-for-images/#findComment-1150024 Share on other sites More sharing options...
joshgarrod Posted December 21, 2010 Author Share Posted December 21, 2010 hi mjdamato, I do want both the record in the database and the name of the image to be the same - how do I achieve this at the moment the image is saved to the server with the 5 digit number on the end but not in the database. Sorry I am a bit out of my depth with all this. Quote Link to comment https://forums.phpfreaks.com/topic/222324-random-name-generate-for-images/#findComment-1150029 Share on other sites More sharing options...
Psycho Posted December 21, 2010 Share Posted December 21, 2010 hi mjdamato, I do want both the record in the database and the name of the image to be the same - how do I achieve this at the moment the image is saved to the server with the 5 digit number on the end but not in the database. Sorry I am a bit out of my depth with all this. Then your answer is simple. Look at this bit of code you posted previously if (isset ($_FILES['fupload'])) { //upload the image to tmp directory $url = $_FILES['fupload']['name']; // Set $url To Equal The Filename For Later Use if ($_FILES['fupload']['type'] == "image/jpg" || $_FILES['fupload']['type'] == "image/jpeg" || $_FILES['fupload']['type'] == "image/pjpeg") { $file_ext = strrchr($_FILES['fupload']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php $copy = copy($_FILES['fupload']['tmp_name'], $idir. basename($_FILES['fupload']['name'], $file_ext).rand(10000 , 99999).$file_ext); // Move Image From Temporary Location To Perm } } You "save" the original name to the variable $url which you later used in your INSERT statement, but you actually saved the file using the name $idir. basename($_FILES['fupload']['name'], $file_ext).rand(10000 , 99999).$file_ext); You just need to create the new name as a varaible first then use that to both save the file AND to save to the database. Here is a modification of that block of code that you can use to rewrite the similar blocks: if (isset ($_FILES['fupload'])) { //upload the image to tmp directory $file_ext = strrchr($_FILES['fupload']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php $url = basename($_FILES['fupload']['name'], $file_ext).time().$file_ext); // Set $url To Equal The Filename For Later Use if (in_array($_FILES['fupload']['type'], array('image/jpg', 'image/jpeg','image/pjpeg')) { $copy = copy($_FILES['fupload']['tmp_name'], $idir.$url); // Move Image From Temporary Location To Perm } } Quote Link to comment https://forums.phpfreaks.com/topic/222324-random-name-generate-for-images/#findComment-1150036 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.