Skip to content

Instantly share code, notes, and snippets.

@kitsaels
Last active March 18, 2021 21:12

Revisions

  1. kitsaels revised this gist Jun 4, 2019. 1 changed file with 19 additions and 0 deletions.
    19 changes: 19 additions & 0 deletions parseProperties.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    <?php

    function parseProperties($message) {
    $properties = explode(';', $message);
    $result = array();
    foreach ($properties as $prop) {
    $property = trim($prop);
    $pos = strpos($property, '=');
    if ($pos > 0) {
    $key = trim(substr($property, 0, $pos));
    $val = trim(substr($property, $pos+1));
    if (strlen($val) > 0 && $val{0} == '"') {
    $val = substr($val, 1, -1);
    }
    $result[$key] = $val;
    }
    }
    return $result;
    }
  2. kitsaels revised this gist Jun 3, 2019. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion getFilename.php
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ function getFilename($header) {
    return $matches[1];
    }
    if (preg_match('/Content-Disposition:.*?filename=([^; ]+)/', $header, $matches)) {
    return $matches[1];
    return rawurldecode($matches[1]);
    }
    throw new Exception(__FUNCTION__ .": Filename not found");
    }
    2 changes: 1 addition & 1 deletion getFilename2.php
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ function getFilename($header) {
    return $matches[1];
    }
    if (preg_match('/filename=([^; ]+)/', $header, $matches)) {
    return $matches[1];
    return rawurldecode($matches[1]);
    }
    throw new Exception(__FUNCTION__ .": Filename not found");
    }
  3. kitsaels revised this gist Jun 3, 2019. 1 changed file with 22 additions and 0 deletions.
    22 changes: 22 additions & 0 deletions getFilename2.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    <?php

    function getFilename($header) {
    if (preg_match('/filename="(.+?)"/', $header, $matches)) {
    return $matches[1];
    }
    if (preg_match('/filename=([^; ]+)/', $header, $matches)) {
    return $matches[1];
    }
    throw new Exception(__FUNCTION__ .": Filename not found");
    }

    header("Content-Type: text/plain;charset=UTF-8");

    $s1 = 'attachment; filename="FILE NAME HERE"';
    echo getFilename($s1) . PHP_EOL;

    $s2 = 'attachment; filename=file.ext';
    echo getFilename($s2) . PHP_EOL;

    $s3 = $_SERVER['HTTP_CONTENT_DISPOSITION'] ?? "";
    echo getFilename($s3) . PHP_EOL;
  4. kitsaels created this gist Jun 3, 2019.
    20 changes: 20 additions & 0 deletions getFilename.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    <?php

    function getFilename($header) {
    if (preg_match('/Content-Disposition:.*?filename="(.+?)"/', $header, $matches)) {
    return $matches[1];
    }
    if (preg_match('/Content-Disposition:.*?filename=([^; ]+)/', $header, $matches)) {
    return $matches[1];
    }
    throw new Exception(__FUNCTION__ .": Filename not found");
    }

    $s1 = 'Content-Disposition: attachment; filename="FILE NAME HERE"';
    echo getFilename($s1) . PHP_EOL;

    $s2 = 'Content-Disposition: attachment; filename=file.ext';
    echo getFilename($s2) . PHP_EOL;

    $s3 = 'Content-Disposition: attachment; name=file.ext';
    echo getFilename($s3) . PHP_EOL;