标签: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 user
echo '<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>
{"screenshot":{"name":"Penguins.jpg","type":"","tmp_name":"","error":2,"size":0}}
截图如下:
然后我查询$_FILES[‘screenshot‘][‘error‘]为2,上网查询了一下,关于$_FILES超级全局变量大体如下:
另外,查询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)) {
PHP上传文件时无法上传成功,$_FILES['screenshot']['tmp_name']为空,布布扣,bubuko.com
PHP上传文件时无法上传成功,$_FILES['screenshot']['tmp_name']为空
标签:des style blog http color 使用
原文地址:http://blog.csdn.net/ccf19881030/article/details/37580725