willg Posted February 18, 2007 Share Posted February 18, 2007 Hi, I am having trouble with some PHP to read a csv file and create a file from each line of data from the CSV. The PHP should read the title, author, citation, publisher and URL fields from the CSV. Then, it should name the CSV file according to the URL (adding .desc onto the end) and arrange the other data in the pattern here: <title>TITLE FROM CSV HERE (CITATION FROM CSV HERE) - PUBLISHER FROM CSV HERE</title> <meta name="description" content="AUTHORS FROM CSV HERE">'; So far, I have come up with the below, but I am new to PHP and am not sure how to proceed this to a complete PHP code. Thanks for reading this. <?php //need something to open CSV and read each line of data $new_url = $url.".desc"; $dir = "pdf_desc/"; $check_exists = "TRUE"; function encode($message) { $message = str_replace('http://', '', $message); $message = str_replace('https://', '', $message); $message = str_replace(' ', '%20', $message); $message = str_replace('/', '%2F', $message); $message = str_replace('\\', '%5C', $message); $message = str_replace(':', '%3A', $message); $message = str_replace('*', '%2A', $message); $message = str_replace('?', '%3F', $message); $message = str_replace('"', '%22', $message); $message = str_replace('<', '%3C', $message); $message = str_replace('>', '%3E', $message); $message = str_replace('|', '%7C', $message); return $message; } { $encode_url = encode($new_url); $code = '<title>'.$title.' ('.$citation.') - '.$publisher.'</title> <meta name="description" content="'.$author.'">'; if(file_exists($dir.$encode_url) && $check_exists == "TRUE") { echo "<center><strong>File Already Exists With That Name</strong></center>"; } else { $file_create = fopen($dir.$encode_url, 'w') or die("can't create file"); fwrite($file_create, $code); fclose($file_create); } } ?> Link to comment https://forums.phpfreaks.com/topic/39048-read-csv-and-write-files-for-each-entry/ Share on other sites More sharing options...
kenrbnsn Posted February 18, 2007 Share Posted February 18, 2007 For reading a csv file, look at the function fgetcsv() Ken Link to comment https://forums.phpfreaks.com/topic/39048-read-csv-and-write-files-for-each-entry/#findComment-188076 Share on other sites More sharing options...
willg Posted February 18, 2007 Author Share Posted February 18, 2007 Thanks. I think I am getting nearer. The below code still did not work but it did create 1 file of the right format which did not contain the corresponding data from the CSV. So, there is no probelm creating the files but instead reading the data from the CSV is problematic. Any ideas how to fix that? The new code is below (The CSV file I am using separates the fields with commas). <?php $fh = fopen("./testCSV.csv", "r"); while (list($title, $citation, $author, $publisher, $url) = fgetcsv($fh, 1024, ",")); $new_url = $url.".desc"; $dir = "pdf_desc/"; $check_exists = "TRUE"; function encode($message) { $message = str_replace('http://', '', $message); $message = str_replace('https://', '', $message); $message = str_replace(' ', '%20', $message); $message = str_replace('/', '%2F', $message); $message = str_replace('\\', '%5C', $message); $message = str_replace(':', '%3A', $message); $message = str_replace('*', '%2A', $message); $message = str_replace('?', '%3F', $message); $message = str_replace('"', '%22', $message); $message = str_replace('<', '%3C', $message); $message = str_replace('>', '%3E', $message); $message = str_replace('|', '%7C', $message); return $message; } { $encode_url = encode($new_url); $code = '<title>'.$title.' ('.$citation.') - '.$publisher.'</title> <meta name="description" content="'.$author.'">'; if(file_exists($dir.$encode_url) && $check_exists == "TRUE") { echo "<center><strong>File Already Exists With That Name</strong></center>"; } else { $file_create = fopen($dir.$encode_url, 'w') or die("can't create file"); fwrite($file_create, $code); fclose($file_create); } } ?> Link to comment https://forums.phpfreaks.com/topic/39048-read-csv-and-write-files-for-each-entry/#findComment-188091 Share on other sites More sharing options...
willg Posted February 19, 2007 Author Share Posted February 19, 2007 Can anyone help? Link to comment https://forums.phpfreaks.com/topic/39048-read-csv-and-write-files-for-each-entry/#findComment-188522 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.