不得上传图片文件。我正在学习如何建立一个博客,所以我开始简单地获得基本的机制。在一个页面上创建文章的表单,用于处理表单和报告任何错误的页面,以及用于查看所有文章的简短描述的另一个页面。还有一个用于查看上传图像的文件和另一个用于查看整篇文章的文件,但这里不需要它们。所有文章数据都会转到MySQL数据库中的一个表中。
每当我上传文章时,我都会在处理页面上收到此消息:"在上传的数据中找不到图像" 我可以删除!从这个:if(!array_key_exists(' image',$ _FILES)){throw new Exception('在上传的数据中找不到图像'); } ...在处理文件中获取"文件不是上传的文件" ......听起来很相似。连接不是问题。我将粘贴到相关文件和MySQL表中。
这是表单文件:
<?php
session_start(); #redirect
if (isset($_SESSION['usr_id']) && !empty($_SESSION['usr_id']) ) {
} else {
header('Location: notloggedin.php'); #redirect URL
}
?>
<?php include('header.php'); ?>
<div class="container blog-container"><!-- Begin Blog Container-->
<div class="row"><!-- Begin Blog Row -->
<div class="col-lg-9 col-sm-12 blog-main-content"><!-- Begin Blog Content Div (Left Column) -->
<div class="blog-header"><!-- Begin Blog Content Header -->
<p><g16 class="blog-header-text">THE DATABASE DRIVEN BLOG BY ROBERT</g16></p>
</div><!-- End Blog Content Header -->
<div class="blog-content"><!-- Begin - Main blog content in this div -->
<br><br>
<p align="center">
<form metdod="post" action="article_process.php" enctype="multipart/form-data" name="form1">
<table widtd="100%" border="0" ><!-- TABLE TD'S AND TEXT REFUSE TO OBEY CSS!!! -->
<tr>
<td align="right" valign="top" width="30%" style="padding-left: 15px; padding-right: 10px; padding-top: 10px;"><g16 style="text-align: right; line-height: 80%; font-size: 16pt; color: black;">Title:</g16></td>
<td><g14 style="font-size: 12pt;"><input name="title" id="title" type="text" size="60" maxlengtd="75" /></g14></td>
</tr>
<tr>
<td align="right" valign="top" style="padding-left: 15px; padding-right: 10px; padding-top: 10px;"><g16 style="text-align: right; line-height: 80%; font-size: 16pt; color: black;">Date:</g16></td>
<td style=" padding-top: 5px;"><g14 style="font-size: 12pt;"><input type="text" disabled="disabled" value=<?php echo date("Y-m-d"); ?> name="cdate" id="cdate" /></g14></td>
</tr>
<tr>
<td align="right" valign="top" style="padding-left: 15px; padding-right: 10px; padding-top: 10px;"><g16 style="text-align: right; line-height: 80%; font-size: 16pt; color: black;">Author:</g16></td>
<td style=" padding-top: 5px;"><g14 style="font-size: 12pt;"><input type="text" disabled="disabled" value=<?php echo $_SESSION['usr_id']; ?> name="author" id="author" /></g14></td>
</tr>
<tr>
<td align="right" valign="top" style="padding-left: 15px; padding-right: 10px; padding-top: 10px;"><g16 style="text-align: right; line-height: 80%; font-size: 16pt; color: black;">Categories:</g16></td>
<td style=" padding-top: 5px;"><g14 style="font-size: 12pt;"><label for="category"></label>
<select name="category" id="category">
<option value="Corruption"><g14>Corruption</g14></option>
<option value="Capitalism"><g14>Capitalism</g14></option>
<option value="Fascism"><g14>Fascism</g14></option>
</select></g14></td>
</tr>
<tr>
<td align="right" valign="top" style="padding-left: 15px; padding-right: 10px; padding-top: 10px;"><g16 style="text-align: right; line-height: 80%; font-size: 16pt; color: black;">Short Description:</g16></td>
<td style=" padding-top: 5px;"><g12 style="font-size: 12pt;"><input name="desc" type="text" id="desc" size="60" maxlengtd="200" /></g12></td>
</tr>
<tr>
<td align="right" valign="top" style="padding-left: 15px; padding-right: 10px; padding-top: 10px;"><g16 style="text-align: right; line-height: 80%; font-size: 16pt; color: black;">Article:</g16></td>
<td style=" padding-top: 5px;"><g12 style="font-size: 12pt;"><textarea name="article" cols="59" rows="20"></textarea></g12></td>
</tr>
<tr>
<td align="right" valign="top" style="padding-left: 15px; padding-right: 10px; padding-top: 10px;"><g16 style="text-align: right; line-height: 80%; font-size: 16pt; color: black;">Upload File:</g16></td>
<td style=" padding-top: 5px;"><g14 style="font-size: 12pt;"><input type="file" name="image" /></g14></td>
</tr>
<tr>
<td align="right" valign="top" style="padding-left: 15px; padding-right: 10px; padding-top: 10px;"></td>
<td style=" padding-top: 5px;"><g14 style="font-size: 12pt;"><input name="submit" type="submit" value="Upload the Article" /><g14><input name="reset" type="reset" value="Reset" /></g14></td>
</tr>
</table>
</form>
</p>
<br><br>
</div><!-- End - Main blog content in this div -->
<div class="blog-header" style="vertical-align: middle; padding-bottom: 1px;"><!-- Begin Blog Content Footer -->
<p><g16 class="blog-header-text"><hr></g16></p>
</div><!-- End Blog Content Footer -->
</div><!-- End Blog Content Div (Left Column) -->
<!-- End of Body -->
<?php include('side-comments.php'); ?>
<?php include('footer.php'); ?>
这是处理表单的文件,article-process.php:
<?php
session_start();
?>
<?php include('header.php'); ?>
<?php
require_once('dbconnect.php');
function assertValidUpload($code)
{
if ($code == UPLOAD_ERR_OK) {
return;
}
switch ($code) {
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
$msg = 'Image is too large';
break;
case UPLOAD_ERR_PARTIAL:
$msg = 'Image was only partially uploaded';
break;
case UPLOAD_ERR_NO_FILE:
$msg = 'No image was uploaded';
break;
case UPLOAD_ERR_NO_TMP_DIR:
$msg = 'Upload folder not found';
break;
case UPLOAD_ERR_CANT_WRITE:
$msg = 'Unable to write uploaded file';
break;
case UPLOAD_ERR_EXTENSION:
$msg = 'Upload failed due to extension';
break;
default:
$msg = 'Unknown error';
}
throw new Exception($msg);
}
$errors = array();
try {
if (!array_key_exists('image', $_FILES)) {
throw new Exception('Image not found in uploaded data');
}
$image = $_FILES['image'];
// ensure the file was successfully uploaded
assertValidUpload($image['error']);
if (!is_uploaded_file($image['tmp_name'])) {
throw new Exception('File is not an uploaded file');
}
$info = getImageSize($image['tmp_name']);
if (!$info) {
throw new Exception('File is not an image');
}
}
catch (Exception $ex) {
$errors[] = $ex->getMessage();
}
if (count($errors) == 0) {
// no errors, so insert the image
$today = date("Y-m-d");
$story = mysql_real_escape_string($_POST['article']);
$query = sprintf(
"insert into article (filename, mime_type, file_size, file_data, title, curr_date, author, description, category, story)
values ('%s', '%s', %d, '%s', '$_POST[title]','$today', '$_POST[author]', '$_POST[desc]', '$_POST[category]', '$story')",
mysql_real_escape_string($image['name']),
mysql_real_escape_string($info['mime']),
$image['size'],
mysql_real_escape_string(
file_get_contents($image['tmp_name'])));
mysql_query($query, $conn);
$id = (int) mysql_insert_id($conn);
// finally, redirect the user to view the new image
header('Location: thanks-article.php');
exit;
}
?>
<div class="container blog-container"><!-- Begin Blog Container-->
<div class="row"><!-- Begin Blog Row -->
<div class="col-lg-9 col-sm-12 blog-main-content"><!-- Begin Blog Content Div (Left Column) -->
<div class="blog-header"><!-- Begin Blog Content Header -->
<p><g16 class="blog-header-text">THE DATABASE DRIVEN BLOG BY ROBERT</g16></p>
</div><!-- End Blog Content Header -->
<div class="blog-content blog-content-message"><!-- Begin - Main blog content in this div -->
<div style="width: 60%; margin-left: auto; margin-right: auto; padding: 15px;"><!-- Begin - Contains Error Messages -->
<p><g16 style="font-size: 18pt;">
The following errors occurred:
</g16></p>
<g14 style="font-size: 14pt; color: #cc0000;"><?php foreach ($errors as $error) { ?>
<ul>
<li>
<?php echo htmlSpecialChars($error) ?>
</li>
<?php } ?>
</ul></g14>
<p><g14 style="font-size: 14pt;">
<a href="index-blog.php">Try again</a>
</g14></p>
</div><!-- End - Contains Error Messages -->
<div style="width: 40%; margin-left: auto; margin-right: auto; padding: 15px;">
<table>
<tr>
<td width="10" align="center" valign="middle">
<a href="index-blog.php"><img src='img/c-02.png' onmouseover="this.src='img/c-02-invert.png';" onmouseout="this.src='img/c-02.png';" /></a>
</td>
</tr>
</table>
</div>
</div><!-- End - Main blog content in this div -->
<div class="blog-header" style="vertical-align: middle; padding-bottom: 1px;"><!-- Begin Blog Content Footer -->
<p><g16 class="blog-header-text"><hr></g16></p>
</div><!-- End Blog Content Footer -->
</div><!-- End Blog Content Div (Left Column) -->
<!-- End of Body -->
<?php include('side-comments.php'); ?>
<?php include('footer.php'); ?>
如果文章成功提交,请使用此页面,thanks-article.php:
<?php
session_start();
?>
<?php include('header.php'); ?>
<div class="container blog-container"><!-- Begin Blog Container-->
<div class="row"><!-- Begin Blog Row -->
<div class="col-lg-9 col-sm-12 blog-main-content"><!-- Begin Blog Content Div (Left Column) -->
<div class="blog-header"><!-- Begin Blog Content Header -->
<p><g16 class="blog-header-text">THE DATABASE DRIVEN BLOG BY ROBERT</g16></p>
</div><!-- End Blog Content Header -->
<div class="blog-content blog-content-message"><!-- Begin - Main blog content in this div -->
<p><br></p>
<p style="text-align: center;"><g18 style="font-size: 32pt; color: #004d00; text-shadow: 0 0 8px #ffff80, 0 0 16px #b3ff66;">YOU CREATED AN ARTICLE!</g18><br></p>
<p style="text-align: center;">Click <a href="view.php">here</a> to view the image you uploaded.</p>
<p style="text-align: center;">Click <a href="article.php">here</a> to view your article.</p>
<p style="text-align: center;">Click <a href="index-blog-view.php">here</a> to view the article list.</p>
<div style="width: 40%; margin-left: auto; margin-right: auto; padding: 15px;">
<table>
<tr>
<td width="auto" align="center" valign="middle">
<img src='img/c-08.png' />
</td>
</tr>
</table>
</div>
</div><!-- End - Main blog content in this div -->
<div class="blog-header" style="vertical-align: middle; padding-bottom: 1px;"><!-- Begin Blog Content Footer -->
<p><g16 class="blog-header-text"><hr></g16></p>
</div><!-- End Blog Content Footer -->
</div><!-- End Blog Content Div (Left Column) -->
<!-- End of Body -->
<?php include('side-comments.php'); ?>
<?php include('footer.php'); ?>
这是数据库中的表格:
CREATE TABLE `article` (
`article_id` bigint(20) NOT NULL,
`filename` varchar(255) NOT NULL,
`mime_type` varchar(255) NOT NULL,
`file_size` int(11) NOT NULL,
`file_data` longblob NOT NULL,
`title` varchar(75) NOT NULL,
`curr_date` varchar(20) NOT NULL,
`author` varchar(50) NOT NULL,
`description` varchar(200) NOT NULL,
`category` varchar(25) NOT NULL,
`story` longblob NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
我可以补充一点,当我把它放在远程服务器上时,我得到了#34; 404 - 找不到文件或目录。&#34;与所有相同的文件和表...不同的连接脚本。 谢谢你的帮助!