HelperFile.php 4.01 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 1/28/16
 * Time: 8:05 AM
 */

namespace qfq;

require_once(__DIR__ . '/../Constants.php');

13
14
15
16
/**
 * Class HelperFile
 * @package qfq
 */
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class HelperFile {

    /**
     * Iterate over array $files. Delete only named files which are stored in '/tmp/' . DOWNLOAD_FILE_PREFIX.
     *
     * @param array $files
     */
    public static function cleanTempFiles(array $files) {

        foreach ($files as $file) {
            if (self::isQfqTemp($file)) {
                unlink($file);
            }

            $dir = dirname($file);
            if (self::isQfqTemp($dir)) {
                rmdir($dir);
            }
        }
    }

38
39
40
41
42
43
44
45
    /**
     * Returns a uniqe (use for temporary) filename, prefixed with QFQ TMP_FILE_PREFIX
     *
     * @return bool|string
     */
    public static function tempnam(){
        return tempnam(sys_get_temp_dir(), TMP_FILE_PREFIX);
    }
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

    /**
     * Check against standard QFQ Temp location. If it is a Qfq Temp location, return true, else false.
     *
     * @param string $name Absolute filename.
     *
     * @return bool
     */
    public static function isQfqTemp($name) {
        $prefix = sys_get_temp_dir() . '/' . TMP_FILE_PREFIX;
        $len = strlen($prefix);

        return (substr($name, 0, $len) == $prefix);
    }

    /**
     * Creates a temporary directory.
     */
    public static function mktempdir() {
        $name = tempnam(sys_get_temp_dir(), TMP_FILE_PREFIX);
        unlink($name);
        mkdir($name);

        return $name;
    }
71
72
73
74
75

    /**
     * Return the mimetype of $pathFilename
     *
     * @param string $pathFilename
76
     * @param bool $flagIgnoreError
77
78
79
     * @return string
     * @throws UserFormException
     */
80
    public static function getMimeType($pathFilename, $flagIgnoreError = false) {
81
82
83
84

        // E.g.: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=binary'
        $fileMimeType = exec('file --brief --mime ' . $pathFilename, $output, $return_var);
        if ($return_var != 0) {
85
86
87
88
89

            if ($flagIgnoreError) {
                return '';
            }

90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
            throw new UserFormException('Error get mime type of upload.', ERROR_UPLOAD_GET_MIME_TYPE);
        }

        return $fileMimeType;
    }

    /**
     * Returns an array with filestat information to $pathFileName
     *
     * @param $pathFileName
     * @return array
     * @throws UserFormException
     */
    public static function getFileStat($pathFileName) {
        $vars = array();

        if (empty($pathFileName)) {
            return array();
        }

        $pathFileName = self::correctRelativPathFileName($pathFileName);

        $vars[VAR_FILE_MIME_TYPE] = self::getMimeType($pathFileName);
        $vars[VAR_FILE_SIZE] = filesize($pathFileName);

        return $vars;
    }

    /**
     * Correct $pathFilename, if the cwd is .../qfq/api'
     *
     * @param $pathFileName
     * @return string
     */
    public static function correctRelativPathFileName($pathFileName) {

        if (empty($pathFileName)) {
            return '';
        }

        if ($pathFileName[0] == '/') {
            return $pathFileName;
        }

        if (substr(getcwd(), -8) == '/qfq/api') {
            return '../../../../../' . $pathFileName;
        }

        return $pathFileName;
    }

    /**
     * Split $pathFileName into it's components and fill an array, with array keys like used in STORE_VAR.
     *
     * @param string $pathFileName
     * @return array
     */
    public static function pathInfo($pathFileName) {
        $vars = array();

        $pathParts = pathinfo($pathFileName);
        $vars[VAR_FILENAME] = $pathFileName;

        if (isset($pathParts['basename'])) {
            $vars[VAR_FILENAME_ONLY] = $pathParts['basename'];
        }

        if (isset($pathParts['filename'])) {
            $vars[VAR_FILENAME_BASE] = $pathParts['filename'];
        }

        if (isset($pathParts['extension'])) {
            $vars[VAR_FILENAME_EXT] = $pathParts['extension'];
        }

        return $vars;
    }

168
}