多个图像上载时间不会在Laravel中的文件夹中上载相同的图像

时间:2018-06-02 07:14:38

标签: php laravel

我在多张图片上传时遇到问题,但所有上传的图片都不一样所以我停止上传文件夹中的重复图片

控制器:RoomsController.php

public function add_photos(Request $request,EmailController $email_controller)
{      

    if(isset($_FILES["photos"]["name"]))
    {  $rows = array();
        $err = array();
        foreach($_FILES["photos"]["error"] as $key=>$error) 
        {
            $tmp_name = $_FILES["photos"]["tmp_name"][$key];

            $name = str_replace(' ', '_', $_FILES["photos"]["name"][$key]);

            $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));

            $name = time().'_.'.$ext;

            $filename = dirname($_SERVER['SCRIPT_FILENAME']).'/images/rooms/'.$request->id;




            if(!file_exists($filename))
            {
                mkdir(dirname($_SERVER['SCRIPT_FILENAME']).'/images/rooms/'.$request->id, 0777, true);
            }

            if($ext == 'png' || $ext == 'jpg' || $ext == 'jpeg' || $ext == 'gif')   
            {            
                // if($this->helper->compress_image($tmp_name, "images/rooms/".$request->id."/".$name, 80))
                if(UPLOAD_DRIVER=='cloudinary')
                {
                    $c=$this->helper->cloud_upload($tmp_name);
                    if($c['status']!="error")
                    {
                         $name=$c['message']['public_id'];    
                    }
                    else
                    {
                        $err = array('error_title' => ' Photo Error', 'error_description' => $c['message']);
                        $result = RoomsPhotos::where('room_id',$request->id)->get();
                        $rows['succresult'] = $result;
                        $rows['error'] = $err;
                        return json_encode($rows);
                        exit;
                    }
                }
                else
                {
                    if(move_uploaded_file($tmp_name, "images/rooms/".$request->id."/".$name))
                    {
                        // $this->helper->compress_image("images/rooms/".$request->id."/".$name, "images/rooms/".$request->id."/".$name, 80, 993, 662);
                        $this->helper->compress_image("images/rooms/".$request->id."/".$name, "images/rooms/".$request->id."/".$name, 80, 1440, 960);
                        $this->helper->compress_image("images/rooms/".$request->id."/".$name, "images/rooms/".$request->id."/".$name, 80, 1349, 402);
                        $this->helper->compress_image("images/rooms/".$request->id."/".$name, "images/rooms/".$request->id."/".$name, 80, 450, 250);
                    }
                }
                $photos          = new RoomsPhotos;
                $photos->room_id = $request->id;
                $photos->name    = $name;
                $photos->save();

                $this->update_status($request->id);
            }

以上是我的控制器添加照片,我必须在图像/房间文件夹中设置我的上传图像

型号:Roomsphoto.php

<?php


namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class RoomsPhotos extends Model
{

    protected $table = 'rooms_photos';

    public $timestamps = false;

    protected $appends = ['steps_count'];

    // Get steps_count using sum of rooms_steps_status
    public function getStepsCountAttribute()
    {
        $result = RoomsStepsStatus::find($this->attributes['room_id']);

        return 6 - ($result->basics + $result->description + $result->location + $result->photos + $result->pricing + $result->calendar);
    }

    // Get Name Attribute
    public function getNameAttribute(){
        $photo_src=explode('.',$this->attributes['name']);
        if(count($photo_src)>1)
        {
            $photo_details = pathinfo($this->attributes['name']); 
            $name = @$photo_details['filename'].'_450x250.'.@$photo_details['extension'];
            return url().'/images/rooms/'.$this->attributes['room_id'].'/'.$name;
        }
        else
        {
            $options['secure']=TRUE;
            $options['width']=450;
            $options['height']=250;
            $options['crop']='fill';
            return $src=\Cloudder::show($this->attributes['name'],$options);
        }

    }
    // Get Slider Image Name Attribute
    public function getSliderImageNameAttribute(){
        $photo_src=explode('.',$this->attributes['name']);
        if(count($photo_src)>1)
        {
            $photo_details = pathinfo($this->attributes['name']); 
            $name = @$photo_details['filename'].'_1440x960.'.@$photo_details['extension'];
            return url().'/images/rooms/'.$this->attributes['room_id'].'/'.$name;
        }
        else
        {
            $options['secure']=TRUE;
            $options['width']=1440;
            $options['height']=960;
            $options['crop']='fill';
            return $src=\Cloudder::show($this->attributes['name'],$options);
        }

    }
    // Get Banner Image Name Attribute
    public function getBannerImageNameAttribute(){
        $photo_src=explode('.',$this->attributes['name']);
        if(count($photo_src)>1)
        {
            $photo_details = pathinfo($this->attributes['name']); 
            $name = @$photo_details['filename'].'_1349x402.'.@$photo_details['extension'];
            return url().'/images/rooms/'.$this->attributes['room_id'].'/'.$name;
        }
        else
        {
            $options['secure']=TRUE;
            $options['width']=1349;
            $options['height']=402;
            $options['crop']='fill';
            return $src=\Cloudder::show($this->attributes['name'],$options);
        }
    }
}

以上是我的样板房照片

查看文件:photos.blade.php

<div id="js-photos-grid" class="photo-encourage">
<div class="row row-table">
  <div class="col-md-6">
<div class="add-photos-button">
  <input type="file" class="hide" name="photos[]" multiple="true" id="upload_photos" accept="image/*">
  <button id="photo-uploader" class="btn btn-large row-space-2" style="position: relative; z-index: 0;">
    <i class="icon icon-upload ico_left"></i> {{ trans_choice('messages.lys.add_photo',2) }}
  </button>
</div>
  </div>

<div class="h4 text-right text-muted" id="photo_count"  ng-cloak ng-show="photos_list.length > 0" {{ ($rooms_status->photos == 0) ? 'style="display:none;"' : ''}}>
<small>@{{ photos_list.length }} {{ trans_choice('messages.lys.photo',1) }}<span ng-show="photos_list.length > 1">s</span></small>
</div>
</div>

以上是我的刀片查看多张照片上传添加照片多个时间

0 个答案:

没有答案