上传多个图像,根据之前上传的值提供唯一名称

时间:2015-03-02 01:31:25

标签: php arrays image file laravel

我正在尝试上传多张图片,但根据之前上传的值给它一个唯一的名称。

问题:如果我添加一个睡眠(3),序列将是正确的,但有些图像仍然不会被上传,可能是因为它正在睡觉?

如果我不睡觉,则值为1然后2 2 2 2 2 2 2 2 3

在继续之前,如何等待move_uploaded_file完成?它似乎没有这样做。

$files = $_FILES['fileselect'];
            foreach ($files as $file) {
                    //Check Database for Latest File "Name" Count

                    //TODO , verify Orderby
                    $node = DB::table('product_options')->where('product_id', Input::get('productID'))->orderby('created_at','desc')->first();


                    if($node){
                        //If extra image already exist
                        $keywords = preg_split("/[_]+/", $node->image);

                        var_dump($keywords);
                        $fn = Input::get('productID').'_'.($keywords[1]+1).'_extra'.'.jpg';
                        $returnID = Option::create(array('image'=>'productImg/'.$fn,'product_id' =>Input::get('productID')));
                        move_uploaded_file($file[0],'productImg/' . $fn);



                        echo("<p>File $fn uploaded.</p>");

                    }else{

                        $fn = Input::get('productID').'_1_extra'.'.jpg';
                        $returnID = Option::create(array('image'=>'productImg/'.$fn,'product_id' =>Input::get('productID')));
                        move_uploaded_file($file[0],'productImg/' . $fn);
                        echo ("<p>File $fn uploaded.</p>");
                    }
            }

没有睡眠,Echo会返回此

File 52_1_extra.jpg uploaded.

array(3) { [0]=> string(13) "productImg/52" [1]=> string(1) "1" [2]=> string(9) "extra.jpg" }
File 52_2_extra.jpg uploaded.

array(3) { [0]=> string(13) "productImg/52" [1]=> string(1) "1" [2]=> string(9) "extra.jpg" }
File 52_2_extra.jpg uploaded.

array(3) { [0]=> string(13) "productImg/52" [1]=> string(1) "1" [2]=> string(9) "extra.jpg" }
File 52_2_extra.jpg uploaded.

array(3) { [0]=> string(13) "productImg/52" [1]=> string(1) "1" [2]=> string(9) "extra.jpg" }
File 52_2_extra.jpg uploaded.

使用Sleep Echo会将其按顺序(正确)返回但是在我的文件夹中丢失一半(<一半)。

1 个答案:

答案 0 :(得分:1)

我认为你必须使用变量控制增量,如下所示:

$files = $_FILES['fileselect'];
$i = 0;
            foreach ($files as $file) {
                    //Check Database for Latest File "Name" Count

                    //TODO , verify Orderby
                    $node = DB::table('product_options')->where('product_id', Input::get('productID'))->orderby('created_at','desc')->first();


                    if($node){
                        //If extra image already exist
                        $keywords = preg_split("/[_]+/", $node->image);

                        var_dump($keywords);

if($i == 0) {
$i += $keywords[1] + 1;
}
else {
++$i;
}

                        $fn = Input::get('productID').'_'. $i .'_extra'.'.jpg';
                        $returnID = Option::create(array('image'=>'productImg/'.$fn,'product_id' =>Input::get('productID')));
                        move_uploaded_file($file[0],'productImg/' . $fn);



                        echo("<p>File $fn uploaded.</p>");

                    }else{

                        $fn = Input::get('productID').'_1_extra'.'.jpg';
                        $returnID = Option::create(array('image'=>'productImg/'.$fn,'product_id' =>Input::get('productID')));
                        move_uploaded_file($file[0],'productImg/' . $fn);
                        echo ("<p>File $fn uploaded.</p>");
                    }
            }