dj-kenpo Posted September 8, 2007 Share Posted September 8, 2007 it's 3am and I'm going mental, this makes no sense. I have a function that adds a number to a file if it exists ie file[1].jpg file[2].jpg, just incase they're the same name. the function works, ok, but then GOES IN REVERSE...! instead of returnin the value when it get to return; it somehow goes in reverse?! I commented like crazy, so here's the function + results it prints... I've lost my mind... function file_name_int_increment($orig_filename, $new_name,$array, $count){ if(array_key_exists ($new_name, $array )){ print "if exists-$new_name $count<br />"; $count++; $new_name = $orig_filename."[$count]"; file_name_int_increment($orig_filename, $new_name, $array, $count); print "file_name_int_increment($orig_filename, $new_name, $array, $count);<br />"; }//if else{ print "else"; } print "dd2-$orig_filename - $new_name -$count<br />"; print "2- will return $count<br />"; return $count; }//end function $count = file_name_int_increment($orig_filename, $orig_filename, $attachments, 0);} $attachments is my array of filenames... output if exists-14.gif 0 if exists-14.gif[1] 1 if exists-14.gif[2] 2 elsedd2-14.gif - 14.gif[3] -3 2- will return 3 file_name_int_increment(14.gif, 14.gif[3], Array, 3); dd2-14.gif - 14.gif[3] -3 2- will return 3 file_name_int_increment(14.gif, 14.gif[2], Array, 2); dd2-14.gif - 14.gif[2] -2 2- will return 2 file_name_int_increment(14.gif, 14.gif[1], Array, 1); dd2-14.gif - 14.gif[1] -1 2- will return 1 dd3-14.gif [1] Quote Link to comment https://forums.phpfreaks.com/topic/68456-solved-gargh-function-defies-all-logic/ Share on other sites More sharing options...
dj-kenpo Posted September 8, 2007 Author Share Posted September 8, 2007 in this case it gets to 3, which is the correct count, ut then counts back down to 1. it ALWAYS counts back down to 1, instead of returning the value... I don't see how this is possible, it should be done running Quote Link to comment https://forums.phpfreaks.com/topic/68456-solved-gargh-function-defies-all-logic/#findComment-344169 Share on other sites More sharing options...
wildteen88 Posted September 8, 2007 Share Posted September 8, 2007 You are not catching the return value when you call a new instance of file_name_int_increment within the if statement. Change this line: file_name_int_increment($orig_filename, $new_name, $array, $count); to: $count = file_name_int_increment($orig_filename, $new_name, $array, $count); Quote Link to comment https://forums.phpfreaks.com/topic/68456-solved-gargh-function-defies-all-logic/#findComment-344195 Share on other sites More sharing options...
dj-kenpo Posted September 8, 2007 Author Share Posted September 8, 2007 Ok, that did it obviously. silly me. I've never run a function within a function before so i forgot about the count not 'magicly' carrying. I'm dumb. there's still some weirdness though that might be my misunderstanding? after it figures out that say, count should be '6' it will then loop through 6 more times... for no reason, if it were 7, then 7 times, etc.. see the out put if exists-14.gif 0 if exists-14.gif[1] 1 if exists-14.gif[2] 2 if exists-14.gif[3] 3 if exists-14.gif[4] 4 if exists-14.gif[5] 5 elsedd2-14.gif - 14.gif[6] -6 2- will return 6 dd2-14.gif - 14.gif[6] -6 2- will return 6 dd2-14.gif - 14.gif[5] -6 2- will return 6 dd2-14.gif - 14.gif[4] -6 2- will return 6 dd2-14.gif - 14.gif[3] -6 2- will return 6 dd2-14.gif - 14.gif[2] -6 2- will return 6 dd2-14.gif - 14.gif[1] -6 2- will return 6 dd3-14.gif [6] //// 14 /. gif why would it continue running the function? we have our number, the if statment is not fulfilled, and there's nothign telling it to GO back to the function..... very confused... Quote Link to comment https://forums.phpfreaks.com/topic/68456-solved-gargh-function-defies-all-logic/#findComment-344336 Share on other sites More sharing options...
wildteen88 Posted September 8, 2007 Share Posted September 8, 2007 Place: print "dd2-$orig_filename - $new_name -$count<br />"; print "2- will return $count<br />"; Within your else statement. If its outside the else it will be executed every time the function gets called. <?php function file_name_int_increment($orig_filename, $new_name,$array, $count) { print "<p>if exists-$new_name $count "; if(array_key_exists ($new_name, $array )) { echo 'TRUE<br />'; $count++; $new_name = $orig_filename.'['.$count.']'; print "file_name_int_increment($orig_filename, $new_name, $array, $count);</p>"; $count = file_name_int_increment($orig_filename, $new_name, $array, $count); } else { echo "FALSE<br />$orig_filename - $new_name - $count<br />will return $count</p>"; } return $count; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/68456-solved-gargh-function-defies-all-logic/#findComment-344344 Share on other sites More sharing options...
dj-kenpo Posted September 8, 2007 Author Share Posted September 8, 2007 wicked. thanks wildteen! if anyones interested her's the full workign code to increment a filename, if you had an upload system, or in my case, email attachments. function file_name_int_increment($orig_filename, $new_name,$filename_bare,$ext,$array, $count){ if(array_key_exists ($new_name, $array )){ $count++; $new_name = $filename_bare."[$count].".$ext; $count = file_name_int_increment($orig_filename, $new_name, $filename_bare,$ext,$array, $count); }//if return $count; }//end function call the function after you upload the file and store that file name into an array of filenames. in this case my array of filenames is called $attachments. $photos_uploaded = $_FILES['imap_file']; $orig_filename = $photos_uploaded['name']; if(is_array($attachments)){ $ext = end(explode('.',$orig_filename)); $filename_bare = str_replace(".".$ext, "",$orig_filename); $count = file_name_int_increment($orig_filename, $orig_filename, $filename_bare,$ext,$attachments, 0);} } this will correctly format the new filename eg image.jpg image[1].jpg image[2].jpg image[3].jpg if they are the same filename + extension. hopefully that helps someone... Quote Link to comment https://forums.phpfreaks.com/topic/68456-solved-gargh-function-defies-all-logic/#findComment-344378 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.