【问题标题】:Insert form and multiple upload插入表单和多次上传
【发布时间】:2015-05-24 06:56:24
【问题描述】:

我想插入一个广告。广告包含标题、一些文本、价格并且可以有一个或多个图像。表格都在一页上。一个文件中的 php 和 html 也是如此。

在我的数据库中,我有一个 advertisement 表和一个 image 表。他们都有一个唯一的 id 作为主要的。

image 表还包含一个filename 和一个来自advertisement tableadvertisement_id,它是该表的外键。

问题:

  1. 如何上传多个文件?

  2. 我需要一次性插入所有内容还是先插入广告再插入图片?

<div class="form-group">
    <label for="image"  class="col-sm-2 control-label">Image:</label>
    <div class="col-sm-6">
        <input type="file" id="exampleInputFile" name="filename[]" id="filename" multiple">
    </div>
</div>

【问题讨论】:

    标签: php html mysql upload


    【解决方案1】:

    关于你的第二个问题,我认为插入广告然后插入所有图片会更明智。

    第一个问题,

    html 代码是这样的

        <form name="multiupload" action="uploadmany.php" method="post" enctype="multipart/form-data">
        <input name="filesToUpload[]" type="file" multiple /></td>
        <td collapse=2><input name="savemultiimg" type="submit" value="submit"></td>
        </form>
    

    这里是 php 代码。变量$err是上传状态的通知。

        <?php
    require("DataBaseConnection.php");
    if(isset($_POST["savemultiimg"])) {
        for($i=0;$i<count($_FILES['filesToUpload']['tmp_name']);$i++){
            $name=$_FILES['filesToUpload']['name'][$i];
            $tmp_name=$_FILES['filesToUpload']['tmp_name'][$i];
    
    
    $target_dir = "images/";  //the dir where you want to save images
    $target_file = $target_dir . rand().'_'.($name); //this will be the name of the img, and we will give it a random number so if you uploaded two image with same name you don't have a problem
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    // Check if image file is a actual image or fake image
        $check = getimagesize($tmp_name);
        if($check !== false) {
            //echo "File is an image - " . $check["mime"] . ".";
            $uploadOk = 1;
        } else {
            $err= "file you uploaded is not an image";
            $uploadOk = 0;
        }//end if
    
    // Check if file already exists,, this will appear if you removed the random number on the top
    if (file_exists($target_file)) {
        $err ="file already exists";
        $uploadOk = 0;
    }//end if
    
    if ($uploadOk != 0)
    {
    if (move_uploaded_file($tmp_name, $target_file)) {
        $err ="file uploaded succ.";
        $sql = "...";//your sql here
        $result = $con->query($sql);
    
    } else {
        $err ="unecpected error occured";
    }   //end if upload
    
        }//end ($uploadOk != 0)
    }//end forloop
    header('Location: index.php?err='.$err.');
    }
    ?>
    

    此代码来自我的一个大学项目,我刚刚对其进行了修改以适应您的需求。如果它给您带来任何错误,请告诉我,我很乐意为您提供帮助。

    源代码是从w3schools复制而来的,并针对多次上传进行了更改。

    【讨论】:

    • 我现在就试试,结果告诉你!
    • 我无法检查它是否工作。该页面没有显示任何页面中实现的代码。
    • 由于我的错误进行了一些更改后,效果很好!我只需要一个多重选择器。
    • 你的意思是像this这样的东西吗?
    • 是的,但我尝试了其中一些,但无法正确安装:(
    猜你喜欢
    • 2017-10-12
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多