2

I've looked for questions on this topic, but failed to get what I'm looking for. This is for C++, I need similar for PHP. This is for including php files, I just want to read a CSV file.

I have this:

if(file_exists("data.csv")){ echo "CSV file found"; $csv_data = file_get_contents("data.csv"); $lines = explode("\n", trim($csv_data)); $array = array(); foreach ($lines as $line){ $array[] = str_getcsv($line); }else {echo "File not found";} 

But I want to NOT specify the file name - i.e. generically load/read/open the file.

Is there any simple why of doing that? Doesn't make sense, but I was told to not have anything hard coded in my PHP script.

Thanks in advance.

4
  • You mean you want to replace "data.csv" with a variable? Commented Aug 29, 2012 at 14:10
  • You can just put the filename string in a variable. What do you mean by generically open the file? The is a specific file. Commented Aug 29, 2012 at 14:10
  • where would the file location come from? Commented Aug 29, 2012 at 14:10
  • I think he wants to do it with a file handle rather than the filename string. Commented Aug 29, 2012 at 14:14

3 Answers 3

3

use fgetcsv

if(file_exists("data.csv")){ echo "CSV file found"; $handle = fopen("data.csv", "r"); if(!$handle) die("Could not open file!"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); $row++; for ($c=0; $c < $num; $c++) { echo $data[$c] . "<br />\n"; } } fclose($handle); }else {echo "File not found";} 
Sign up to request clarification or add additional context in comments.

2 Comments

OK, I am probably missing something here. The OP states "I want to NOT specify the file name" and "I was told to not have anything hard coded in my PHP script". But your answer has the filename very much hardcoded. Still, this has three upvotes?! Why?
Because, I believe, when I was answering the question 7 hours ago it was more about how to open/parse a CSV file..dont know about the upvotes though :)
2

If you may not have anything hard coded in your script, you need to put those hardcoded things into some sort of external config file. You will have to hardcode the name of that config file into your bootstrap or whatever comes first in your application. Once the config is loaded, make the configuration data available in the places where it is needed. Not hardcoding configuration data into your code will allow you to create more reusable components and code, e.g. CSV Reader that can read any CSV file instead of a CSV Reader that can only read that one particular CSV file hardcoded into it.

Example:

// config.php <?php return array( 'csvFile' => '/path/to/file.csv', … ); // bootstrap.php <?php $config = include '/path/to/config.php'; … // someFile.php <?php include '/path/to/bootstrap.php'; $file = new SplFileObject($config['csvFile']); $file->setFlags(SplFileObject::READ_CSV); foreach ($file as $row) { // Do something with values } 

Comments

2

Put your code into a function...

function open_file($file_name) { if (!file_exists($file_name)) { return false; } $csv_data = file_get_contents($file_name); $lines = explode("\n", trim($csv_data)); $array = array(); foreach ($lines as $line) { $array[] = str_getcsv($line); } return $array; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.