Da im vorherigen Artikel „Das Problem verstümmelter Dateinamen beim Hochladen chinesischer Dateien in PHP“ auf das Problem des Datei-Uploads gestoßen ist, wollen wir einige Probleme zusammenfassen, die beim Hochladen von Dateien in PHP häufig auftreten, damit Sie dies nicht tun müssen Achten Sie darauf, wenn Sie sie in Zukunft verwenden.
1. Erstellen Sie zunächst die einfachste Upload-Datei
<span> 1</span> <span><</span><span>html</span><span>></span> <span> 2</span> <span><</span><span>head</span><span>></span> <span> 3</span> <span><</span><span>meta </span><span>http-equiv</span><span>="Content-Type"</span><span> content</span><span>="text/html; charset=utf-8"</span><span>></span> <span> 4</span> <span></</span><span>head</span><span>></span> <span> 5</span> <span><</span><span>body</span><span>></span> <span> 6</span> <span> 7</span> <span><</span><span>form </span><span>action</span><span>="upload_file.php"</span><span> method</span><span>="post"</span> <span> 8</span> <span>enctype</span><span>="multipart/form-data"</span><span>></span> <span> 9</span> <span><</span><span>label </span><span>for</span><span>="file"</span><span>></span>Filename:<span></</span><span>label</span><span>></span> <span>10</span> <span><</span><span>input </span><span>type</span><span>="file"</span><span> name</span><span>="file"</span><span> id</span><span>="file"</span> <span>/></span> <span>11</span> <span><</span><span>br </span><span>/></span> <span>12</span> <span><</span><span>input </span><span>type</span><span>="submit"</span><span> name</span><span>="submit"</span><span> value</span><span>="Submit"</span> <span>/></span> <span>13</span> <span></</span><span>form</span><span>></span> <span>14</span> <span>15</span> <span></</span><span>body</span><span>></span> <span>16</span> <span></</span><span>html</span><span>></span>
<span> 1</span> <?<span>php </span><span> 2</span> <span>if</span> ((<span>$_FILES</span>["file"]["size"] < 20000<span>) </span><span> 3</span> <span> { </span><span> 4</span> <span>if</span> (<span>$_FILES</span>["file"]["error"] > 0<span>) </span><span> 5</span> <span> { </span><span> 6</span> <span>echo</span> "Return Code: " . <span>$_FILES</span>["file"]["error"] . "<br />"<span>; </span><span> 7</span> <span> } </span><span> 8</span> <span>else</span> <span> 9</span> <span> { </span><span>10</span> <span>echo</span> "Upload: " . <span>$_FILES</span>["file"]["name"] . "<br />"<span>; </span><span>11</span> <span>echo</span> "Type: " . <span>$_FILES</span>["file"]["type"] . "<br />"<span>; </span><span>12</span> <span>echo</span> "Size: " . (<span>$_FILES</span>["file"]["size"] / 1024) . " Kb<br />"<span>; </span><span>13</span> <span>echo</span> "Temp file: " . <span>$_FILES</span>["file"]["tmp_name"] . "<br />"<span>; </span><span>14</span> <span>15</span> <span>if</span> (<span>file_exists</span>("upload/" . <span>$_FILES</span>["file"]["name"<span>])) </span><span>16</span> <span> { </span><span>17</span> <span>echo</span> <span>$_FILES</span>["file"]["name"] . " already exists. "<span>; </span><span>18</span> <span> } </span><span>19</span> <span>else</span> <span>20</span> <span> { </span><span>21</span> <span>move_uploaded_file</span>(<span>$_FILES</span>["file"]["tmp_name"], <span>22</span> "upload/" . <span>$_FILES</span>["file"]["name"<span>]); </span><span>23</span> <span>echo</span> "Stored in: " . "upload/" . <span>$_FILES</span>["file"]["name"<span>]; </span><span>24</span> <span> } </span><span>25</span> <span> } </span><span>26</span> <span> } </span><span>27</span> <span>else</span> <span>28</span> <span> { </span><span>29</span> <span>echo</span> "Invalid file"<span>; </span><span>30</span> <span> } </span><span>31</span> ?>
2. Verstehen Sie dann den Wert der superglobalen Variablen $_FILES
$_FILES['userfile']['name']
$_FILES['userfile']['type']
$_FILES['userfile']['size']
$_FILES['userfile']['tmp_name']
$_FILES['userfile']['error']
Darunter alle Werte von $_FILES['userfile']['error']:
UPLOAD_ERR_OK Der Wert ist 0, es tritt kein Fehler auf und die Datei wurde erfolgreich hochgeladen.
UPLOAD_ERR_INI_SIZE Der Wert ist 1 und die hochgeladene Datei überschreitet den durch die Option upload_max_filesize in php.ini begrenzten Wert.
UPLOAD_ERR_FORM_SIZE Bei einem Wert von 2 überschreitet die Größe der hochgeladenen Datei den durch die Option MAX_FILE_SIZE im HTML-Formular angegebenen Wert.
UPLOAD_ERR_PARTIAL Der Wert ist 3, die Datei wird nur teilweise hochgeladen.
UPLOAD_ERR_NO_FILE Der Wert ist 4, es wurde keine Datei hochgeladen.
UPLOAD_ERR_NO_TMP_DIR hat einen Wert von 6 und der temporäre Ordner kann nicht gefunden werden. Eingeführt in PHP 4.3.10 und PHP 5.0.3.
UPLOAD_ERR_CANT_WRITE Der Wert ist 7, das Schreiben der Datei ist fehlgeschlagen. Eingeführt in PHP 5.1.0.
3. Viele Situationen: Es ist notwendig, den hochgeladenen Dateityp genau zu beurteilen
Wir wissen, dass es unklug ist, $_FILES['userfile']['type'] zu verwenden, um den Typ der hochgeladenen Datei zu bestimmen, da die Beurteilung auf dem Suffixnamen der Datei basiert mp3 Das Suffix der Datei wird in jpg geändert, um sie als Bild zum Hochladen zu tarnen. Daher empfiehlt PHP offiziell die Verwendung der PHP-Erweiterung php_fileinfo, um den Mime der Datei zu ermitteln Linux ist etwas anders.
4. Szenario 1: Die hochgeladene Datei automatisch mit demselben Namen umbenennen
<span> 1</span> <span>if</span> (<span>file_exists</span>("./upload/" . <span>$_FILES</span>["file"]["name"<span>])) </span><span> 2</span> <span>{ </span><span> 3</span> <span>do</span><span>{ </span><span> 4</span> <span>$suffix</span> =""<span>; </span><span> 5</span> <span>$suffix_length</span> = 4<span>; </span><span> 6</span> <span>$str</span> = "0123456789abcdefghijklmnopqrstuvwxyz"<span>; </span><span> 7</span> <span>$len</span> = <span>strlen</span>(<span>$str</span>)-1<span>; </span><span> 8</span> <span>//</span><span>文件名后追加4个随机字符 </span> <span> 9</span> <span>for</span>(<span>$i</span>=0 ; <span>$i</span><<span>$suffix_length</span>; <span>$i</span>++<span>){ </span><span>10</span> <span>$suffix</span> .= <span>$str</span>[<span>rand</span>(0,<span>$len</span><span>)]; </span><span>11</span> <span> } </span><span>12</span> <span>$upload_filename</span> = <span>$_FILES</span>['file']['name'<span>]; </span><span>13</span> <span>$filename</span> = <span>substr</span>(<span>$upload_filename</span>,0,<span>strrpos</span>(<span>$upload_filename</span>,".")).<span>$suffix</span>.".".<span>substr</span>(<span>$upload_filename</span>,<span>strrpos</span>(<span>$_FILES</span>["file"]["name"],".")+1<span>); </span><span>14</span> }<span>while</span>(<span>file_exists</span>("./upload/".<span>$filename</span><span>)); </span><span>15</span> <span>move_uploaded_file</span>(<span>$_FILES</span>["file"]["tmp_name"],"./upload/" . <span>$filename</span><span>); </span><span>16</span> }<span>else</span><span>{ </span><span>17</span> <span>move_uploaded_file</span>(<span>$_FILES</span>["file"]["tmp_name"], "upload/" . <span>$_FILES</span>["file"]["name"<span>]); </span><span>18</span> }
5. Szenario 2: Dateien basierend auf dem Datum in Verzeichnisse hochladen
<span>1</span> <span>$structure</span> = './'.<span>date</span>("Y").'/'.<span>date</span>("m").'/'.<span>date</span>("d").'/'<span>; </span><span>2</span> <span>3</span> <span>4</span> <span>if</span> (!<span>mkdir</span>(<span>$structure</span>, 0777, <span>true</span><span>)) { </span><span>5</span> <span>die</span>('Failed to create folders...'<span>); </span><span>6</span> <span>} </span><span>7</span> <span>8</span> <span>move_uploaded_file</span>(<span>$_FILES</span>["file"]["tmp_name"],<span>$structure</span> . <span>$_FILES</span>["file"]["name"]);
6. Szenario 3: Hochladen mehrerer Dateien
<span>1</span> <span><</span><span>form </span><span>action</span><span>=""</span><span> method</span><span>="post"</span><span> enctype</span><span>="multipart/form-data"</span><span>> <span>2</span> <span><</span><span>p</span><span>></span><span>Pictures: </span><span>3</span> <span><</span><span>input </span><span>type</span><span>="file"</span><span> name</span><span>="pictures[]"</span> <span>/></span> <span>4</span> <span><</span><span>input </span><span>type</span><span>="file"</span><span> name</span><span>="pictures[]"</span> <span>/></span> <span>5</span> <span><</span><span>input </span><span>type</span><span>="file"</span><span> name</span><span>="pictures[]"</span> <span>/></span> <span>6</span> <span><</span><span>input </span><span>type</span><span>="submit"</span><span> value</span><span>="Send"</span> <span>/></span> <span>7</span> <span></</span><span>p</span><span>></span> <span>8</span> <span></</span><span>form</span><span>></span>
<span>1</span> <?<span>php </span><span>2</span> <span>foreach</span> (<span>$_FILES</span>["pictures"]["error"] <span>as</span> <span>$key</span> => <span>$error</span><span>) { </span><span>3</span> <span>if</span> (<span>$error</span> ==<span> UPLOAD_ERR_OK) { </span><span>4</span> <span>$tmp_name</span> = <span>$_FILES</span>["pictures"]["tmp_name"][<span>$key</span><span>]; </span><span>5</span> <span>$name</span> = <span>$_FILES</span>["pictures"]["name"][<span>$key</span><span>]; </span><span>6</span> <span>move_uploaded_file</span>(<span>$tmp_name</span>, "data/<span>$name</span>"<span>); </span><span>7</span> <span> } </span><span>8</span> <span>} </span><span>9</span> ?>
In manchen Fällen ist diese Variablenstruktur für mehrere Dateien nicht einfach zu verwenden:
array(1) {
["upload"]=>array(2) {
["name"]=>array(2) {
[0]=>string(9)"file0.txt"
[1]=>string(9)"file1.txt"
}
["type"]=>array(2) {
[0]=>string(10)"text/plain"
[1]=>string(10)"text/html"
}
}
}
In vielen Fällen brauchen wir eine Struktur wie diese
array(1) {
["upload"]=>array(2) {
[0]=>array(2) {
["name"]=>string(9)"file0.txt"
["type"]=>string(10)"text/plain"
},
[1]=>array(2) {
["name"]=>string(9)"file1.txt"
["type"]=>string(10)"text/html"
}
}
}
Verwenden Sie die folgende Funktion, um die Struktur einfach zu konvertieren
<span>1</span> <span>function</span> diverse_array(<span>$vector</span><span>) { </span><span>2</span> <span>$result</span> = <span>array</span><span>(); </span><span>3</span> <span>foreach</span>(<span>$vector</span> <span>as</span> <span>$key1</span> => <span>$value1</span><span>) </span><span>4</span> <span>foreach</span>(<span>$value1</span> <span>as</span> <span>$key2</span> => <span>$value2</span><span>) </span><span>5</span> <span>$result</span>[<span>$key2</span>][<span>$key1</span>] = <span>$value2</span><span>; </span><span>6</span> <span>return</span> <span>$result</span><span>; </span><span>7</span> <span>} </span><span>8</span> <span>$upload</span> = diverse_array(<span>$_FILES</span>["upload"]);
7.
<span><</span><span>input </span><span>type</span><span>="hidden"</span><span> name</span><span>="MAX_FILE_SIZE"</span><span> value</span><span>="字节"</span> <span>/></span>
Kann die Größe der Upload-Datei begrenzen (kann umgangen werden).
max_input_time = 60,每个脚本可以消耗的时间,单位也是秒
memory_limit = 128M,这个是脚本运行最大消耗的内存
post_max_size = 8M,表单提交最大数据为 8M,此项不是限制上传单个文件的大小,而是针对整个表单的提交数据进行限制的。
upload_max_filesize = 2M ,上载文件的最大许可大小
<span>1</span> <span>location / { </span><span>2</span> root html<span>; </span><span>3</span> index index.html index.htm<span>; </span><span>4 </span>client_max_body_size 1000m<span>; </span><span>5</span> }
以上就介绍了php上传文件常见问题(基础),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。