Page 1 of 1
Is this possible?
Posted: Thu Apr 14, 2005 8:44 am
by paolone
Hello... I'm wondering how to get the date text out of a file name that is being upload on a server, and adding this value to the database itself...
I mean :
A file called
XX_XXXXX_XXXXX_XXX_03042005.csv is generated every day. And the date is the last digits. When inserting the values stored in in a database, can i add in an extra field where i can store the date value?
I think i can use the explode funtion to breack the name apart, but then?? How to store the last digit? Maybe assigning each value to an array?
If this is a solution, is there a better one?
Posted: Thu Apr 14, 2005 9:05 am
by Chris Corbyn
Two options...
a) If the string (filename) is always the same length you can just take the
substr() of the relevant part
b) If you need something more foolproof then use a Regular Expression (shown below)...
Code: Select all
$filename = "XX_XXXXX_XXXXX_XXX_03042005.csv";
preg_match('/_(\d{8})\.csv$/', $filename, $matches); //Extract the date
$date = $matches[1]; //This is your date
echo $date;
Posted: Thu Apr 14, 2005 9:05 am
by malcolmboston
erm, dont really understand what the problem is here, its simple
explode on the filename to get the name
run an insert/update query on the database
seems simple enough to me, am i missing something?
Posted: Thu Apr 14, 2005 9:40 am
by paolone
Hey d11wtq, the same result but my code (of course) is longer...
Code: Select all
$filename = "Transaction_Response_Time_Monitoring_07032005.csv";
//separate and collect to an array each element separated by _
$namepart = explode("_", $filename);
//get last one containing the date plus the extensiona and again explode it
$date_container = explode(".", $namepart[4]);
//assign the value (text only) to the variable
$date = $date_container[0];
echo "<pre>";
echo "namepart[0] =".$namepart[0]."<br>";
echo "namepart[1] =".$namepart[1]."<br>";
echo "namepart[2] =".$namepart[2]."<br>";
echo "namepart[3] =".$namepart[3]."<br>";
echo "namepart[4] =".$namepart[4]."<br>";
echo "The resulting date is <b>".$date."</b><br>";
But then once i got this, how can i insert this in the database while uploading? Malcolmboston, i get your advice, but can i do it at the same time of the upload or do i have to set up another step?
Posted: Thu Apr 14, 2005 9:47 am
by malcolmboston
yes you would need to do it in steps, at least i think so and that would definitely be my method
i would also split up the code into functions for eg
Code: Select all
function GetDate ($string)
{
// function to get date from string here
return $string;
}
function InsertDateMySQL ($string)
{
// function to insert into MySQL
}
then a simple call like:
Code: Select all
$string = GetDate ($string = "somethinghere");
InsertDateMySQL ($string);
all it requires is a simple passing of vars from one function to another.
doing it this way makes your code easy to change in the future too.
Posted: Fri Apr 15, 2005 9:54 am
by paolone
guys, first of all let me tell you that this forum rules...
thanks to your help i'm getting to the result expected !
Now i'm checking how to work on the dateformat, as it is returned as 02032005 (ddmmyyyy) and i want to insert it in a MySQL database in a different format... like yyyy-mm-dd
Unfortunatelly i've not yet installed PHP5 on the server...
Posted: Fri Apr 15, 2005 9:57 am
by Bennettman
Code: Select all
$orig = "02032005";
$new = substr($orig, 4, 4) . substr($orig, 2, 2) . substr($orig, 0, 2);
^_-