|
| 1 | +<?php |
| 2 | + |
| 3 | + /** |
| 4 | + * |
| 5 | + * PHP Utilities - File Uploader |
| 6 | + * @see https://github.com/andreazorzi/php-utilities |
| 7 | + * @author Andrea Zorzi (andreazorzi) <info@zorziandrea.com> |
| 8 | + * @version 1.0.0 |
| 9 | + * |
| 10 | + */ |
| 11 | + |
| 12 | + /** |
| 13 | + * |
| 14 | + * Upload single file on server |
| 15 | + * |
| 16 | + * @param Array $file $_FILES["file_name"] variable |
| 17 | + * @param String $folder The folder where to save the file |
| 18 | + * @param Array $settings { |
| 19 | + * Additional settings |
| 20 | + * |
| 21 | + * @param Array $accept File extensions accepted, must start with dot, if not set, it does not accept any file |
| 22 | + * @param String $nameformat Pattern to set the new file name, if not set, it keeps the original name |
| 23 | + * @param int $iterator Starting iterator number |
| 24 | + * } |
| 25 | + * |
| 26 | + * @return int Status code indicating whether the operation was successful { |
| 27 | + * 0: The operation was successful |
| 28 | + * 1: An error occurred with the uploaded file |
| 29 | + * 2: The uploaded file is not an allowed type |
| 30 | + * 3: The file was not saved |
| 31 | + * } |
| 32 | + * |
| 33 | + */ |
| 34 | + function singleFileUpload(&$file, $folder, $settings = array()){ |
| 35 | + checkFolder($folder); |
| 36 | + |
| 37 | + if(!isset($settings["iterator"])){ |
| 38 | + $settings["iterator"] = 0; |
| 39 | + } |
| 40 | + |
| 41 | + if(isset($file) && $file["error"] == 0){ |
| 42 | + $fileinfo = getFileInfo($file["name"]); |
| 43 | + $filename = $file["name"]; |
| 44 | + |
| 45 | + if(isset($settings["accept"]) && !in_array($fileinfo["ext"], $settings["accept"])){ |
| 46 | + return 2; |
| 47 | + } |
| 48 | + |
| 49 | + if(isset($settings["nameformat"])){ |
| 50 | + $filename = $settings["nameformat"]; |
| 51 | + $filename = str_replace("{filename}", $fileinfo["name"], $filename); |
| 52 | + $filename = str_replace(".{ext}", $fileinfo["ext"], $filename); |
| 53 | + $filename = str_replace("{t}", time(), $filename); |
| 54 | + $filename = str_replace("{i}", $settings["iterator"], $filename); |
| 55 | + |
| 56 | + $settings["iterator"] += 1; |
| 57 | + } |
| 58 | + |
| 59 | + if(file_put_contents($folder.$filename, file_get_contents($file["tmp_name"])) !== false){ |
| 60 | + return 0; |
| 61 | + } |
| 62 | + |
| 63 | + return 3; |
| 64 | + } |
| 65 | + |
| 66 | + return 1; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * |
| 71 | + * Upload miltiple files on server |
| 72 | + * |
| 73 | + * @param Array $file $_FILES["file_name"] variable |
| 74 | + * @param String $folder The folder where to save the file |
| 75 | + * @param Array $settings { |
| 76 | + * Additional settings |
| 77 | + * |
| 78 | + * @param Array $accept File extensions accepted, must start with dot, if not set, it does not accept any file |
| 79 | + * @param String $nameformat Pattern to set the new file name, if not set, it keeps the original name |
| 80 | + * @param int $iterator Starting iterator number |
| 81 | + * } |
| 82 | + * |
| 83 | + * @return Array Array of status codes for each uploaded files indicating whether the operation was successful { |
| 84 | + * 0: The operation was successful |
| 85 | + * 1: An error occurred with the uploaded file |
| 86 | + * 2: The uploaded file is not an allowed type |
| 87 | + * 3: The file was not saved |
| 88 | + * } |
| 89 | + * |
| 90 | + */ |
| 91 | + function multipleFileUpload(&$file, $folder, $settings = array()){ |
| 92 | + checkFolder($folder); |
| 93 | + |
| 94 | + if(!isset($settings["iterator"])){ |
| 95 | + $settings["iterator"] = 0; |
| 96 | + } |
| 97 | + |
| 98 | + $res = array(); |
| 99 | + |
| 100 | + for($i = 0; $i < count($file["name"]); $i++){ |
| 101 | + $status = 0; |
| 102 | + |
| 103 | + if($file["error"][$i] == 0){ |
| 104 | + $fileinfo = getFileInfo($file["name"][$i]); |
| 105 | + $filename = $file["name"][$i]; |
| 106 | + |
| 107 | + if(isset($settings["accept"]) && !in_array($fileinfo["ext"], $settings["accept"])){ |
| 108 | + $res[] = 2; |
| 109 | + } |
| 110 | + else{ |
| 111 | + if(isset($settings["nameformat"])){ |
| 112 | + $filename = $settings["nameformat"]; |
| 113 | + $filename = str_replace("{filename}", $fileinfo["name"], $filename); |
| 114 | + $filename = str_replace(".{ext}", $fileinfo["ext"], $filename); |
| 115 | + $filename = str_replace("{t}", time(), $filename); |
| 116 | + $filename = str_replace("{i}", $settings["iterator"], $filename); |
| 117 | + |
| 118 | + $settings["iterator"] += 1; |
| 119 | + } |
| 120 | + |
| 121 | + if(file_put_contents($folder.$filename, file_get_contents($file["tmp_name"][$i])) !== false){ |
| 122 | + $res[] = 0; |
| 123 | + } |
| 124 | + else{ |
| 125 | + $res[] = 3; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + else{ |
| 130 | + $res[] = 1; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return $res; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * |
| 139 | + * Check if the passed folder exists, in case the folder is created |
| 140 | + * |
| 141 | + * @param String $folder The folder path to check |
| 142 | + * |
| 143 | + */ |
| 144 | + function checkFolder($folder){ |
| 145 | + if (!file_exists($folder)) { |
| 146 | + mkdir($folder, 0777, true); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * |
| 152 | + * Get the information of a given file |
| 153 | + * |
| 154 | + * @param String $filename The path of the file to check |
| 155 | + * |
| 156 | + * @return Array Array that contains the name and extension of the file |
| 157 | + * |
| 158 | + */ |
| 159 | + function getFileInfo($filename){ |
| 160 | + $pathinfo = pathinfo($filename); |
| 161 | + |
| 162 | + return array( |
| 163 | + "ext" => isset($pathinfo["extension"]) ? ".".$pathinfo["extension"] : "", |
| 164 | + "name" => isset($pathinfo["filename"]) ? $pathinfo["filename"] : "" |
| 165 | + ); |
| 166 | + |
| 167 | + isset($pathinfo["extension"]) ? $pathinfo["extension"] : ""; |
| 168 | + } |
| 169 | + |
| 170 | +?> |
0 commit comments