最近在学习《HeadFirst PHP & MySQL》一书的第5章“使用存储在文件中的数据”,做一个文件上传的应用时,出现了错误,就是文件无法成功上传,
PHP上传文件时无法上传成功,$FILES[screenshot][
。这个问题困扰了我很久,不过还好最后终于解决了。原因是我上传的图片文件大小超过了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代码如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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 user
echo '
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.
';}
}
?>