Get the extension of a file

4

Hello, I tried to use pathinfo() but I think I did not use it correctly, I would like to know how to use it.

$arquivo_up = $_FILES['arquivo']['name'];

$extensao = pathinfo($arquivo_up);
$extensao = $extensao['extension'];

if($extensao == "torrent")
{
}

He gives this error

and line 2 is this

$arquivo_up = $_FILES['arquivo']['name'];
    
asked by anonymous 05.05.2015 / 19:06

3 answers

7

Your code is very close to what you're trying to get. Try this:

$ext = pathinfo($filename, PATHINFO_EXTENSION);

pathinfo can receive several options according to your needs:

  • PATHINFO_DIRNAME
  • PATHINFO_BASENAME
  • PATHINFO_EXTENSION
  • PATHINFO_FILENAME

It may happen, I do not know if it is your case, that the file has no extension, in which case it will not return any extension.

Another likely cause may be that your setting is not correct. Usually these are the parameters you should use (php.config)

file_uploads    "1"
upload_max_filesize     "2M"
post_max_size   "8M"
max_file_uploads    20

Upload_max_filesize should match your needs. If you are uploading larger files you should adjust.

Another thing you should check, the form tag

<form enctype="multipart/form-data" action='...' method='post'>
    
05.05.2015 / 19:09
0

It's not an error in itself. It is reporting that the variable is null, or the $ FILES [] contains nothing. Usually appears at pre-checkout times.

It can happen that the form does not send the file, if the enctype is not in multipart / form-data

    
29.05.2017 / 05:40
0

Since your $_FILES is empty, here is a checklist for upload of files with PHP

On the server:

  • Verify in your php.ini whether the following directives have these values:

    file_uploads = On
    post_max_size = 100M
    upload_max_filesize = 100M

  •   

    Beware of common errors when setting up the unit of size. Note above that it is 100M and not 100MB

  • Make sure you're editing php.ini right. After saving the options and restarting the server check through a phpinfo () if the values defined are the ones you defined
  •   

    You may need a .htaccess or .user.ini file if you are using a shared server where you would not have access to 'php.ini

  • Ensure that your temporary files directory, set in the upload_tmp_dir of php.ini directive, and the directory in which you are going to write the uploaded files have the required read and write permissions set
  •   

    Such directories can not have spaces like /tmp/my uploads

  • It is very rare, especially in your case with small files, but a comment, now extinct, in the PHP Manual, was directed to make sure that there is enough disk space for the operation
  • As for HTML:

  • Make sure your form has the enctype="multipart/form-data" attribute.

  • GET requests do not support uploads with multipart/form-data , so your forms must be set with method="POST"

  •   

    This attribute must be in the <form> tag (s) and surrounded by quotes and not backticks that some editors end up accidentally converting.

  • Make sure you do not have two or more <input type="file" /> with the same value for the name attribute. If you need multiple uploads, add a couple of brackets to the end of the name:

    <input type="file" name="files[]">
    <input type="file" name="files[]">
    
  •   

    Although obvious, the name attribute must be present in all fields that must be processed

  • Remember to close all <form> tags on your page with the </form> counterpart

  • Make sure you are not nesting forms:

    <form><form></form></form>
    
  • And no overlapping tags:

    <div><form></div></form>
    
  • Last, but not least, check that there is no JavaScript disabling the submission of the form or removing the <input type="file"> element

  • Regarding the end user:

  • Make sure the file being uploaded does not have any non-alpha characters in it

  • If you are uploading "asynchronous" JavaScript should obviously be enabled

  • What about the programmer?

    Well, you're getting an undefined E_NOTICE index so you should check if that index exists.

    Give a var_dump () / print_r () in $ _FILES and see the structure of the superglobal. Is it REALLY one-dimensional?

    Quite common in multiple uploads, suddenly what you use as $_FILES['arquivo']['name'] may have an additional numeric index

    Resolved the case, to get the file extension you can do with pathinfo () , as you are already doing, but add the default constant PATHINFO_EXTENSION as already suggested.

    Just note that if the path to be parsed ends with a dot (which for all intents and purposes is a file without a valid extension), pathinfo () will return an empty string, but if the file does not have any extension it will return NULL.

    So be careful;)

    Another option would be simple string manipulation:

    $ext = ltrim( substr( __FILE__, strrpos( __FILE__, '.' ) ), '.' );
    

    If there is a dot in the string, it returns the value after it, backwards.

    If it does not, it returns the string itself, since strrpos () will fail and cast automatic PHP will make substr () consider this "failure" as zero and this causes the substring to start from the beginning.

        
    29.05.2017 / 15:47