Skip to content

Commit

Permalink
fixes #1 better handling for UTF8 csv file exported from Mac
Browse files Browse the repository at this point in the history
* accept *.csv files, not only *.txt
* remove BOM
* automatic detection of line endings
  • Loading branch information
plegall committed Jun 19, 2019
1 parent a750515 commit b47ece6
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions admin_update.php
Expand Up @@ -29,6 +29,13 @@

$admin_base_url = get_root_url().'admin.php?page=plugin-properties_mass_update-update';

function ppmu_remove_utf8_bom($text)
{
$bom = pack('H*','EFBBBF');
$text = preg_replace("/^$bom/", '', $text);
return $text;
}

// +-----------------------------------------------------------------------+
// | Checks |
// +-----------------------------------------------------------------------+
Expand Down Expand Up @@ -65,7 +72,7 @@

if (UPLOAD_ERR_OK == $_FILES['update']['error'])
{
if ('text/plain' == $_FILES['update']['type'])
if (in_array($_FILES['update']['type'], array('text/plain', 'text/csv')))
{
$text_file = $_FILES['update']['tmp_name'];
}
Expand All @@ -76,8 +83,10 @@

if (isset($text_file))
{
$raw = file_get_contents($text_file);
$raw_lines = explode("\n", $raw);
ini_set("auto_detect_line_endings", true);
$raw_lines = file($text_file);

$raw_lines[0] = ppmu_remove_utf8_bom($raw_lines[0]);

$query = 'SELECT id, file FROM '.IMAGES_TABLE.';';
$existing_files = hash_from_query($query, 'file');
Expand All @@ -86,14 +95,14 @@
$update_files = array();
$missing_files = array();
$tags_of = array();

foreach ($raw_lines as $raw_line)
{
if (!preg_match($regex_for_separator[$_POST['separator']], $raw_line, $matches))
{
continue;
}

// in case the same file is defined twice, we only save the first occurence
if (isset($update_files[$matches[1]]) or isset($missing_files[$matches[1]]))
{
Expand Down

0 comments on commit b47ece6

Please sign in to comment.