Read CSV and write file


#1

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 FROM CSV HERE (CITATION FROM CSV HERE) - PUBLISHER 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]

<?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.' ('.$citation.') - '.$publisher.' '; if(file_exists($dir.$encode_url) && $check_exists == "TRUE") { echo "File Already Exists With That Name"; } else { $file_create = fopen($dir.$encode_url, 'w') or die("can't create file"); fwrite($file_create, $code); fclose($file_create); } } ?>

[/php]

ADMIN EDIT: Added PHP code tags for readability. Please review http://phphelp.com/guidelines.php for posting guidelines.


#2

take a look at the fread function http://php.net/fread

Used with the fopen, fclose, feof, etc… You should be able to do what you need.