| | |
PHP and XML Question
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
You know how I am writting this file to my server every day
is there any way to delete an old file when the new one is uploaded every day
???
Thank you
PHP Syntax (Toggle Plain Text)
# $file = "pets_feed_" . date("Ymd") . ".xml";// - for yyyymmdd
is there any way to delete an old file when the new one is uploaded every day
???
Thank you
Hold on, something still isn't right. I am still working on this.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
OK that's better, had to add another dimension to the array to loop through the rows, you probably noticed that. Try this.
php Syntax (Toggle Plain Text)
<?php error_reporting(E_ALL ^ E_NOTICE); ini_set("display_errors", true); ini_set('memory_limit', '24M'); function csv2xml($file, $container = 'data') { $dataarray = array(); $r = "<{$container}>\n"; $row = 0; $cols = 0; $titles = array(); $handle = @fopen($file, 'r'); if (!$handle) return $handle; while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) { if (!$cols) $cols = count($data); for ($i = 0; $i < $cols; $i++) { if ($row == 0) { $titles[$i] = $data[$i]; continue; } $emailsarray = array(); if(strtolower($titles[$i]) == "description") { foreach(explode(" ", $data[$i]) as $value) { if(eregi("^[a-zA-Z0-9/\._\-]+@[a-zA-Z0-9_\-]+\.[a-zA-Z0-9\._\-]+$", $value)) { $emailsarray[] = $value; } } } $dataarray[$row][$titles[$i]][] = trim($data[$i]) != ""?$data[$i]:""; foreach($emailsarray as $value) { $dataarray[$row]["email"][] = $value; } } $row++; } fclose($handle); $containers = array("email"); for($i = 1; $i <= count($dataarray); $i++) { foreach($dataarray[$i] as $key=>$value) { if(in_array(strtolower($key), $containers)) { $r .= "\t\t<" . $key . "-container>\n"; } foreach($value as $value2) { $tabchars = "\t\t"; if(in_array(strtolower($key), $containers)) { $tabchars .= "\t\t"; } $r .= $tabchars . "<$key>$value2</$key>\n"; } if(in_array(strtolower($key), $containers)) { $r .= "\t\t</" . $key . "-container>\n"; } } } $r .= "</{$container}>\n"; return $r; } $xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n"; $xml .= csv2xml('dtifeed.csv', 'petad'); $xmlfile = @fopen('dtifeed.xml', 'wb') or die('Could not open XML file for writing'); fwrite($xmlfile, $xml) or die('Could not write string to XML file'); fclose($xmlfile); echo "Successfully wrote the XML file"; ?>
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
•
•
•
•
You know how I am writting this file to my server every day
PHP Syntax (Toggle Plain Text)
# $file = "pets_feed_" . date("Ymd") . ".xml";// - for yyyymmdd
is there any way to delete an old file when the new one is uploaded every day
???
Thank you
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
Hey Rob at the end I did not need the email thing this is the final copy of code
PHP Syntax (Toggle Plain Text)
<?php error_reporting(E_ALL ^ E_NOTICE); ini_set("display_errors", true); ini_set('memory_limit', '24M'); $file = "pets_feed_" . date("Ymd") . ".xml";// - for yyyymmdd if (!file_exists($file)) touch($file); $fh = fopen($file, "r"); function csv2xml($file, $container = 'data') { $r = "<{$container}>\n"; $row = 0; $cols = 0; $titles = array(); $handle = @fopen($file, 'r'); if (!$handle) return $handle; while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) { if (!$cols) $cols = count($data); for ($i = 0; $i < $cols; $i++) { if ($row == 0) { $titles[$i] = $data[$i]; continue; } $r .= trim($data[$i]) != ""?"\t\t<{$titles[$i]}>$data[$i]</{$titles[$i]}>\n":""; } $row++; } fclose($handle); $r .= "</{$container}>"; return $r; } $xml = '<?xml version="1.0" encoding="ISO-8859-1" ?> '; $xml .= csv2xml('petsfeed.csv', 'petad'); $xmlfile = @fopen($file, "wb") or die('Could not open XML file for writing'); fwrite($xmlfile, $xml) or die('Could not write string to XML file'); fclose($xmlfile); echo "Successfully wrote the XML file"; ?> The only problem is that I need the <petad> to open and close every ad instead of opening at the beginning of the file and closing at the end any ideas?
![]() |
Similar Threads
- How to call a PHP function from Javascript and return the results back into Javascrip (PHP)
- Question re: education (IT Professionals' Lounge)
- Multi-Language Interface in PHP/XML or PHP/MySQL (PHP)
- How can insert PHP snip code into a php website (PHP)
- Xml? (IT Professionals' Lounge)
- Inserting Elements into existing XML (C#)
- using php to check for filenames (PHP)
- How to use xml? (HTML and CSS)
Other Threads in the PHP Forum
- Previous Thread: Symfony MOdule redirection
- Next Thread: Multiple SQL Queries in PHP page
| Thread Tools | Search this Thread |
.htaccess action ajax apache api array auto beginner binary bounce broken cakephp checkbox class cms code cron curl database date display dynamic echo email error errorlog file files folder form format forms function functions google href htaccess html image include insert integration interactive ip java javascript joomla limit link login loop mail malfunctioning masterthesis menu mlm mod_rewrite multiple mysql nodes oop paypal pdf php popup problem query radio ram random recursion reference regex remote return script search server sessions sms soap source space sql syntax system table tutorial unset update upload url validation validator variable video web websitecontactform xml youtube





