However, all that happens is the browser goes blank. Once I can get the file opened an put into an array I can manipulate the data. Unfortunately, I can't get past step one. Can you offer some suggestions?Thanks for your help.Chris"> Read a csv file - Forums

Forums

Read a csv file

chriso0258 03 Nov, 2015
Hello,
I'm trying to read a csv file uploaded to a directory (which has the appropriate rights). Before I continue writing code, I want to make sure the file is being read so I'm first seeing if I can print the contents. I'm using the following script in a custom code located in the onLoad event:


<?php
$file = fopen("/temp/personnel.csv","r");
while(! feof($file))
  {
  echo "<br>" . (fgetcsv($file));
  }
fclose($file);
?> 


However, all that happens is the browser goes blank. Once I can get the file opened an put into an array I can manipulate the data. Unfortunately, I can't get past step one. Can you offer some suggestions?

Thanks for your help.
Chris
GreyHead 03 Nov, 2015
Hi Chris,

If the browser goes blank there is probably a PHP Error, if you temporarily set Site Error Reporting to Maximum you should see a more useful message,

My best guess is that the file folder path isn't complete and a bit more is needed before the /temp/. Please try
$file = fopen( JPATH_SITE."/temp/personnel.csv","r" );

Bob
chriso0258 03 Nov, 2015
Hi Bob, thanks for your reply,

I did try that path in addition to JPATH_SITE."/html/temp/personnel.csv since I'm running Ubuntu 14.04 and the www/html directory is the default directory. I also tried putting Error Reporting on Maximum and I still only get a blank page. Is there a way to verify what path is being sent if that indeed is the issue?
GreyHead 03 Nov, 2015
Hi Chris,

Try this to debug
<?php
$file = JPATH_SITE."/temp/personnel.csv";
echo'<div>$file: '.print_r($file, true).'</div>';
/*
$file = fopen($file,"r");
while(! feof($file))
  {
  echo "<br>" . (fgetcsv($file));
  }
fclose($file);
*/
?> 

Bob
chriso0258 03 Nov, 2015
Hi Bob,

Thanks for your reply. Output was

$file: /var/www/html/temp/personnel.csv


I've modified the code as follows to see where it's failing:
<?php
echo "<h2>Processing file....</h2>";

$file = fopen("/var/www/html/temp/personnel.csv","r");

echo "<h2>The file is opened</h2>";
/*
while(! feof($file))
  {
  var_dump(fgetcsv($file));
  }

fclose($file);
*/
echo "<br>End Processing.";
?> 


The error I'm getting is

Warning: fopen(/var/www/html/temp/personnel.csv): failed to open stream: No such file or directory in /var/www/html/administrator/components/com_chronoforms5/chronoforms/actions/custom_code/custom_code.php(20) : eval()'d code on line 4



Does this shed any more light? I'm still in the dark.
chriso0258 03 Nov, 2015
Answer
Hello again,

This is embarrassing 😶 . The directory is not temp, it's tmp.

Thanks Bob for your help. Sorry to waste your time 😟

Chris.
GreyHead 03 Nov, 2015
Hi Chris,

Well spotted - those can be the hardest to find.

Bob.
This topic is locked and no more replies can be posted.