Grab file extension to send with attachment

Asked

Viewed 543 times

1

I have a code that sends attachments, I saved it with custom name to find easier later in the database and also to make a more organized backup, but by this fact, it does not save the file extension.

In this part of the code $emailattachment = $_POST['email'].$date = date('_d-m-Y H:i:s'); i would like to add a .$extensao. How I would do to grab the original file extension, just the extension and add there?

Code send Attachments?

$_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);
} 

1 answer

1


Considering your example:

//caminho do anexo
$path = $_FILES['attachment']['tmp_name'];
//pega a extensao do arquivo
$ext = pathinfo($path, PATHINFO_EXTENSION);

More details of a look pathinfo.

  • It worked, but I had to edit it, some things weren’t "right"

  • @Gustavosouza Post your functional code then, should help others.

  • I put it there in my question, "Solution".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.