Hướng dẫn php pathinfo get extension - php pathinfo get extension

Cách "tốt nhất" phụ thuộc vào ngữ cảnh và những gì bạn đang làm với phần mở rộng tệp đó. Tuy nhiên, However, However,

Nội dung chính ShowShow

  • Làm cách nào để nhận tiện ích mở rộng tệp trong PHP?
  • Làm thế nào để bạn tìm thấy phần mở rộng tệp?
  • Làm cách nào để có được phần mở rộng tên hình ảnh trong PHP?
  • Làm cách nào để tìm đường dẫn của tệp PHP?

Pathinfo nói chung là tốt nhất khi bạn xem xét tất cả các góc.

pathinfo($file, PATHINFO_EXTENSION)

Nó không phải là nhanh nhất, nhưng nó đủ nhanh. Nó rất dễ đọc, dễ nhớ và tái sử dụng ở mọi nơi. Bất cứ ai cũng có thể hiểu nó trong nháy mắt và xóa cờ pathinfo_ext nếu họ cần thêm thông tin về tệp.

Phương pháp Strrpos. Được mô tả trong một số câu trả lời nhanh hơn có nhưng yêu cầu kiểm tra an toàn bổ sung, đến lượt nó, yêu cầu bạn phải bọc nó bên trong một chức năng, để làm cho nó dễ dàng tái sử dụng. Sau đó, bạn phải mang chức năng với bạn từ dự án để dự án hoặc tìm kiếm nó. Gói nó trong một cuộc gọi chức năng với kiểm tra thêm cũng làm cho nó chậm hơn và nếu bạn cần bất kỳ thông tin nào khác về tệp bạn bây giờ có các phương thức khác để gọi và tại thời điểm đó, bạn sẽ mất lợi thế tốc độ trong khi có một giải pháp khó đọc hơn. Tiềm năng cho tốc độ là có nhưng không xứng đáng trừ khi bạn cần giải quyết một nút cổ chai như vậy.

Tôi cũng sẽ loại trừ bất kỳ ý tưởng nào bằng cách sử dụng chất nền, bùng nổ và hầu hết các thao tác thủ công khác vì những lý do tương tự được đề cập ở trên.

❌SplfileInfo rất tuyệt nhưng chiếm nhiều không gian não hơn với rất nhiều giao diện mà bạn không nghi ngờ gì khi lãng phí thời gian học chỉ để tìm kiếm chúng trở lại vào lần tới. Tôi chỉ sử dụng nó trong các trường hợp cụ thể mà bạn sẽ tìm thấy các giao diện bổ sung đáng giá ai đó học SPL khi họ quay lại để thêm/chỉnh sửa mã của bạn sau.

Tôi hoàn toàn không xem xét preg_replace vì bất kỳ hàm regex nào trong PHP chậm hơn trung bình 3 lần so với bất kỳ chức năng nào khác, khó đọc hơn và trong hầu hết các trường hợp có thể dễ dàng thực hiện với một cái gì đó đơn giản hơn. Regex rất mạnh mẽ và nó có vị trí của nó trong những tình huống cụ thể trong đó nó có thể thay thế một số cuộc gọi phương thức và kiểm tra điều kiện trong một dòng. Nhận một phần mở rộng tệp theo cách này giống như sử dụng đe để búa trong đinh.


Mặc dù tất nhiên "tốt nhất" sẽ đi vào dư luận, tôi cho rằng các phương pháp khác chỉ là "tốt nhất" trong các trường hợp chuyên ngành.

Ví dụ: nếu bạn chỉ muốn kiểm tra một loại cụ thể thì tôi sẽ không sử dụng bất kỳ phương pháp nào được đề xuất vì Stripos sẽ là so sánh không nhạy cảm nhất với việc sử dụng.

if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}

Nhưng một lần nữa Pathinfo vẫn sẽ đẹp hơn để đọc và có lẽ đáng giá chi phí hiệu suất.

Nhưng những gì về https://ome.com.///lica.ted?urls?

Trích xuất các đường dẫn từ URL là một mối quan tâm riêng biệt nằm ngoài phạm vi của câu hỏi và sẽ yêu cầu thêm một bước trong mọi trường hợp mà một so sánh chuỗi một lần đơn giản sẽ không làm.

Trong bài viết này, chúng ta sẽ xem cách trích xuất tiện ích mở rộng tên tệp trong PHP, cùng với việc hiểu được việc thực hiện của họ thông qua các ví dụ. Có một vài cách khác nhau để trích xuất phần mở rộng từ tên tệp với PHP, được đưa ra dưới đây:

Sử dụng hàm pathinfo (): Hàm này trả về thông tin về một tệp. Nếu tham số tùy chọn thứ hai bị bỏ qua, một mảng kết hợp có chứa tên dirname, basename, tiện ích mở rộng và tên tệp sẽ được trả về. Nếu tham số thứ hai là đúng, nó sẽ trả về dữ liệu cụ thể.pathinfo() function: This function returns information about a file. If the second optional parameter is omitted, an associative array containing dirname, basename, extension, and the filename will be returned. If the second parameter is true, it will return specific data.pathinfo() function: This function returns information about a file. If the second optional parameter is omitted, an associative array containing dirname, basename, extension, and the filename will be returned. If the second parameter is true, it will return specific data.

Ví dụ: Ví dụ này mô tả việc sử dụng hàm pathInfo () trả về thông tin về một đường dẫn sử dụng mảng kết hợp hoặc chuỗi. & Nbsp;This example describes the use of the pathinfo() function that returns information about a path using an associative array or a string. This example describes the use of the pathinfo() function that returns information about a path using an associative array or a string. 

PHP

if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
9

if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
0
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
1
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
2
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
3__

if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
0
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
5
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
2

if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
1__12
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
6
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
5
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
8
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
9

Sử dụng hàm end (): Nó phát nổ biến tệp và nhận phần tử mảng cuối cùng là phần mở rộng tệp. Hàm Php End () được sử dụng để có phần tử cuối cùng của mảng.end() function: It explodes the file variable and gets the last array element to be the file extension. The PHP end()function is used to get the last element of the array.end() function: It explodes the file variable and gets the last array element to be the file extension. The PHP end()function is used to get the last element of the array.

Ví dụ: Ví dụ này mô tả việc sử dụng hàm & nbsp; end () được sử dụng để tìm phần tử cuối cùng của mảng đã cho.This example describes the use of the end() function that is used to find the last element of the given array.This example describes the use of the end() function that is used to find the last element of the given array.

PHP

if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
9

if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
0
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
1
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
2
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
3__

if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
0
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
97
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
2
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
99

if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
2
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
01
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
02
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
1
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
04

if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
0
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
5
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
2

if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
1__12
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
6
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
5
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
8
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
9

Sử dụng hàm end (): Nó phát nổ biến tệp và nhận phần tử mảng cuối cùng là phần mở rộng tệp. Hàm Php End () được sử dụng để có phần tử cuối cùng của mảng.substr() and strrchr() functions:substr() and strrchr() functions:

  • Ví dụ: Ví dụ này mô tả việc sử dụng hàm & nbsp; end () được sử dụng để tìm phần tử cuối cùng của mảng đã cho. A part of the string is returned. A part of the string is returned.
  • if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
    {
        //its a pdf file
    }
    
    0
    if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
    {
        //its a pdf file
    }
    
    5
    if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
    {
        //its a pdf file
    }
    
    2
    if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
    {
        //its a pdf file
    }
    
    08
    if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
    {
        //its a pdf file
    }
    
    2
    if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
    {
        //its a pdf file
    }
    
    97
    if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
    {
        //its a pdf file
    }
    
    04The last occurrence of a string inside another string is determined.
    The last occurrence of a string inside another string is determined.

Sử dụng các hàm Subr () và strrchr ():This example uses both substr()  function & strchr() function. This example uses both substr()  function & strchr() function. 

PHP

if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
9

if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
0
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
1
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
2
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
3__

if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
0
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
5
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
2

if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
1__12
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
6
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
5
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
8
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
9

Sử dụng hàm end (): Nó phát nổ biến tệp và nhận phần tử mảng cuối cùng là phần mở rộng tệp. Hàm Php End () được sử dụng để có phần tử cuối cùng của mảng.substr() and strrchr() functions: strrpos() function: This function is used to find the last occurrence position of a ‘.’ in a filename and enhances the file position by 1 to explode string (.)

Ví dụ: Ví dụ này mô tả việc sử dụng hàm & nbsp; end () được sử dụng để tìm phần tử cuối cùng của mảng đã cho. A part of the string is returned.This example describes the use of the strrpos() function that finds the position of the last occurrence of a string in another string. 

PHP

if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
9

if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
0
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
1
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
2
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
3__

if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
0
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
5
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
2

if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
1__12
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
6
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
5
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
8
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
9

Sử dụng hàm end (): Nó phát nổ biến tệp và nhận phần tử mảng cuối cùng là phần mở rộng tệp. Hàm Php End () được sử dụng để có phần tử cuối cùng của mảng.substr() and strrchr() functions: preg_replace() function: Using regular expressions like replace and search. The first parameter of thisfunction is the search pattern, the second parameter $1 is a reference to whatever matches the first (.*), and the third parameter is the file name.

Ví dụ: Ví dụ này mô tả việc sử dụng hàm & nbsp; end () được sử dụng để tìm phần tử cuối cùng của mảng đã cho. A part of the string is returned.This example uses the preg_replace() function to perform a regular expression for search and replace the content.

PHP

if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
9

if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
0
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
1
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
2
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
3__

if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
0
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
5
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
2

if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
1__12
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
6
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
5
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
8
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
9

Làm cách nào để nhận tiện ích mở rộng tệp trong PHP?

Sử dụng hàm end (): Nó phát nổ biến tệp và nhận phần tử mảng cuối cùng là phần mở rộng tệp. Hàm Php End () được sử dụng để có phần tử cuối cùng của mảng.substr() and strrchr() functions: echo $extension ; ?> Using end() function: It explodes the file variable and gets the last array element to be the file extension.

Ví dụ: Ví dụ này mô tả việc sử dụng hàm & nbsp; end () được sử dụng để tìm phần tử cuối cùng của mảng đã cho. A part of the string is returned.

if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
0
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
5
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
2
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
08
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
2
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
97
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
04The last occurrence of a string inside another string is determined. Select the Properties option. In the Properties window, similar to what is shown below, see the Type of file entry, which is the file type and extension.

Sử dụng các hàm Subr () và strrchr ():This example uses both substr()  function & strchr() function. 

Sử dụng hàm end (): Nó phát nổ biến tệp và nhận phần tử mảng cuối cùng là phần mở rộng tệp. Hàm Php End () được sử dụng để có phần tử cuối cùng của mảng. strrpos() function: This function is used to find the last occurrence position of a ‘.’ in a filename and enhances the file position by 1 to explode string (.).

Ví dụ: Ví dụ này mô tả việc sử dụng hàm & nbsp; end () được sử dụng để tìm phần tử cuối cùng của mảng đã cho.This example describes the use of the strrpos() function that finds the position of the last occurrence of a string in another string. 

Sử dụng hàm end (): Nó phát nổ biến tệp và nhận phần tử mảng cuối cùng là phần mở rộng tệp. Hàm Php End () được sử dụng để có phần tử cuối cùng của mảng. preg_replace() function: Using regular expressions like replace and search. The first parameter of thisfunction is the search pattern, the second parameter $1 is a reference to whatever matches the first (.*), and the third parameter is the file name.

Ví dụ: Ví dụ này mô tả việc sử dụng hàm & nbsp; end () được sử dụng để tìm phần tử cuối cùng của mảng đã cho.This example uses the preg_replace() function to perform a regular expression for search and replace the content.

if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
1__12
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
6
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
5
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
8
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
    //its a pdf file
}
9Làm cách nào để nhận tiện ích mở rộng tệp trong PHP?$filename = pathinfo(path); Example: This example explains the pathinfo() function that will return information about a path. Here, we will use $filename['basename'], when we want to access the file name.