PHP / MySQL多上传表格

时间:2015-07-26 20:35:25

标签: php forms upload

我是PHP的新手,我正在尝试创建一个包含多个上传字段和一个提交按钮的HTML / PHP表单。

我目前正在使用一个上传字段,将文件上传到目录,路径保存到mySQL表。

的index.php

<?php
include_once 'dbconfig.php';
?>

    <form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="file" />
    <button type="submit" name="btn-upload">upload</button>
    </form>

upload.php的

<?php
include_once 'dbconfig.php';
if(isset($_POST['btn-upload']))
{    

    $file = rand(1000,100000)."-".$_FILES['file']['name'];
    $file_loc = $_FILES['file']['tmp_name'];
    $file_size = $_FILES['file']['size'];
    $file_type = $_FILES['file']['type'];
    $folder="uploads/";

    // new file size in KB
    $new_size = $file_size/1024;  
    // new file size in KB

    // make file name in lower case
    $new_file_name = strtolower($file);
    // make file name in lower case

    $final_file=str_replace(' ','-',$new_file_name);

    if(move_uploaded_file($file_loc,$folder.$final_file))
    {
        $sql="INSERT INTO tbl_uploads(file,type,size) VALUES('$final_file','$file_type','$new_size')";
        mysql_query($sql);
        ?>
        <script>
        alert('successfully uploaded');
        window.location.href='index.php?success';
        </script>
        <?php
    }
    else
    {
        ?>
        <script>
        alert('error while uploading file');
        window.location.href='index.php?fail';
        </script>
        <?php
    }
}
?>

我一直在努力(到处搜索)要做的是拥有多个输入/上传字段。我还需要将新上传字段的路径放在新的mySQL列中。

类似:

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

        <input type="file" name="file" />
        <input type="file" name="file-two" />
        <input type="file" name="file-three" />

        <button type="submit" name="btn-upload">upload</button>
        </form>

如果有人能帮我解决这个问题,我将非常感激。我搜索了很多,否则我不会寻求帮助。谢谢!

2 个答案:

答案 0 :(得分:0)

想象一下,您要上传单个文件。上传后,您$_FILES变量将类似于:

array(1) {
  ["file"]=>
  array(5) {
    ["name"]=>
    string(12) "example.png"
    ["type"]=>
    string(9) "image/png"
    ["tmp_name"]=>
    string(24) "C:\php\tmp\php279.tmp"
    ["error"]=>
    int(0)
    ["size"]=>
    int(33007)
  }
}

现在,如果您在上传后尝试上传带有乘法上传按钮的文件(就像您提供的HTML一样),那么您的$_FILES vaiable就是这样的:

array(2) {
  ["file"]=>
  array(5) {
    ["name"]=>
    string(12) "example1.png"
    ["type"]=>
    string(9) "image/png"
    ["tmp_name"]=>
    string(25) "C:\php\tmp\php7C1F.tmp"
    ["error"]=>
    int(0)
    ["size"]=>
    int(33007)
  }
  ["file-two"]=>
  array(5) {
    ["name"]=>
    string(12) "example2.png"
    ["type"]=>
    string(9) "image/png"
    ["tmp_name"]=>
    string(25) "C:\php\tmp\php7C1F.tmp"
    ["error"]=>
    int(0)
    ["size"]=>
    int(33007)
  }
}

如你所见,这是一个相关的阵列。所以你只需要一个foreach循环来处理所有上传的文件。

所以你的代码就像这样:

<?php

if(isset($_POST['btn-upload']))
{    

    foreach ($_FILES as $file) {
        $file = rand(1000,100000)."-".$file['file']['name'];
        $file_loc = $file['file']['tmp_name'];
        $file_size = $file['file']['size'];
        $file_type = $file['file']['type'];
        $folder="uploads/";

        // new file size in KB
        $new_size = $file_size/1024;  
        // new file size in KB

        // make file name in lower case
        $new_file_name = strtolower($file);
        // make file name in lower case

        $final_file=str_replace(' ','-',$new_file_name);

        if(move_uploaded_file($file_loc,$folder.$final_file))
        {
            $sql="INSERT INTO tbl_uploads(file,type,size) VALUES('$final_file','$file_type','$new_size')";
            mysql_query($sql);
            ?>
            <script>
            alert('successfully uploaded');
            window.location.href='index.php?success';
            </script>
            <?php
        }
        else
        {
            ?>
            <script>
            alert('error while uploading file');
            window.location.href='index.php?fail';
            </script>
            <?php
        }

    }
}
?>

HTML代码没有变化:

<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="file" name="file-two" />
    <input type="file" name="file-three" />
    <button type="submit" name="btn-upload">upload</button>
</form>

答案 1 :(得分:0)

我假设你正在寻找一个多文件上传器。我将为您提供我用于我的项目的代码,然后根据您的要求进行更改。

<强>的index.php

<form id="add_theme_photos" action="" method="post" enctype="multipart/form-data">
      <h5>Add Files</h5>
     <input type="file" name="file[]" id="fileToUpload"  multiple="multiple"/>
    <button type="submit" name="btn-upload">Add Photos</button>
</form>

<强> upload.php的

if (isset($_POST['btn-upload'])) {
include 'connect.php'; <-- file connecting to sql
$errors = array();
foreach($_FILES['file']['tmp_name'] as $key => $tmp_name ){
    $file_name = $key.$_FILES['file']['name'][$key];
    $file_size =$_FILES['file']['size'][$key];
    $file_tmp =$_FILES['file']['tmp_name'][$key];
    $file_type=$_FILES['file']['type'][$key];   
    if($file_size > 2097152){
        $errors[]='File size must be less than 2 MB';
    }

     // new file size in KB
     $new_size = $file_size/1024;  
     // new file size in KB

    $imageURL = "media/images/".$file_name;
    $stmt = $conn->prepare("INSERT INTO tbl_uploads(file,type,size) VALUES(?, ?, ?)";
    $stmt->bind_param('sss',  $final_file','$file_type','$new_size);
    $desired_dir="../media/images";
    if (empty($errors) == true) {
        if (is_dir($desired_dir) == false) {
            mkdir($desired_dir, 0700);
        }
        if (is_dir($desired_dir."/".$file_name)==false) {
            move_uploaded_file($file_tmp,"../media/images/".$file_name);
        } else {
            $new_dir-"media/images/".$file_name.time();
        }
        $stmt->execute();
        $stmt->close();
    } else {
        print_r($errors);
    }
    if (empty($error)) {
        echo "";
    }
}
}
  

注意:   文件保存在:$ desired_dir =&#34; files / saved / here&#34 ;;   $ conn - &gt;连接数据库

这是关于主题http://techstream.org/Web-Development/PHP/Multiple-File-Upload-with-PHP-and-MySQL的教程。 :)