上传后将3张图片放入数据库

时间:2014-10-23 17:35:34

标签: php database mysqli upload insert

PHP上传看起来像这样

<?php
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'];
    for($i = 0; $i < count($tmp_name_array); $i++){
        if(move_uploaded_file($tmp_name_array[$i], "videoCover/".$name_array[$i])){
            echo $name_array[$i]." upload is complete<br>";
        } else {
            echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
        }   
    }

                        include '../connect/con.php';

$id = mysqli_real_escape_string($con, $_POST['id']);
$vidLD = mysqli_real_escape_string($con, $_POST['vidLD']);
$vidYear = mysqli_real_escape_string($con, $_POST['vidYear']);
$vidCity = mysqli_real_escape_string($con, $_POST['vidCity']);
$vidZanr = mysqli_real_escape_string($con, $_POST['vidZanr']);
$vidQuality = mysqli_real_escape_string($con, $_POST['vidQuality']);
$vidTranslated = mysqli_real_escape_string($con, $_POST['vidTranslated']);
$vidMaker = mysqli_real_escape_string($con, $_POST['vidMaker']);
$vidRoles = mysqli_real_escape_string($con, $_POST['vidRoles']);
$vidTime = mysqli_real_escape_string($con, $_POST['vidTime']);
$imgShot1 = PICTURE1;
$imgShot2 = PICTURE2;
$imgShot3 = PICTURE1;

$sql="INSERT INTO videoinformation (id, vidLD, vidYear,  vidCity, vidZanr, vidQuality, vidTranslated, vidTime, vidMaker, vidRoles, imgShot1, imgShot2, imgShot3) VALUES ('$id', '$vidLD', '$vidYear', '$vidCity', '$vidZanr', '$vidQuality', '$vidTranslated', '$vidMaker', '$vidRoles', '$imgShot1', '$imgShot2', '$imgShot3')";

if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}echo "Video Description are added";

mysqli_close($con);
}
?>

这是我的表格:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form action="../upload/uploadLD.php" method="post" enctype="multipart/form-data">


 <table style="width:500px, margin-left:50px">  
        <tr><td>Video ID: </td>     
<td><select id="id" name="id">

            <?php

            include '../connect/con.php';

        $con = mysqli_connect("$db_host", "$db_username", "$db_pass", "$db_name");

        mysqli_select_db($db_name, $con);
        if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); }

        $cdquery="SELECT * FROM newsvid";
        $cdresult = mysqli_query($con,$cdquery);

        while ($cdrow=mysqli_fetch_array($cdresult)) {
        $cdTitle=$cdrow["id"];
            echo "<option>
                $cdTitle
            </option>"; } 

            mysqli_close($con);?>

 </select>
</td></tr>


  <tr><td>Video LD</td><td><input name="vidLD"></td></tr>
  <tr><td>Year:</td><td><input name="vidYear"></td></tr>
  <tr><td>City:</td><td><input name="vidCity"></td></tr>
  <tr><td>Zanr:</td><td><input name="vidZanr"></td></tr>
  <tr><td>Quality:</td><td><input name="vidQuality"></td></tr>
  <tr><td>Translated:</td><td><input name="vidTranslated"></td></tr>
  <tr><td>Time:</td><td><input name="vidTime"></td></tr>
  <tr><td>Maker:</td><td><input name="vidMaker"></td></tr>
  <tr><td>Roles:</td><td><input name="vidRoles"></td></tr>  
</table>    





  <p><input type="file" name="file_array[]"></p>
  <p><input type="file" name="file_array[]"></p>
  <p><input type="file" name="file_array[]"></p>
  <input type="submit" value="Upload all files">

</form>
</body>
</html>

问题是:在上传脚本

中我必须代替PICTURES1-2-3
$imgShot1 = PICTURE1;
$imgShot2 = PICTURE2;
$imgShot3 = PICTURE1;

获取上传的文件名,将其插入数据库后????

更新后的shoul看起来像这样???

$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'];
for($i = 0; $i < count($tmp_name_array); $i++){
    if(move_uploaded_file($tmp_name_array[$i], "videoCover/".$name_array[$i])){
        echo $name_array[$i]." upload is complete<br>";
        $imgShot[$i]=base64_encode(file_get_contents('videoCover/'.$name_array[$i]));
        $sql="INSERT INTO videoinformation '".$imgShot[0]."','".$imgShot[1]."','".$imgShot[2];
    } else {
        echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
    }   
}

............................................... .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. ...................................

2 个答案:

答案 0 :(得分:1)

假设我理解正确(将图像存储在数据库而不是路径中),你可以这样做:

在“for if if moved”中:

$imgShot[$i]=base64_encode(file_get_contents('videoCover/'.$name_arrray[$i]));

这将读取文件的内容,base64对其进行编码,使其准备好运输并将其存放在$ imgShot变量(现在是一个数组)中。

然后你的insert语句如下所示:

$sql="insert into [...] '".$imgShot[0]."','".$imgShot[1]."','".$imgShot[2]."' [...]";

然后,您可以使用数据库中的图像作为base64_encoded解释,如:

<img src="data:image/jpeg;base64,'.$img_data_from_db.'" /&GT;

或php.net/base64_decode它,将其写入新的图像名称,然后使用它。

请记住,base64_encode会增加保存图像php.net/base64_encode所需的大小。

希望这有帮助。

编辑:另外,你会删除/注释掉这个:

$imgShot1 = PICTURE1;
$imgShot2 = PICTURE2;
$imgShot3 = PICTURE1;

编辑2: 不应该在评论中放置查询,这里是......

$sql="INSERT INTO videoinformation (id, vidLD, vidYear, vidCity, vidZanr, vidQuality, vidTranslated, vidTime, vidMaker, vidRoles, imgShot1, imgShot2, imgShot3) VALUES ('$id', '$vidLD', '$vidYear', '$vidCity', '$vidZanr', '$vidQuality', '$vidTranslated', '$vidMaker', '$vidRoles', '".$imgShot[0]."','".$imgShot[1]."','".$imgShot[2]."')";

正确?

<?php

include '../connect/con.php';

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'];
    for($i = 0; $i < count($tmp_name_array); $i++){
        if(move_uploaded_file($tmp_name_array[$i], "videoCover/".$name_array[$i])){
            echo $name_array[$i]." upload is complete<br>";
            $imgShot[$i]=base64_encode(file_get_contents('videoCover/'.$name_array[$i]));
        } else {
            echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
        }   
    }



$id = mysqli_real_escape_string($con, $_POST['id']);
$vidLD = mysqli_real_escape_string($con, $_POST['vidLD']);
$vidYear = mysqli_real_escape_string($con, $_POST['vidYear']);
$vidCity = mysqli_real_escape_string($con, $_POST['vidCity']);
$vidZanr = mysqli_real_escape_string($con, $_POST['vidZanr']);
$vidQuality = mysqli_real_escape_string($con, $_POST['vidQuality']);
$vidTranslated = mysqli_real_escape_string($con, $_POST['vidTranslated']);
$vidMaker = mysqli_real_escape_string($con, $_POST['vidMaker']);
$vidRoles = mysqli_real_escape_string($con, $_POST['vidRoles']);
$vidTime = mysqli_real_escape_string($con, $_POST['vidTime']);

$sql="INSERT INTO videoinformation (id, vidLD, vidYear, vidCity, vidZanr, vidQuality, vidTranslated, vidTime, vidMaker, vidRoles, imgShot1, imgShot2, imgShot3) VALUES ('$id', '$vidLD', '$vidYear', '$vidCity', '$vidZanr', '$vidQuality', '$vidTranslated', '$vidMaker', '$vidRoles', '".$imgShot[0]."','".$imgShot[1]."','".$imgShot[2]."')";
if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}echo "Video Description are added";

mysqli_close($con);
}
?>

答案 1 :(得分:0)

您搜索$_FILES http://php.net/manual/de/reserved.variables.files.php

您可以从$_FILES - 数组

获取所有信息

编辑:测试一下!

<FORM ENCTYPE="multipart/form-data" ACTION="upload.php" METHOD="POST">
<INPUT NAME="file[]" TYPE="file">
<INPUT NAME="file[]" TYPE="file">
<INPUT NAME="file[]" TYPE="file">
</FORM>

<?php
foreach( $_FILES['file'] as $file ){
    print_r( $file );
}