scrappy1855 Posted January 6, 2010 Share Posted January 6, 2010 I'm can't seem to find anything on filename handling in php . I am needing to not append to my baddebt.txt file but create a new baddebt-1.txt, baddebt-2.txt, baddebt-3.txt depending on how many queries are submitted. I was hoping that i could get some advice. Here is my code that is now working thanks to some other help on this forum. However i realized another problem for if someone submits 2 queries in a row it won't be able to handle creation of multiple txt files. <?php error_reporting(E_ALL); ini_set("display_errors", 1); $customernumber = $_POST["customernumber"]; $yearwkstart = $_POST["yearwkstart"]; $yearwkend = $_POST["yearwkend"]; $email = $_POST["email"]; $file = "c:\asys_reports\baddebt.txt"; $values = "$customernumber\r\n$yearwkstart\r\n$yearwkend\r\n$ email\r\n"; IF (!isset($_POST["customernumber"])) { if (!empty($_SERVER['HTTPS']) && ('on' == $_SERVER['HTTPS'])) { $uri = 'https://'; } else { $uri = 'http://'; } $uri .= $_SERVER['HTTP_HOST']; header('Location: '.$uri.'/index_files/baddebtquery.htm'); } elseif (empty($customernumber) || empty($yearwkstart) || empty($yearwkend) || empty($email)) { if (!empty($_SERVER['HTTPS']) && ('on' == $_SERVER['HTTPS'])) { $uri = 'https://'; } else { $uri = 'http://'; } $uri .= $_SERVER['HTTP_HOST']; header('Location: '.$uri.'/index_files/error.htm'); } else { $values = "$customernumber\r\n$yearwkstart\r\n$yearwkend\r\n$ email\r\n"; $fp = fopen($file, "w") or die("Couldn't open $file for writing!"); $numBytes = fwrite($fp, $values) or die("Couldn't write values to file!"); fclose($fp); echo exec( if (!empty($_SERVER['HTTPS']) && ('on' == $_SERVER['HTTPS'])) { $uri = 'https://'; } else { $uri = 'http://'; } $uri .= $_SERVER['HTTP_HOST']; header('Location: '.$uri.'/index_files/complete.htm'); } ?> [\PHP] Quote Link to comment https://forums.phpfreaks.com/topic/187478-creating-test-1txt-when-testtxt-already-exists/ Share on other sites More sharing options...
teamatomic Posted January 6, 2010 Share Posted January 6, 2010 What you first need to do is determine what the last baddebt-X.txt file is easy to understand way is get all babble files into an array and natsort them then array_pop the last one and work on it list($a,$b)=explode(".","baddebt-X.txt"); list($c,$d)=explode("-",$a"); $X=$d+1; $new_file="baddebt-$X.txt"; if you loop through querys just increment $X++ and build a file new for each query. This also assumes that the first file is baddebt-X.txt and would fail if there was just a baddebt.txt. HTH Teamtomic Quote Link to comment https://forums.phpfreaks.com/topic/187478-creating-test-1txt-when-testtxt-already-exists/#findComment-989964 Share on other sites More sharing options...
ignace Posted January 6, 2010 Share Posted January 6, 2010 $baddebtFiles = glob('baddebt*'); $baddebtCount = sizeof($baddebtFiles); echo "next: baddebt-$baddebtCount.txt"; Quote Link to comment https://forums.phpfreaks.com/topic/187478-creating-test-1txt-when-testtxt-already-exists/#findComment-989972 Share on other sites More sharing options...
scrappy1855 Posted January 6, 2010 Author Share Posted January 6, 2010 I found another acceptable solution that did exactly what i wanted. Thank you to everyone for the help its been greatly appreciated. Here's the working code: <?php error_reporting(E_ALL); ini_set("display_errors", 1); $customernumber = $_POST["customernumber"]; $yearwkstart = $_POST["yearwkstart"]; $yearwkend = $_POST["yearwkend"]; $email = $_POST["email"]; $num=''; do{ $file = 'c:\asys_reports\baddebt'.$num.'.txt'; }while(file_exists($file) && $num-->intval(PHP_INT_MAX+1)); $values = "$customernumber\r\n$yearwkstart\r\n$yearwkend\r\n$email\r\n"; IF (!isset($_POST["customernumber"])) { if (!empty($_SERVER['HTTPS']) && ('on' == $_SERVER['HTTPS'])) { $uri = 'https://'; } else { $uri = 'http://'; } $uri .= $_SERVER['HTTP_HOST']; header('Location: '.$uri.'/index_files/baddebtquery.htm'); } elseif (empty($customernumber) || empty($yearwkstart) || empty($yearwkend) || empty($email)) { if (!empty($_SERVER['HTTPS']) && ('on' == $_SERVER['HTTPS'])) { $uri = 'https://'; } else { $uri = 'http://'; } $uri .= $_SERVER['HTTP_HOST']; header('Location: '.$uri.'/index_files/error.htm'); } else { $values = "$customernumber\r\n$yearwkstart\r\n$yearwkend\r\n$email\r\n"; $fp = fopen($file, "w") or die("Couldn't open $file for writing!"); $numBytes = fwrite($fp, $values) or die("Couldn't write values to file!"); fclose($fp); if (!empty($_SERVER['HTTPS']) && ('on' == $_SERVER['HTTPS'])) { $uri = 'https://'; } else { $uri = 'http://'; } $uri .= $_SERVER['HTTP_HOST']; header('Location: '.$uri.'/index_files/complete.htm'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/187478-creating-test-1txt-when-testtxt-already-exists/#findComment-990006 Share on other sites More sharing options...
ignace Posted January 7, 2010 Share Posted January 7, 2010 $num-->intval This reminds me about a discussion on StackOverflow intval(PHP_INT_MAX+1) This is funny but no so funny as this: PHP_INT_MAX+1 Quote Link to comment https://forums.phpfreaks.com/topic/187478-creating-test-1txt-when-testtxt-already-exists/#findComment-990253 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.