Home > Software design >  How to separate the extension from a file?
How to separate the extension from a file?

Time:10-09

I need to encode a file before i include it to the bank with the code below:

    $file = 'CT0001_0002.1.jpg';
    $validate = pathinfo($file, PATHINFO_EXTENSION);
    list($name,$extension) = explode($validate,$file);
    $date = date("Y-m-d H:i");
    $cod = md5($name.$date).".".$extension;
    echo $cod;

When I print, it returns like this:

6ab87875286005866f0504961fc2438c.

Without the extension. Pathinfo() is enabled on the server. He should return:

6ab87875286005866f0504961fc2438c.jpg

CodePudding user response:

pathinfo already provides all the details you'd need:

<?php

$file = 'CT0001_0002.1.jpg';
$validate = pathinfo($file);
$date = date("Y-m-d H:i");

$cod = md5($validate['filename'] . $date) . "." . $validate['extension'];
echo $cod;

print_r($validate) would output:

Array
(
    [dirname] => .
    [basename] => CT0001_0002.1.jpg
    [extension] => jpg
    [filename] => CT0001_0002.1
)
  •  Tags:  
  • php
  • Related