標籤:des style blog http color 使用
最近在學習《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代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Guitar Wars - Add Your High Score</title> <link rel="stylesheet" type="text/css" href="style.css" /></head><body> <h2>Guitar Wars - Add Your High Score</h2><?php require_once 'appvars.php'; require_once 'connectvars.php'; if (isset($_POST['submit'])) { // Grab the score data from the POST $name = $_POST['name']; $score = $_POST['score']; $screenshot = $_FILES['screenshot']['name']; // echo "name: $name <br />";// echo "score: $score <br />";// echo "screenShot: $screenshot <br />"; 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 '<p>Thanks for adding your new high score!</p>';echo '<p><strong>Name:</strong> ' . $name . '<br />';echo '<strong>Score:</strong> ' . $score;echo '<img src="' . GW_UPLOADPATH . $screenshot . '" alt="Score image" /></p>';echo '<p><a href="index.php"><< Back to high scores</a></p>';// Clear the score data to clear the form$name = "";$score = "";$screenshot = "";mysqli_close($dbc); } } else { echo '<p class="error">Please enter all of the information to add your high score.</p>'; } }?> <hr /> <form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="hidden" name="MAX_FILE_SIZE" value="32768" /> <label for="name">Name:</label> <input type="text" id="name" name="name" value="<?php if (!empty($name)) echo $name; ?>" /><br /> <label for="score">Score:</label> <input type="text" id="score" name="score" value="<?php if (!empty($score)) echo $score; ?>" /><br /> <label for="screeshot">ScreenShot</label> <input type="file" id="screenshot" name="screenshot" /> <hr /> <input type="submit" value="Add" name="submit" /> </form></body> </html>
在使用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
<?php$uploads_dir = '/uploads';foreach ($_FILES["pictures"]["error"] as $key => $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)) {
...
}代碼塊沒有執行。