在php

时间:2015-08-24 11:41:14

标签: javascript php

我必须将多个图像上传到mysql但是这段代码无法获得正确的输出请帮帮我... 这是我的代码: -

$target_folder = "../tour-images/";
if($_FILES['photo_url']['name']) {
    //echo "hii";
    $allowable_extensions = array("image/jpg", "image/jpeg", "image/pjpeg", "image/png","image/gif");
    $imagename = $_FILES["photo_url"]["name"];
    //echo "<br>hii".$imagename."<br>".$allowable_extensions;
    $file_type = $_FILES["photo_url"]["type"];
    print_r($file_type);

    if (in_array($file_type, $allowable_extensions)) {
        for($i = 0; $i < count($_FILES['file']['name']); $i++) {
            //$file_ext=substr($file_type, 6);
            $file_ext = explode('.', $file_type[$i]);
            echo $file_ext;
            $source = $_FILES["photo_url"]['tmp_name'];
            $target1 = $target_folder.$imagename.".".$file_ext;
            echo "<br>".$target1;
            $photourl=$target_folder.$imagename.".".$file_ext;
            echo "hcxxxxxxx".$photourl;

            if(move_uploaded_file($source, $target1))
            {
                $filename11 = $target1;                     
            }
        }
    }
} else {
    $filename = $target_folder."noLogo.jpeg";
    $photourl = "noLogo.jpeg";
}

$sql = "INSERT INTO image_details(tour_id,photo_url) VALUES('".$id."','".$filename11."')";
mysql_query($sql);

echo $sql;

1 个答案:

答案 0 :(得分:0)

尝试以下代码上传多个文件并存储在数据库中。

<!doctype html>
<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <form method="post" enctype="multipart/form-data">
            <input type="text" name="Filename">
            <input type="file" name="photo_url[]" multiple>
            <input type="submit" value="Upload">
        </form>
        <?php
            if (isset($_FILES['photo_url'])) {
                $images = array();
                $myFile = $_FILES['photo_url'];
                $fileCount = count($myFile["name"]);
                $Filename=$_POST['Filename'];
                $structure = "Uploads/SystemConfiguration/$Filename";
                if (!mkdir($structure, 0777, true))
                {
                 die('Failed to create folders...');
                }

                for ($i = 0; $i < $fileCount; $i++) {
                $name = $_FILES["photo_url"]["name"][$i];
                $tmp_name=$_FILES["photo_url"]["tmp_name"][$i];
                move_uploaded_file($tmp_name,"$structure/$name");
                $images[] = "$structure/$name";
                }
            }
            $all_images = implode(",",$images);
        ?>
    </body>
</html>

$sql = "INSERT INTO image_details(tour_id,photo_url) VALUES('".$id."','".$all_images."')";
mysql_query($sql);
echo $sql;