Upload image in Lavarel 5 encrypt and delete with Dropzone.js

0

I have a problem with Dropzone.js to delete images after being encrypted and upload with Laravel 5 I have the following code:

$file = $request->file('image');
            $extension       = $file->getClientOriginalExtension() ?: 'png';
            $fileName        = $file->getClientOriginalName();
            $folderName      = '/uploads/images/posts/' . date("Ym", time()) .'/'.date("d", time());
            $destinationPath = public_path() . $folderName;
            $safeName        = md5($fileName).'.'.$extension;
            $file->move($destinationPath, $safeName);

Once encrypted and uploaded by ajax with Dropzone if the user makes a mistake or uploads it by mistake or mistake, it should be deleted by the same user before being published.

I put the Dropzone JS code that I use, but that code sends me the original name of the file but not the one encrypted in L5

Dropzone.options.uploadWidget = {
  paramName: 'image',
  maxFilesize: 10, // MB
  maxFiles: 1,
  dictDefaultMessage: 'Click para subir una imagen',
  previewTemplate: document.querySelector('#preview-template').innerHTML,
  previewsContainer: '#dropzonePreview',
  addRemoveLinks: true,
  dictRemoveFile: 'Remove',
  acceptedFiles: 'image/*',
  init: function() {
    this.on('success', function( file, resp ){
      //console.log(resp.url);
      $('#file').attr( 'value',resp.name);
    });

    this.on('thumbnail', function(file) {
      if ( file.width < 200 || file.height < 200 ) {
        file.rejectDimensions();
      } else {
        file.acceptDimensions();
      }
    });
    this.on('removedfile', function(file) {

      $.ajax({
        type: 'POST',
        url: site_path + '/post/ajaxdeletefile',
        data: {id: file.name},
        dataType: 'html',
        success: function(data){
        //  console.log(data);
        }
      });
    });
  },

  accept: function(file, done) {
    file.acceptDimensions = done;
    file.rejectDimensions = function() {
      done('The image must be at least 640 x 480px')
    };
  }
};
    
asked by vdjkelly 27.02.2016 в 17:01
source

1 answer

0

If you only need an image in the folder where it will be saved, what you can do is read the name of the file

function listar_archivos($carpeta){
    if(is_dir($carpeta)){
        if($dir = opendir($carpeta)){
            while(($archivo = readdir($dir)) !== false){
                if($archivo != '.' && $archivo != '..' && $archivo != '.htaccess'){
                    echo '<li><a target="_blank" href="'.$carpeta.'/'.$archivo.'">'.$archivo.'</a></li>';
                }
            }
            closedir($dir);
        }
    }
}

echo listar_archivos('\tu\directorio');

this if you only need one image for each post so you can delete it from the server side

    
answered by 01.03.2016 / 22:17
source