<?php //include config
require_once('../includes/config.php');
include("function.php");
//if not logged in redirect to login page
if(!$user->is_logged_in()){ header('Location: login.php'); }
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Admin - Add Post</title>
<link rel="stylesheet" href="../style/normalize.css">
<link rel="stylesheet" href="../style/main.css">
<script src="//tinymce.cachefly.net/4.0/tinymce.min.js"></script>
<script>
tinymce.init({
selector: "textarea",
plugins: [
"advlist autolink lists link image charmap print preview anchor",
"searchreplace visualblocks code fullscreen",
"insertdatetime media table contextmenu paste"
],
toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
});
</script>
</head>
<body>
<div id="wrapper">
<?php include('menu.php');?>
<p><a href="./">Blog Admin Index</a></p>
<h2>Add Post</h2>
<?php
if(isset($_POST['submit']))
{
$cn=makeconnection();
$target_dir = "subcatimages/";
$target_file = $target_dir.basename($_FILES["t4"]["name"]);
$uploadok = 1;
$imagefiletype = pathinfo($target_file, PATHINFO_EXTENSION);
//check if image file is a actual image or fake image
$check=getimagesize($_FILES["t4"]["tmp_name"]);
if($check!==false) {
echo "file is an image - ". $check["mime"]. ".";
$uploadok = 1;
}else{
echo "file is not an image.";
$uploadok=0;
}
//check file size
if($_FILES["t4"]["size"]>5000000){
echo "sorry, your file is too large.";
$uploadok=0;
}
//aloow certain file formats
if($imagefiletype != "jpg" && $imagefiletype !="png" && $imagefiletype !="jpeg" && $imagefileype !="gif"){
echo "sorry, only jpg, jpeg, Png & gif files are allowed.";
$uploadok=1;
}else{
if(move_uploaded_file($_FILES["t4"]["tmp_name"], $target_file)){
$s="insert into picture(Filename)values('" . basename($_FILES["t4"]["name"]) . "')";
mysqli_query($cn,$s);
echo "<script>alert('Record Save');</script>";
} else{
echo "sorry there was an error uploading your file.";
}}
//if form has been submitted process it
//collect form data
extract($_POST);
//very basic validation
if($postTitle ==''){
$error[] = 'Please enter the title.';
}
if($postDesc ==''){
$error[] = 'Please enter the description.';
}
if($postCont ==''){
$error[] = 'Please enter the content.';
}
if(!isset($error)){
try {
$postSlug = slug($postTitle);
//insert into database
$stmt = $db->prepare('INSERT INTO blog_posts_seo (postTitle,postSlug,postDesc,postCont,postDate) VALUES (:postTitle, :postSlug, :postDesc, :postCont, :postDate)') ;
$stmt->execute(array(
':postTitle' => $postTitle,
':postSlug' => $postSlug,
':postDesc' => $postDesc,
':postCont' => $postCont,
':postDate' => date('Y-m-d H:i:s'),
));
$postID = $db->lastInsertId();
//add categories
if(is_array($catID)){
foreach($_POST['catID'] as $catID){
$stmt = $db->prepare('INSERT INTO blog_post_cats (postID,catID)VALUES(:postID,:catID)');
$stmt->execute(array(
':postID' => $postID,
':catID' => $catID
));
}
}
//redirect to index page
//header('Location: index.php?action=added');
//exit;
} catch(PDOException $e) {
echo $e->getMessage();
}
}
}
//check for any errors
if(isset($error)){
foreach($error as $error){
echo '<p class="error">'.$error.'</p>';
}
}
?>
<form action="" method="post">
<p><label>Title</label><br />
<input type='text' name='postTitle' value='<?php if(isset($error)){ echo $_POST['postTitle'];}?>'></p>
<input type='file' name='t4'>
<p><label>Description</label><br />
<textarea name='postDesc' cols='60' rows='10'><?php if(isset($error)){ echo $_POST['postDesc'];}?></textarea></p>
<p><label>Content</label><br />
<textarea name='postCont' cols='60' rows='10'><?php if(isset($error)){ echo $_POST['postCont'];}?></textarea></p>
<fieldset>
<legend>Categories</legend>
<?php
$stmt2 = $db->query('SELECT catID, catTitle FROM blog_cats ORDER BY catTitle');
while($row2 = $stmt2->fetch()){
if(isset($_POST['catID'])){
if(in_array($row2['catID'], $_POST['catID'])){
$checked="checked='checked'";
}else{
$checked = null;
}
}
echo "<input type='checkbox' name='catID[]' value='".$row2['catID']."' > ".$row2['catTitle']."<br />";
}
?>
</fieldset>
<p><input type='submit' name='submit' value='Submit'></p>
</form>
</div>
这是我的PHP样本与html表单代码请建议我 当我单独使用文件和表单代码时,我能够上传文件 但不能一起工作。
显示错误如下
注意:未定义的索引:t4 in 第48行的F:\ xampp \ htdocs \ new_travel \ blog \ admin \ add-post.php
注意:未定义的索引:t4 in 第52行的F:\ xampp \ htdocs \ new_travel \ blog \ admin \ add-post.php
警告:getimagesize():文件名不能为空 第52行文件中的F:\ xampp \ htdocs \ new_travel \ blog \ admin \ add-post.php是 不是图像。注意:未定义的索引:t4 in 第61行的F:\ xampp \ htdocs \ new_travel \ blog \ admin \ add-post.php
注意:未定义的变量:imagefileype in 第68行的F:\ xampp \ htdocs \ new_travel \ blog \ admin \ add-post.php抱歉, 只有jpg,jpeg,Png&amp;允许使用gif文件。
答案 0 :(得分:0)
除非您正确设置表单的内容类型,否则从HTML表单上传文件将无效:
<form action="" method="post" enctype="multipart/form-data">
如果没有这个,文件数据将不会传输到服务器。