global var returns to 0 after function call
Posted: Tue Jul 20, 2010 7:08 am
HI, I have problem with global.
I declared $fileCount = 0; at the begining of the file.
then in a if(isset($_POST)) statment I call global $fileCount; and then $fileCount = $fileC;
$fileC is the local var. after that I called my function CreateImportButtons($fileC) in that function I did a print on $fileCount after calling global...
It returns 2 and the function creates my 2 buttons.
Then I click my button 1 of thoose 2, under that if(isset($_POST)) statement, I did print of $fileCount, now it's 0 again.
Why is that? Why is it reset to 0?
I'll post the relevant code bellow]
I declared $fileCount = 0; at the begining of the file.
then in a if(isset($_POST)) statment I call global $fileCount; and then $fileCount = $fileC;
$fileC is the local var. after that I called my function CreateImportButtons($fileC) in that function I did a print on $fileCount after calling global...
It returns 2 and the function creates my 2 buttons.
Then I click my button 1 of thoose 2, under that if(isset($_POST)) statement, I did print of $fileCount, now it's 0 again.
Why is that? Why is it reset to 0?
I'll post the relevant code bellow
Code: Select all
<!-- Simple form for uploading the file -->
<form enctype="multipart/form-data" action="<?php echo $PHP_SELF; ?>" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload (max 100KB): <input name="uploadedfile" type="file" /><br />
<input type="submit" name="uploadbutton" value="Upload file" />
</form>
<?php
$fileCount = 0;
$target_path = 'tradera.csv'; //Save as tradera.csv in same dir
if (isset ($_POST['uploadbutton'])) {
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
print('The file '. basename( $_FILES['uploadedfile']['name']).
' has been uploaded<br/>');
$file = dirname(__file__) . '/tradera.csv';
$lines = count(file($file));
if ($lines <= 15) { // Less then 15, Do one file
print('There is less then 15 lines<br/>');
rename(dirname(__file__) . '/tradera.csv',dirname(__file__) . '/tradera_1.csv');
$fileC = 1;
} elseif ($lines >= 15 && $lines <= 20) { // More then 15 but less then 20, one file
print('There is between 15 and 20 lines<br/>');
rename(dirname(__file__) . '/tradera.csv',dirname(__file__) . '/tradera_1.csv');
$fileC = 1;
} elseif ($lines > 20) { // More then 20 lines, do more file
print('There is more then 20 lines<br/>');
$orgFile = fopen($file,'r'); // Open file
$c = 0; // Counter
$fileC = 0; // File counter
$newFileLines = ''; // empty line
while(!feof($orgFile)) { // While not end of file
$line = fgets($orgFile); // Get line
$newFileLines = $newFileLines . $line; // Add to lines
$c = $c+1; // Count +1
//print('count: ' . $c . '<br/>');
if ($c == 15 || feof($orgFile)) { // If count is 15 or file ends
$c = 0; // count is reset to 0
$fileC = $fileC+1; // File count +1
$newFile = fopen(dirname(__file__) . '/tradera_' . $fileC . '.csv','w'); // Open a new file
fwrite($newFile,$newFileLines); // Write new line data
fclose($newFile); // close it
$newFileLines = ''; // reset new file lines
}
}
fclose($orgFile); // close file
print($fileC . ' files was created<br/><br/>'); // report nr of files created
}
global $fileCount;
$fileCount = $fileC;
print($fileCount . ' modded!<br/>');
CreateImportButtons($fileC);
} else {
print('There was an error uploading the file, please try again!<br/>');
}
}
function CreateImportButtons($fileC) {
?>
<form enctype="multipart/form-data" action="<?php echo $PHP_SELF; ?>" method="POST">
<?php
global $fileCount;
print($fileCount . ' read @ buttons<br/>');
for ($i=1; $i <= $fileC; $i++) {
?>
<input type="submit" name="importbutton" value="Import file <?php echo $i?>" />
<?php
print("       ");
}
?>
</form>
<?php
}
if(isset($_POST['importbutton'])) {
print(substr($_POST['importbutton'],-1) . '<br/>');
global $fileCount;
print($fileCount . '<br/>');
$fileC = $fileCount;
CreateImportButtons($fileC);
}
?>