Page 3 of 3

Re: Using PDO and running into problems

Posted: Thu May 03, 2012 8:11 am
by Pavilion
OK - I'm attempting your approach x_mutatis_mutandis_x (without much success - sigh). Following is the applicable script block:

Code: Select all

echo $upload . " upload echo <br />";
echo pathinfo($upload,PATH_INFO_EXTENSION);

function processFile($upload) {
	switch (pathinfo($upload, PATH_INFO_EXTENSION)) {
		case 'xls':
			echo "xls content returned ";
			break;
		case 'xlsx':
			return processExcelXls($upload);
			break;
		case 'csv':
		   echo "csv content returned <br />";
		   return processExcelCsv($upload);
		   break;
		case 'txt':
		   echo "txt content returned <br />";
		   return processExcelCsv($upload);
		   break;
		 default:
		   return file_get_contents($upload); //or echo error, however you want to handle the default case
        }
}
Right now I'm just trying to get the switch statement working. You'll notice there are echos included. The switch block echos are not showing up when they should. So... in an attempt at problem solving I included the following two lines BEFORE the switch:

Code: Select all

echo $upload . " upload echo <br />";
echo pathinfo($upload,PATH_INFO_EXTENSION);
These two lines echo the following:
upload/2_ExportCSV.csv upload echo

Warning: pathinfo() expects parameter 2 to be long, string given in /home/content/38/7901938/html/tfm/import.php on line 72
The bolded output of the $upload echo is an accurate path to a valid file.

So... why the warning????

I've tried the following for pathinfo

Code: Select all

echo pathinfo("upload/" . $_SESSION['user_id'] . "_" . $_FILES["file"]["name"],PATH_INFO_EXTENSION);
And get the same results.

What am I doing wrong???

Thanks so much - all of you - for your advice and counsel.

Pavilion

Re: Using PDO and running into problems

Posted: Thu May 03, 2012 8:21 am
by Pavilion
OK - I've figured out part of the problem.

I changed the syntax from

Code: Select all

echo pathinfo($upload,PATH_INFO_EXTENSION);
to:

Code: Select all

echo pathinfo($upload, PATHINFO_EXTENSION);
And now these two echos

Code: Select all

echo $upload . " upload echo <br />";
echo pathinfo($upload, PATHINFO_EXTENSION);
are returning:
upload/2_ExportEmails_Title.txt upload echo
txt
So.. the error message is gone, but when I move the new syntax into the switch statement - as follows:

Code: Select all

function processFile($upload) {
	switch (pathinfo($upload, PATHINFO_EXTENSION)) {
		case 'xls':
			echo "xls content returned ";
			break;
		case 'xlsx':
			return processExcelXls($upload);
			break;
		case 'csv':
		   echo "csv content returned <br />";
		   return processExcelCsv($upload);
		   break;
		case 'txt':
		   echo "txt content returned <br />";
		   return processExcelCsv($upload);
		   break;
		 default:
		   return file_get_contents($upload); //or echo error, however you want to handle the default case
        }
}
The switch block is NOT echoing out
"txt content returned"
Advice and suggestions are welcome and I am thankful.

Pavilion

Re: Using PDO and running into problems

Posted: Thu May 03, 2012 8:48 am
by Celauran
It's working fine for me, so the error doesn't appear to be in the code you've posted.

Re: Using PDO and running into problems

Posted: Thu May 03, 2012 10:11 pm
by Pavilion
Celauran wrote:It's working fine for me, so the error doesn't appear to be in the code you've posted.
Thanks Celauran. I've got the Switch throwing echos now. It helps, I know I'm feeding the data through the select case properly. But that's about as far as I've gotten. Under the heading of "baby steps", I've spent some time reviewing PHPExcel Library and am completely over-whelmed. There are thousands of files in the whole library. I did a search for the functions I was referred to:

Code: Select all

function processExcelXls($filename) {
          //Use PHPExcel library (see url below for documentation/api)
}

function processExcelCsv($filename) {
          //Use fgetcsv() here
}
And have found nothing. And ... I must confess I'm back to the question I asked earlier. For me, newbie that I am, what is the benefit against the frustration? I'm willing to do this - but I've no idea where to even begin with the documentation. I tried transferring it from my local drive to the server through an FTP and the FTP timed out on me.

On the whole - PHPExcel seems to be a robust tool and I'm not arguing the validity. But.... within the context of my newbie status, the fact that I'm just trying to give users a way to upload some email addresses, and that I won't be having users interact with Excel on a regular basis... what is the benefit?

And if you still recommend going this route (I'm game) - but I need some help with the basics. Are the above functions self-defined - or should they be defined in the library? If they are self-defined, then what exactly am I suppose to use from the library?

Thank you so much for your patience - it is deeply appreciated.

Pavilion

Re: Using PDO and running into problems

Posted: Fri May 04, 2012 9:04 am
by x_mutatis_mutandis_x
if you are'nt allowing users to upload excel files with .xls or .xlsx then you don't need PHPExcel, you can just return an error message or false.
[syntax]
function processExcelXls($filename) {
return false;
}
[/syntax]

You can use fgetcsv() in processExcelCsv($filename); return the valid data (or true).

So, in the script where you are calling the function processFile(), you can have:
[syntax]
$result = processFile($uploadedFile);
if ($result === false) {
//Show user an error saying file type not acceptable
} else {
//update database with data from $result
}
[/syntax]

EDIT: Typo and grammar correction.

Re: Using PDO and running into problems

Posted: Fri May 04, 2012 10:02 am
by Pavilion
Hello x_mutatis:

Thank you for responding:
if you are'nt allowing users to upload excel files with .xls or .xlsx then you don't need PHPExcel, you can just return an error message or false.
These are the types of files my current script is allowing for upload:

Code: Select all

$mimes = array('application/vnd.ms-excel','application/octet-stream','text/plain','text/csv','text/tsv');
You can use fgetcsv() in processExcelCsv($filename); return the valid data (or true).
Will fgetcsv() work with the types of files in $mimes?

If not ... what other file handlers do I need?

Thank you for your patience - you have no idea how much it is appreciated.

Pavilion

Re: Using PDO and running into problems

Posted: Fri May 04, 2012 10:20 am
by x_mutatis_mutandis_x
Will work for 'text/csv' and 'text/tsv' (I'm guessing tsv is tab-separated?).
'text/plain' will work, if the file has data with comma-separated or a delimiter you can recognize (basically text file in .csv format).

You have a 3rd parameter, to specify the delimiter (default is ',') for the fgetcsv function:

Code: Select all

fgetcsv($filehandle); //for comma delimited
fgetcsv($filehandle, 0, "\t"); //for tab delimited (assuming you are using php version > 5.0.4; if not you need specify the max line length parameter > 0, for more details see PHP manual for fgetcsv())
You can always return false, if you are unable to parse the data in the file. So your switch block will become something like:

Code: Select all

function processFile($upload) {
         switch (pathinfo($upload, PATHINFO_EXTENSION)) {                
                 case 'csv':
                          return processExcelCsv($upload);
                 case 'tsv':
                          return processExcelCsv($upload, "\t");
                 case 'txt':
                          $delimiter = $_POST['delimiter']; //ask user what delimiter he/she is using in the txt file
                          return processExcelCsv($upload, $delimiter);
                 default:
                    return false; //or echo error, however you want to handle the default case
         }
 }

function processExcelCsv($upload, $delimiter = ',') {
         $filehandle = @fopen($upload, 'r');
         if ($filehandle === false) {
               return false;
         }

         $row = 0;
         $parsedData = array();

         while (($data = fgetcsv($filehandle, 0, $delimiter)) != false) {
                  $numFields = count($data);
                  $row++;

                  for ($column=0; $column<$numFields; $column++) {
                         $parsedData[$row][$column] = $data[$column];
                  }

          }

          fclose($filehandle);

           if (empty($parsedData)) {
                  return false;
           }

           return $parsedData;
}

Re: Using PDO and running into problems

Posted: Fri May 04, 2012 7:19 pm
by Pavilion
x_mutatis

Thank you for your help. Your solution worked. I'm not giving the users an option in delimiters right now, maybe later. But, right now I'm just trying to keep things simple.

At any rate, I can definitely see the long-term advantages to processing files with a switch block, it allows for revisions in the future.

Again, thank you so much for all your help.

Pavilion