Jump to content

Creating test-1.txt when test.txt already exists


scrappy1855

Recommended Posts

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]

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

?> 

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.