Mr Chris Posted December 12, 2006 Share Posted December 12, 2006 Hi Guys,Following on from the kind help I was given yesterday I’m trying to put together a script where it uploads a file and saves that file details into a MYSQL Database.Now it works, but 1) When I added a function to re-name the image if the image name already exists it doesn't re-name it.2) If I upload two images with the same name I get the error [b] Cannot redeclare get_unique_name()[/b]Can anyone please help?[code=php:0]<form action="script.php" method="post" enctype="multipart/form-data" name="form1"> <table width="247" border="0" cellspacing="0" cellpadding="0"> <tr> <th width="247"><input name="file[]" type="file" id="file[]"></th></tr> <tr> <td><input name="file[]" type="file" id="file[]"></td></tr> <tr> <td><input name="file[]" type="file" id="file[]"></td></tr> <tr> <td><div align="center"> <input type="submit" name="Submit" value="Submit"> </div></td></tr> </table></form>[/code][code=php:0]<?phpinclude('**************.ini');$can_i_connect = db_connect(); // by db_connect function is in my include file if(!$can_i_connect) { echo "Could not connect to database"; } foreach ($_FILES['file']['name'] as $k => $filename) {if ($filename != '') { $tmpfile = $_FILES['file']['tmp_name'][$k];//Start check name to see if it's a duplicatefunction get_unique_name($filename) { $parts = explode('.', $filename); $ext = array_pop($parts); $name = implode('.', $parts); $num = 0; do { $num++; $new_name = $name . '-' . $num . '.' . $ext; } while(file_exists($new_name)); return $new_name; } //End check name to see if it's a duplicate//Move code starts here//if the temp file is there copy it to the server if (@is_uploaded_file($tmpfile)) { copy($tmpfile, "return_images/" .$new_name); echo "<br>success"; }else { echo "<br>Not Uploaded<br>"; }//Move code ends here }else{ echo "error"; } }foreach ($_FILES['file']['name'] as $value3) {if(!empty($value3)){ $pathname = "$value3"; mysql_select_db($mysql);$sql = "INSERT INTO `images` (`imagepath`) VALUES ('$pathname');";mysql_query($sql) or die('Error, insert query failed');} }echo $pathname;echo $thumbpath;?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/30334-re-name-image/ Share on other sites More sharing options...
HuggieBear Posted December 12, 2006 Share Posted December 12, 2006 Move the function out of the foreach loop to prevent getting the error.As for why the file's not being renamed, it's because you're not calling the function anywhere.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/30334-re-name-image/#findComment-139606 Share on other sites More sharing options...
timmah1 Posted December 12, 2006 Share Posted December 12, 2006 [code]function findexts ($filename){$filename = strtolower($filename) ;$exts = split("[/\\.]", $filename) ;$n = count($exts)-1;$exts = $exts[$n];return $exts;}$ext = findexts ($_FILES['uploaded']['name']) ;$ran = rand () ;$ran2 = $ran.".";$target = "/your/direct/path/$username/";$target = $target . $ran2.$ext; if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))[/code] Quote Link to comment https://forums.phpfreaks.com/topic/30334-re-name-image/#findComment-139625 Share on other sites More sharing options...
Mr Chris Posted December 12, 2006 Author Share Posted December 12, 2006 Thanks Guys,But i'd really like to use the function I initially used as I want to see why it's not working:I've moved the re-name out of the for loop and called it later on in the script, but now it's sayng:[b]Warning: Missing argument 1 for get_unique_name() [/b]On this line [b]function get_unique_name($new_name) { [/b]and still not re-naming the image if it's a duplicate in MYSQL.Any further help much appreciated.ThanksChris[code=php:0]<?phpinclude('***********.ini');$can_i_connect = db_connect(); // by db_connect function is in my include file if(!$can_i_connect) { echo "Could not connect to database"; } foreach ($_FILES['file']['name'] as $k => $filename) {if ($filename != '') { $tmpfile = $_FILES['file']['tmp_name'][$k];//Move code starts here//if the temp file is there copy it to the server if (@is_uploaded_file($tmpfile)) { copy($tmpfile, "return_images/" .$new_name); echo "<br>success"; }else { echo "<br>Not Uploaded<br>"; }//Move code ends here }else{ echo "error"; } }//Start check name to see if it's a duplicatefunction get_unique_name($new_name) { $parts = explode('.', $new_name); $ext = array_pop($parts); $name = implode('.', $parts); $num = 0; do { $num++; $new_name = $name . '-' . $num . '.' . $ext; } while(file_exists($new_name)); return $new_name; } //End check name to see if it's a duplicateforeach ($_FILES['file']['name'] as $new_name) {$x = get_unique_name();if(!empty($x)){ $pathname = "$new_name"; mysql_select_db($mysql);$sql = "INSERT INTO `images` (`imagepath`) VALUES ('$pathname');";mysql_query($sql) or die('Error, insert query failed');} }echo $pathname;echo $thumbpath;?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/30334-re-name-image/#findComment-139640 Share on other sites More sharing options...
HuggieBear Posted December 12, 2006 Share Posted December 12, 2006 This line [code=php:0]$x = get_unique_name();[/code] should be passing a variable (I'm assuming the old name) to the function, so it should look something like this...[code=php:0]$x = get_unique_name($_FILES['file']['name']);[/code] It needs an argument.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/30334-re-name-image/#findComment-140004 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.