将表单中的多个文件上传到服务器PHP并将信息放入Mysql数据库

时间:2015-08-06 04:14:59

标签: php mysql

将一首歌上传到服务器,该服务器还使用php form和mysql将数据注入数据库。这适用于一个文件,但是要上传3个单独的文件。目标是将每个文件上传到服务器并将数据发布到mysql中。当我尝试上传时,它给了我错误。我相信这与我如何添加多个文件有关。以下是我到目前为止的情况:

编辑:我已经搞砸了一些,这就是我得到的,问题在于有多个move_uploaded_files。我正在阅读你可以做循环数组的地方,但我将theese添加到一个mys

上传页面表单

  <form enctype="multipart/form-data" action="add.php" method="POST"> 
Name: <input type="text" name="name"><br>  
B.P.M.: <input type="text" name = "bpm"><br>  
Length: <input type="text" name = "length"><br> 
Keywords: <input type="text" name = "keywords"><br>  
Tagged MP3: <input type="file" name="downloadlink"><br>  
MP3: <input type="file" name="mp3link"><br>  
WAV: <input type="file" name="wavlink"><br>  
<input type="submit" value="Add">  
</form>

add.php(第二页)

    <?php 

 //This is the directory where images will be saved 
$uploads_dir = 'beats/';
foreach ($_FILES[""]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES[""]["tmp_name"][$key];
        $name = $_FILES[""]["name"][$key];
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
    }
}

 //This gets all the other information from the form 
 $name=$_POST['name']; 
 $bpm=$_POST['bpm'];
 $length=$_POST['length'];
 $keywords=$_POST['keywords']; 
 $downloadlink=($_FILES['downloadlink']['name']); 
 $mp3link=($_FILES['mp3link']['name']); 
 $wavlink=($_FILES['wavlink']['name']); 



 // Connects to your Database 
 mysql_connect("localhost", "", "") or die(mysql_error()) ; 
 mysql_select_db("exclusive") or die(mysql_error()) ; 


//Writes the information to the database 
 $date = date('m-d-Y');
 mysql_query("INSERT INTO exclusive VALUES ('', '$date', '$name', '$bpm', '$length', '$keywords', '$downloadlink', '$mp3link', '$wavlink')") ; 

 ?>

1 个答案:

答案 0 :(得分:0)

当您将多个文件上传到数据库时,需要注意的一件事是确保将php.ini文件的upload_max_filesize和post_max_size设置为处理足够大的数量。这不起作用的一个原因是因为我的设置很小。

这是工作代码:

表格(whateveryouwantupload.html)

<form action="addmulti.php" method="post" enctype="multipart/form-data">
  <p>Name: <input type="text" name="name"></p> 
<p>B.P.M.: <input type="text" name = "bpm"></p>
<p>Length: <input type="text" name = "length"></p> 
<p>Keywords: <input type="text" name = "keywords"></p>
  <p>Tagged MP3: <input type="file" name="file_array[]"></p>
  <p>MP3: <input type="file" name="file_array[]"></p>
  <p>WAV: <input type="file" name="file_array[]"></p>
  <input type="submit" value="Upload all files">
</form>

PHP(addmulti.php)

<?php
// Multiple Upload Files
if(isset($_FILES['file_array'])){
    $name_array = $_FILES['file_array']['name'];
    $tmp_name_array = $_FILES['file_array']['tmp_name'];
    $type_array = $_FILES['file_array']['type'];
    $size_array = $_FILES['file_array']['size'];
    $error_array = $_FILES['file_array']['error'];

//Get Form Information 
 $name=$_POST['name']; 
 $bpm=$_POST['bpm'];
 $length=$_POST['length'];
 $keywords=$_POST['keywords']; 
 $downloadlink=$name_array[0]; 
 $mp3link=$name_array[1]; 
 $wavlink=$name_array[2]; 

 // Connect to Database 
 mysql_connect("localhost", "user", "pass") or die(mysql_error()) ; 
 mysql_select_db("exclusive") or die(mysql_error()) ; 

//Writes the information to the database 
            $date = date('m-d-Y');
            mysql_query("INSERT INTO table VALUES ('', '$date', '$name', '$bpm', '$length', '$keywords', '$downloadlink', '$mp3link', '$wavlink')") ;

//Writes file to server
for($i = 0; $i < count($tmp_name_array); $i++){     
        if(move_uploaded_file($tmp_name_array[$i], "testupload/".$name_array[$i])){

            echo $name_array[$i]." upload is complete<br>";
        } else {
            echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
        }
    }
}
?>

<强>的php.ini

max_execution_time = 300
file_uploads = On
post_max_size = 128M
upload_max_filesize = 128M

感谢您的帮助!