Skip to content

Commit

Permalink
MDL-39052 create temp files only in Moodle temp folder
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Apr 16, 2013
1 parent b3661ab commit 667447d
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/csvlib.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,15 @@ function load_csv_content(&$content, $encoding, $delimiter_name, $column_validat
$csv_delimiter = csv_import_reader::get_delimiter($delimiter_name);
// $csv_encode = csv_import_reader::get_encoded_delimiter($delimiter_name);

// create a temporary file and store the csv file there.
$fp = tmpfile();
// Create a temporary file and store the csv file there,
// do not try using fgetcsv() because there is nothing
// to split rows properly - fgetcsv() itself can not do it.
$tempfile = tempnam(make_temp_directory('/cvsimport'), 'tmp');
if (!$fp = fopen($tempfile, 'w+b')) {
$this->_error = get_string('cannotsavedata', 'error');
@unlink($tempfile);
return false;
}
fwrite($fp, $content);
fseek($fp, 0);
// Create an array to store the imported data for error checking.
Expand All @@ -126,6 +133,7 @@ function load_csv_content(&$content, $encoding, $delimiter_name, $column_validat
if (!isset($columns[0])) {
$this->_error = get_string('csvemptyfile', 'error');
fclose($fp);
unlink($tempfile);
return false;
} else {
$col_count = count($columns[0]);
Expand All @@ -137,6 +145,7 @@ function load_csv_content(&$content, $encoding, $delimiter_name, $column_validat
if ($result !== true) {
$this->_error = $result;
fclose($fp);
unlink($tempfile);
return false;
}
}
Expand All @@ -147,6 +156,7 @@ function load_csv_content(&$content, $encoding, $delimiter_name, $column_validat
if (count($rowdata) !== $col_count) {
$this->_error = get_string('csvweirdcolumns', 'error');
fclose($fp);
unlink($tempfile);
$this->cleanup();
return false;
}
Expand All @@ -160,6 +170,7 @@ function load_csv_content(&$content, $encoding, $delimiter_name, $column_validat
fwrite($filepointer, $storedata);

fclose($fp);
unlink($tempfile);
fclose($filepointer);

$datacount = count($columns);
Expand Down

0 comments on commit 667447d

Please sign in to comment.