Código para listar imagens maiores que 1 MB

Código para listar imagens maiores que 1 MB

Escrito por:
Igor Nascimento
Compartilhe:
Data da postagem:
<?php

// Defina o nome do arquivo CSV
$csv_file = ABSPATH . 'arquivos_maiores_1MB.csv';

// Abre ou cria o arquivo CSV
$fp = fopen($csv_file, 'w');

// Cabeçalho do CSV
fputcsv($fp, array('URL da Mídia', 'Tipo', 'Tamanho (MB)', 'Link do Arquivo', 'Usado em (URL do Post/Página)'));

// Busca imagens e PDFs
$args = array(
    'post_type'      => 'attachment',
    'post_mime_type' => array('image', 'application/pdf'),
    'posts_per_page' => -1,
);

$files = get_posts($args);

foreach ($files as $file) {
    $file_path = get_attached_file($file->ID);
    if (file_exists($file_path)) {
        $file_size = filesize($file_path);
        if ($file_size > 1048576) { // maior que 1 MB
            $file_url     = wp_get_attachment_url($file->ID);
            $file_size_mb = round($file_size / 1024 / 1024, 2);

            // Descobre o tipo (Imagem ou PDF)
            $mime = get_post_mime_type($file->ID);
            $tipo = (strpos($mime, 'pdf') !== false) ? 'PDF' : 'Imagem';

            // URL da mídia no WP
            $media_url = get_permalink($file->ID);

            // Verifica se está associado a algum post/página
            $used_in_url = '';
            if ($file->post_parent) {
                $used_in_url = get_permalink($file->post_parent);
            } else {
                $used_in_url = 'Não vinculado';
            }

            // Escreve no CSV
            fputcsv($fp, array($media_url, $tipo, $file_size_mb, $file_url, $used_in_url));
        }
    }
}

// Fecha o arquivo
fclose($fp);

echo 'CSV gerado em: ' . $csv_file;

<?php

// Defina o nome do arquivo CSV
$csv_file = ABSPATH . 'arquivos_maiores_1MB.csv';

// Abre ou cria o arquivo CSV
$fp = fopen($csv_file, 'w');

// Cabeçalho do CSV
fputcsv($fp, array('URL da Mídia', 'Tipo', 'Tamanho (MB)', 'Link do Arquivo', 'Usado em (URL do Post/Página)'));

// Busca imagens e PDFs
$args = array(
    'post_type'      => 'attachment',
    'post_mime_type' => array('image', 'application/pdf'),
    'posts_per_page' => -1,
);

$files = get_posts($args);

foreach ($files as $file) {
    $file_path = get_attached_file($file->ID);
    if (file_exists($file_path)) {
        $file_size = filesize($file_path);
        if ($file_size > 1048576) { // maior que 1 MB
            $file_url     = wp_get_attachment_url($file->ID);
            $file_size_mb = round($file_size / 1024 / 1024, 2);

            // Descobre o tipo (Imagem ou PDF)
            $mime = get_post_mime_type($file->ID);
            $tipo = (strpos($mime, 'pdf') !== false) ? 'PDF' : 'Imagem';

            // URL da mídia no WP
            $media_url = get_permalink($file->ID);

            // Verifica se está associado a algum post/página
            $used_in_url = '';
            if ($file->post_parent) {
                $used_in_url = get_permalink($file->post_parent);
            } else {
                $used_in_url = 'Não vinculado';
            }

            // Escreve no CSV
            fputcsv($fp, array($media_url, $tipo, $file_size_mb, $file_url, $used_in_url));
        }
    }
}

// Fecha o arquivo
fclose($fp);

echo 'CSV gerado em: ' . $csv_file;