PHP上傳檔案時無法上傳成功,$_FILES['screenshot']['tmp_name']為空白_PHP教程

來源:互聯網
上載者:User

PHP上傳檔案時無法上傳成功,$_FILES['screenshot']['tmp_name']為空白


最近在學習《HeadFirst PHP & MySQL》一書的第5章“使用儲存在檔案中的資料”,做一個檔案上傳的應用時,出現了錯誤,就是檔案無法成功上傳。這個問題困擾了我很久,不過還好最後終於解決了。原因是我上傳的圖片檔案大小超過了HTML 表單中 MAX_FILE_SIZE 選項指定的值32768Bytes即32KB導致無法上傳成功。

我使用了XAMPP(Apache + MySQL + PHP + Perl)整合開發包和Zend Studio 10.6作為PHP IDE開發環境,另外關於PHP調試我採用了XDebug,在Zend Studio10.6中配置我參考了博文Zend Studio 10.5 與 XDebug 調試| Zend Debugger 說明 Drupal 原始碼 (一)

我的addscore.php的PHP代碼如下:

    Guitar Wars - Add Your High Score    

Guitar Wars - Add Your High Score

";// echo "score: $score
";// echo "screenShot: $screenshot
"; if (!empty($name) && !empty($score) && !empty($screenshot)) { // Move the file to the target upload folder $target = GW_UPLOADPATH . $screenshot; echo json_encode($_FILES); if (move_uploaded_file($_FILES['screenshot']['tmp_name'], $target)) { // Connect to the database$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die('Error Connecting to MySQL Database!');// Write the data to the database$query = "INSERT INTO guitarwars VALUES (0, NOW(), '$name', '$score','$screenshot')";mysqli_query($dbc, $query) or die('Error querying database;');// Confirm success with the userecho '

Thanks for adding your new high score!

';echo '

Name: ' . $name . '
';echo 'Score: ' . $score;echo '

';echo '

<< Back to high scores

';// Clear the score data to clear the form$name = "";$score = "";$screenshot = "";mysqli_close($dbc); } } else { echo '

Please enter all of the information to add your high score.

'; } }?>

在使用Zend Sutdio10.6調試上面這段PHP代碼時我發現“if (move_uploaded_file($_FILES['screenshot']['tmp_name'], $target)) {”這行代碼裡面的代碼塊沒有執行,查看了超全域變數$_FILES['screenshot']['tmp_name']的值為空白,然後我在這行代碼前以JSON格式列印出$_FILES變數的值,如下:

{"screenshot":{"name":"Penguins.jpg","type":"","tmp_name":"","error":2,"size":0}}

如下:


然後我查詢$_FILES["screenshot']['error']為2,上網查詢了一下,關於$_FILES超級全域變數大體如下:

PHP程式設計語言中的常見的$_FILES系統函數用法有:$_FILES['myFile']['name'] 顯示用戶端檔案的原名稱。$_FILES['myFile']['type'] 檔案的 MIME 類型,例如"image/gif"。$_FILES['myFile']['size'] 已上傳檔案的大小,單位為位元組。$_FILES['myFile']['tmp_name'] 儲存的臨時檔案名稱,一般是系統預設。$_FILES['myFile']['error'] 該檔案上傳相關的錯誤碼。以下為不同代碼代表的意思:0; 檔案上傳成功。1; 超過了檔案大小php.ini中即系統設定的大小。2; 超過了檔案大小MAX_FILE_SIZE 選項指定的值。3; 檔案只有部分被上傳。4; 沒有檔案被上傳。5; 上傳檔案大小為0。

另外,查詢PHP參考手冊關於move_uploaded_file函數的介紹如下:

move_uploaded_file(PHP 4 >= 4.0.3, PHP 5)move_uploaded_file — 將上傳的檔案移動到新位置說明bool move_uploaded_file ( string $filename , string $destination )本函數檢查並確保由 filename 指定的檔案是合法的上傳檔案(即通過 PHP 的 HTTP POST 上傳機制所上傳的)。如果檔案合法,則將其移動為由 destination 指定的檔案。 這種檢查顯得格外重要,如果上傳的檔案有可能會造成對使用者或本系統的其他使用者顯示其內容的話。 參數filename上傳的檔案的檔案名稱。 destination移動檔案到這個位置。 傳回值成功時返回 TRUE。 如果 filename 不是合法的上傳檔案,不會出現任何操作, move_uploaded_file() 將返回 FALSE。 如果 filename 是合法的上傳檔案,但出於某些原因無法移動,不會出現任何操作, move_uploaded_file() 將返回 FALSE。此外還會發出一條警告。 

範例

Example #1 Uploading multiple files

 $error) {    if ($error == UPLOAD_ERR_OK) {        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];        $name = $_FILES["pictures"]["name"][$key];        move_uploaded_file($tmp_name, "$uploads_dir/$name");    }}?> 
原因終於找到了,是因為我上傳了一個超過32768Bytes即32KB大小的Penguins.jpg檔案導致出現$_FILES['screenshot']['error']為2的錯誤,並且$_FILES['screenshot']['tmp_name']為空白,move_uploaded_file($_FILES['screenshot']['tmp_name'], $target)函數調用時返回FALSE,if (move_uploaded_file($_FILES['screenshot']['tmp_name'], $target)) {
...
}代碼塊沒有執行。


http://www.bkjia.com/PHPjc/847869.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/847869.htmlTechArticlePHP上傳檔案時無法上傳成功,$_FILES[#39;screenshot#39;][#39;tmp_name#39;]為空白 最近在學習《HeadFirst PHP MySQL》一書的第5章“使用儲存在檔案中的資料”...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.