【问题标题】:File upload is not working in combine php form but working separately文件上传不能以组合 php 形式工作,而是单独工作
【发布时间】:2017-07-21 10:36:41
【问题描述】:
<?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>

这是我的带有 html 表单代码的 php 示例,请建议我 当我分别使用文件和表单代码时,我能够上传文件 但不能一起工作。

显示错误如下

注意:未定义索引:t4 in F:\xampp\htdocs\new_travel\blog\admin\add-post.php 在第 48 行

注意:未定义索引:t4 in F:\xampp\htdocs\new_travel\blog\admin\add-post.php 在第 52 行

警告:getimagesize():文件名不能为空 F:\xampp\htdocs\new_travel\blog\admin\add-post.php 在第 52 行文件是 不是图像。注意:未定义索引:t4 in F:\xampp\htdocs\new_travel\blog\admin\add-post.php 在第 61 行

注意:未定义变量:imagefileype in F:\xampp\htdocs\new_travel\blog\admin\add-post.php 在第 68 行对不起, 只允许使用 jpg、jpeg、Png 和 gif 文件。

【问题讨论】:

标签: php html mysql forms file-upload


【解决方案1】:

除非您正确设置表单的内容类型,否则无法从 HTML 表单上传文件:

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

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

【讨论】:

  • @Drgeek 没问题。如果答案对您有所帮助,请记住将其标记为“已接受”答案 - 谢谢 :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-26
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多