Fileupload不能在php中工作而不能在数据库

时间:2016-02-25 09:47:30

标签: php html database

我是uploadig文件到特定目录并在数据库中保存文件的名称。

我的表格的html代码:

<form style='margin:15px;' method='post' action='commentheandler.php'>
    <textarea name='commentsub' rows='3' cols='90' required></textarea><br />
    <br/><input type='file' name='attachfile' /><br />";
    <br/><input type='hidden' name='cmember_id' value='" . $id . "'>
    <input type='hidden' name='tsreid' value='" . $id_t . "'>
    <input type='submit' name='comment' value='comment'>
</form>

commentheandler.php

<?php
    include "lib/db.php";
    if (isset($_POST['comment']))
    {
        $id_t = $_POST['tsreid'];
        $comment = $_POST['commentsub'];
        $created_id = $_POST['cmember_id'];

        $time = date("Y-m-d h:i:sa");

        $target_dir = "logoimages/task/";
        $target_file = $target_dir . basename($_FILES["attachfile"]["name"]);
        $uploadOk = 1;
        $FileType = pathinfo($target_file,PATHINFO_EXTENSION);

        if (move_uploaded_file($_FILES["attachfile"]["tmp_name"], $target_file)) {
        } else {
            echo "Sorry, there was an error uploading your file.";
        }

        $file = basename( $_FILES["attachfile"]["name"]); // used to store the filename in a variable

        ($file!=null)
        {   
           $insert=array("taskid"=>$id_t,"comment"=>$comment,"attachfile"=>$file,"createdby"=>$created_id,"created"=>$time,"isdelete"=>0);//storing the data in database
            $db->insert("taskcomment",$insert);
            $loc="location: taskhandler.php?id=" . $id_t . "";
            header($loc);
        }
        else {
            $insert=array("taskid"=>$id_t,"comment"=>$comment,"createdby"=>$created_id,"created"=>$time,"isdelete"=>0);

            $db->insert("taskcomment",$insert);
            $loc="location: taskhandler.php?id=" . $id_t . "";
            header($loc);
        }
    }
?>

这不起作用我无法在database.need建议中获得任何名称。提前谢谢。

哦,我通过添加enctype =“multipart / form-data”来形成,但是我知道如何限制文件大小。?

2 个答案:

答案 0 :(得分:2)

您在表单中缺少enctype="multipart/form-data"

表格应该是,

 <form style='margin:15px;' method='post' action='commentheandler.php' enctype="multipart/form-data">

答案 1 :(得分:0)

enctype='multipart/form-data是一种允许通过POST发送文件的编码类型。很简单,没有这种编码,文件无法通过POST发送。如果要允许用户通过表单上载文件,则必须使用此enctype。所以请在表单标签中定义enctype,如

<form method='post' action='commentheandler.php' enctype="multipart/form-data">