Replace files through FTP

Created on 28 April 2014, over 10 years ago
Updated 14 October 2023, about 1 year ago

Hey all

We're currently facing some issues with corrupted files. They were originally uploaded through the Drupal interface but as it became very impractical to keep changing the files through said interface we decided to overwrite the files through FTP. This resulted in the corrupted files.
How do we make this work? I was thinking about a cron job that checks the file size and changes it, but to be fair I have no idea if it's the saved file size in the database that needs to be changed. We're talking about private files here.
There are quite a few products and new versions regularly, so going through the interface isn't the most ideal option. Is there an alternative workflow?

Any input is greatly appreciated.

P

💬 Support request
Status

Fixed

Component

Miscellaneous

Created by

🇧🇪Belgium ppelgrims

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇫🇷France gilbertdelyon

    This thread is exactly what I need.
    But of course it does not work in DRUPAL 10. (deprecated functions)
    Would somebody be so kind to help me finding how to code this in D10?

    Thanks in advance

  • 🇫🇷France gilbertdelyon

    I have added this this piece of code in a controller in D10.
    May be there is simpler way, but It works!

    private function updatevideoFilesize(){
            
            // ftech video files in file_managed table
            $db = \Drupal::service('database');
            $query = $db->select('file_managed', 't')->fields('t', ['fid','uri', 'filesize']);
            $query->condition('filemime', "video%", 'LIKE');
            $videofiles = $query->execute()->fetchAll();
            
            // variable to be filled with processing reports during the loop
            $output = ''; 
            
            // Loop through the files 
            foreach ($videofiles as $file) {
                
                //$output.= 'fid = '.$file->fid.' | uri = '.$file->uri.' | sizeBEFORE = '.$file->filesize.'<br/>'; //debug
             
                //Load the file entity by URI
                $loadedfile = \Drupal::entityTypeManager()
                                ->getStorage('file')
                                ->loadByProperties(['uri' => $file->uri]);
                 
                // Check if the file entity was loaded successfully.
                //$output.= $loadedfile ? 'successfull load YES <br/>': 'successfull load NO<br/>';//debug
                
                // Get real file size
                $stream_wrapper_manager = \Drupal::service('stream_wrapper_manager')->getViaUri($file->uri);
                $file_path = $stream_wrapper_manager->realpath();
                $realFileSize = filesize($file_path);
                
                if($realFileSize != $file->filesize){
                
                    //$output.=  $file_path.' | realSIZE = '. $realFileSize.'<hr/>'; //debug
        
                    // Store real size of file
                    $query = $db->update('file_managed')->fields(['filesize'=>$realFileSize] );
                    $query->condition ('fid',$file->fid,'=');
                    $query->execute();
                    
                    $output .= 'fid = '.$file->fid.' | uri = '.$file->uri.'<br/>';
                    $output .= 'former size = '.$file->filesize.' | new size = '.$realFileSize.'<hr/>';
                    
                }//end if
                
            } //end foreach 
            
             
            $output = ($output =='')? 'No filesize to be updated' : 'Following filesizes have been uptdated.<hr/>'.$output;
            
            return $output;
            
       }//fin function
    
Production build 0.71.5 2024