gpt4 book ai didi

php - 文件上传无法以组合 php 形式工作,而是单独工作

转载 作者:行者123 更新时间:2023-11-29 18:36:39 25 4
gpt4 key购买 nike

<?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 表单代码,请建议我当我分别使用文件和表单代码时,我能够上传文件但不一起工作。

显示错误如下

Notice: Undefined index: t4 in F:\xampp\htdocs\new_travel\blog\admin\add-post.php on line 48

Notice: Undefined index: t4 in F:\xampp\htdocs\new_travel\blog\admin\add-post.php on line 52

Warning: getimagesize(): Filename cannot be empty in F:\xampp\htdocs\new_travel\blog\admin\add-post.php on line 52 file is not an image. Notice: Undefined index: t4 in F:\xampp\htdocs\new_travel\blog\admin\add-post.php on line 61

Notice: Undefined variable: imagefileype in F:\xampp\htdocs\new_travel\blog\admin\add-post.php on line 68 sorry, only jpg, jpeg, Png & gif files are allowed.

最佳答案

除非您正确设置表单的内容类型,否则从 HTML 表单上传文件将不起作用:

<form action="" method="post" enctype="multipart/form-data">

如果没有这个,文件数据将不会传输到服务器。

关于php - 文件上传无法以组合 php 形式工作,而是单独工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45235482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com