簡體   English   中英

$ _FILES [“上傳”] [“ tmp_name”]有什么作用

[英]What does $ _FILES [“upload”] [“tmp_name”]

我對php有一個一般性的問題,我不明白$ _FILES [“ upload”] [“ tmp_name”]是什么,為什么我應該將文件上載到tmp文件夾,而不將文件上載到永久文件夾? 感謝您的閱讀,祝您有美好的一天!

PHP解釋器使用生成的名稱將上傳的文件放在臨時目錄中,然后在運行PHP腳本之前將路徑存儲在$_FILES['...']['tmp_name']

您可以使用is_uploaded_file()來確保$_FILES['...']['tmp_name']內容確實是上載文件的路徑(並且在請求中並未以某種方式進行欺騙),然后使用move_uploaded_file()將文件放到最終目的地。

或者,如果不需要存儲文件,也可以在不移動文件的情況下對其進行處理。

無論哪種方式,當腳本結束時,解釋器都會刪除它創建的用於存儲上載內容的臨時文件。

該代碼通常如下所示:

if (is_uploaded_file($_FILES['abc']['tmp_name'])) {
    // Generate the path where to store the file
    // Depending on the expected file type you can use getimagesize() 
    // or mime_content_type() to find the correct file extension
    // and various ways to generate an unique file name (to not overwrite
    // the file already existing in the storage directory)
    $finalpath = '...';

    if (move_uploaded_file($_FILES['abc']['tmp_name'], $finalpath)) {
        // Successfully uploaded and processed.
    } else {
        // Cannot move the file; maybe there is a permissions issue
        // or the destination directory simply doesn't exist.
    }
} else {
    // The upload failed.
    // Check the value of $_FILES['abc']['error'] to find out why
    // (usually no file was uploaded or the file is too large).
}

閱讀有關處理文件上傳的PHP方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM