Remove file extension on file upload

后端 未结 4 1924
醉话见心
醉话见心 2021-01-14 05:34

I\'m writing the code for a website right now that\'s uploads images and then displays them gallery style. What I would like to happen is for the file name of the image to b

相关标签:
4条回答
  • 2021-01-14 05:57

    You can use the pathinfo() function (docs).

    $example  = "my_file.jpeg";
    $filename = pathinfo($example, PATHINFO_FILENAME);
    echo $filename; // my_file
    
    0 讨论(0)
  • 2021-01-14 06:01

    You can use rename to remove the extension:

    -> http://www.php.net/manual/en/function.rename.php

    0 讨论(0)
  • 2021-01-14 06:05

    As my comment above implies, it depends on what you consider in the filename to be the name and what is the extension.

    everything up to the last dot:

    $filename = 'some.file.name.zip';
    $name = substr($filename, 0, strrpos($filename, '.'));
    

    everything up to the first dot:

    $filename = 'some.file.name.zip';
    $name = substr($filename, 0, strpos($filename, '.'));
    

    they look the same, but the first one looks for the first dot from the end of the string and the second one from the start of the string.

    0 讨论(0)
  • 2021-01-14 06:16

    Just use preg_replace:

    $name = preg_replace('/(.+)\.\w+$/U', $_FILES['images']['name'], '$1');
    
    0 讨论(0)
提交回复
热议问题