Grab the file extension

Asked

Viewed 15,219 times

7

Hi, I tried to use the pathinfo() but I guess I didn’t use it the right way, I’d like to know how to use.

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

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

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

He gives that mistake

and line 2 is this

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

    You can put an output display of 'echo $arquivo_up'

  • It is not an error per se. You are reporting that the variable is null, or $FILES[] contains nothing. Usually appears at pre-checkout times. The form may not send the file if the enctype is not in Multipart/form-data

2 answers

12

Your code is very close to what you are trying to get. Try this way:

$ext = pathinfo($filename, PATHINFO_EXTENSION);

The function pathinfo can receive various options according to your needs:

  • PATHINFO_DIRNAME
  • PATHINFO_BASENAME
  • PATHINFO_EXTENSION
  • PATHINFO_FILENAME

It can 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 probable cause may be that your configuration 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 be according to your needs. If you are uploading larger files you should adjust.

Another thing you should check, the tag form

<form enctype="multipart/form-data" action='...' method='post'>
  • It still doesn’t work, even if you put a torrent.

  • You can update your question with the output of 'echo $arquivo_up'

  • I put and nothing appears, the code continues normally

  • This means that your array probably does not contain the file name. You can see the output of var_dump($_FILES);

  • It appeared here array (size=0) Empty

0

Since your $_FILES is empty, here’s a Checklist for upload of files with PHP

On the server:

  1. Check 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 while setting up the size drive. Note above that it is 100M and not 100MB

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

You may need a file .htaccess or .user.ini if you were to use a shared server on which you would not have access to `php.ini

  1. Ensure that your temporary file directory, defined in the directive upload_tmp_dir of php.ini, and the directory in which you will write the uploaded files have the proper read and write permissions defined

Such directories cannot have spaces like /tmp/my uploads

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

As for the HTML:

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

  2. GET requests do not support uploads with multipart/form-data, then your forms should be defined with method="POST"

Such attribute must be in (s) tag(s) <form> and wrapped in quotes and not backticks that some editors end up accidentally converting.

  1. Make sure you don’t have two or more <input type="file" /> of the same value for the attribute name. If you need multiple upload, add a pair of brackets at the end of the name:

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

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

  1. Remember to close all tags <form> from your page with the counterparty </form>

  2. Make sure you’re not nesting forms:

    <form><form></form></form>
    
  3. And neither overlapping tags:

    <div><form></div></form>
    
  4. Last but not least, make sure there is no Javascript by disabling the form submission or removing the(s) element(s) <input type="file">

Regarding the end user:

  1. Make sure that the file being sent does not have non-alphinical characters in it

  2. If "asynchronous" upload, Javascript must obviously be enabled

What about the programmer?

Well, you’re getting an E_NOTICE undefined index so you should check if such an index exists.

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

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

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

Just keep in mind path to be analyzed end with a point (which for all intents and purposes is a file without a valid extension), pathinfo() return an empty string, but if the file has no 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 point in the string, returns the value after it, backwards.

If no, returns the string itself, since strrpos() will fail and the cast automatic PHP will make replace() consider this "failure" as zero and this makes the substring start from the beginning of it.

Browser other questions tagged

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