使用逗号分隔的路径上载单行中的多个图像

时间:2014-01-28 14:26:14

标签: php jquery mysql ajax

我想一次选择多个图像并在提交点击想要在单行中用逗号分隔的路径添加mysql。我创建代码如下

代码: -

if(isset($_FILES['files']))
    {
    $count = 1;
    $errors= array();
    foreach($_FILES['files']['tmp_name'] as $key => $tmp_name )
    {
        $file_name = $key.$_FILES['files']['name'][$key];
        $file_size =$_FILES['files']['size'][$key];
        $file_tmp =$_FILES['files']['tmp_name'][$key];
        $file_type=$_FILES['files']['type'][$key];  
        if($file_size > 2097152)
        {
            $errors[]='File size must be less than 2 MB';
        }

        $desired_dir="user_data";
        if(empty($errors)==true)
        {
            if(is_dir($desired_dir)==false)
            {
                // Create directory if it does not exist
                mkdir("$desired_dir", 0700);        
            }

            if(is_dir("$desired_dir/".$file_name)==false)
            {
                move_uploaded_file($file_tmp,"user_data/".$file_name);
            }
            else
            {   
                 //rename the file if another one exist
                 $new_dir="user_data/".$file_name.time();
                 rename($file_tmp,$new_dir) ;
            }
        }
        else
        {
                print_r($errors);
        }

       $file_name = "user_data/".$file_name;

     echo $sql2 = "Insert statement";

    }


<input type="file" id="files" name="files[]"  multiple  placeholder="Please Upload Image" />

那么如何实现这一任务。请帮助我。

1 个答案:

答案 0 :(得分:0)

foreach内,将路径保存为array,如:

$paths[] = "('$path')";

foreach之外构建SQL,类似于:

$sql = "INSERT INTO table (path_field) values " . implode(",", $paths);