I have a code that sends attachments, I save it with custom name to find it easier later on in the database and also to make a more organized backup, but for that fact, it does not save the file extension.
In this part of code $emailattachment = $_POST['email'].$date = date('_d-m-Y H:i:s');
I'd like to add .$extensao
. How would I get the extension of the original file, just the extension and add it there?
Send Attachments Code?
$_UP['pasta'] = '/opt/lampp/htdocs/magento/media/teste/';
$_UP['caminho'] = '/media/teste/';
$_UP['renomeia'] = false;
if(filter_input(INPUT_POST, "btnSubmit")){
if(isset($_POST['email'])){
$emailattachment = $_POST['email'].$date = date('_d-m-Y H:i:s');
}
if ($_UP['renomeia'] == true) {
$nome_final = md5(time()).'.jpg';
} else {
$nome_final = $emailattachment;
}
if (move_uploaded_file($_FILES['attachment']['tmp_name'], $_UP['pasta'] . $nome_final))
Solution
$_UP['pasta'] = '/opt/lampp/htdocs/magento/media/teste/';
$_UP['caminho'] = '/media/teste/';
$_UP['renomeia'] = true;
if(filter_input(INPUT_POST, "btnSubmit")){
$path = $_FILES['attachment']['name'];
$extensao = pathinfo($path, PATHINFO_EXTENSION);
$emailattachment = $_POST['email'] . $date = date('_d-m-Y_H-i-s') .'.'. $extensao;
if ($_UP['renomeia'] == true) {
$nome_final = $emailattachment;
}
if (move_uploaded_file($_FILES['attachment']['tmp_name'], $_UP['pasta'] . $nome_final)) {
$connection = Mage::getSingleton('core/resource')->getConnection('core_write');
$query = "INSERT INTO contato ('nome','caminho') VALUES ('".$nome_final."', '".$_UP['pasta']."')";
$connection->query($query);
}