Jump to content

[SOLVED] GARGH! function defies all logic!


dj-kenpo

Recommended Posts

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]

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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;
}

?>

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.