Hướng dẫn get file name from url php - lấy tên tệp từ url php

Tôi muốn nhận tên tệp mà không có bất kỳ giá trị biến

  $url = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png';
        $file = file_get_contents($url); // to get file
        $name = basename($url); // to get file name
        $ext = pathinfo($url, PATHINFO_EXTENSION); // to get extension
        $name2 =pathinfo($url, PATHINFO_FILENAME); //file name without extension
4 nào từ URL trong PHP?

URL của tôi là

  $url = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png';
        $file = file_get_contents($url); // to get file
        $name = basename($url); // to get file name
        $ext = pathinfo($url, PATHINFO_EXTENSION); // to get extension
        $name2 =pathinfo($url, PATHINFO_FILENAME); //file name without extension
5

Tôi chỉ muốn lấy

  $url = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png';
        $file = file_get_contents($url); // to get file
        $name = basename($url); // to get file name
        $ext = pathinfo($url, PATHINFO_EXTENSION); // to get extension
        $name2 =pathinfo($url, PATHINFO_FILENAME); //file name without extension
6 từ URL?

làm như thế nào?

Tôi đã sử dụng hàm

  $url = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png';
        $file = file_get_contents($url); // to get file
        $name = basename($url); // to get file name
        $ext = pathinfo($url, PATHINFO_EXTENSION); // to get extension
        $name2 =pathinfo($url, PATHINFO_FILENAME); //file name without extension
7 nhưng nó cũng cung cấp tất cả các giá trị biến:
  $url = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png';
        $file = file_get_contents($url); // to get file
        $name = basename($url); // to get file name
        $ext = pathinfo($url, PATHINFO_EXTENSION); // to get extension
        $name2 =pathinfo($url, PATHINFO_FILENAME); //file name without extension
8 nằm trong URL.

Hướng dẫn get file name from url php - lấy tên tệp từ url php

Hỏi ngày 21 tháng 10 năm 2011 lúc 16:17Oct 21, 2011 at 16:17

sqlchildsqlchildsqlchild

8.50427 Huy hiệu vàng102 Huy hiệu bạc166 Huy hiệu Đồng27 gold badges102 silver badges166 bronze badges

1

Điều này sẽ hoạt động:

echo basename($_SERVER['REQUEST_URI'], '?' . $_SERVER['QUERY_STRING']);

Nhưng hãy cẩn thận với bất kỳ phần độc hại nào trong URL của bạn.

Đã trả lời ngày 21 tháng 10 năm 2011 lúc 16:34Oct 21, 2011 at 16:34

4

Các bước sau đây hiển thị tổng số thông tin về cách lấy tệp, tệp với tiện ích mở rộng, tệp không có phần mở rộng. Kỹ thuật này rất hữu ích cho tôi. Hy vọng nó cũng sẽ hữu ích cho bạn.

  $url = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png';
        $file = file_get_contents($url); // to get file
        $name = basename($url); // to get file name
        $ext = pathinfo($url, PATHINFO_EXTENSION); // to get extension
        $name2 =pathinfo($url, PATHINFO_FILENAME); //file name without extension

Đã trả lời ngày 23 tháng 6 năm 2015 lúc 6:13Jun 23, 2015 at 6:13

2

Tốt hơn là sử dụng

  $url = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png';
        $file = file_get_contents($url); // to get file
        $name = basename($url); // to get file name
        $ext = pathinfo($url, PATHINFO_EXTENSION); // to get extension
        $name2 =pathinfo($url, PATHINFO_FILENAME); //file name without extension
9 để chỉ truy xuất đường dẫn, và sau đó chỉ nhận tên tệp với

0. Bằng cách này, chúng tôi cũng tránh các tham số truy vấn.


Có phần giống với câu trả lời của sultan ngoại trừ rằng tôi đang sử dụng tham số


1
  $url = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png';
        $file = file_get_contents($url); // to get file
        $name = basename($url); // to get file name
        $ext = pathinfo($url, PATHINFO_EXTENSION); // to get extension
        $name2 =pathinfo($url, PATHINFO_FILENAME); //file name without extension
9, để chỉ có được đường dẫn.

Hướng dẫn get file name from url php - lấy tên tệp từ url php

Fipsi

6409 Huy hiệu bạc21 Huy hiệu Đồng9 silver badges21 bronze badges

Đã trả lời ngày 14 tháng 9 năm 2017 lúc 11:04Sep 14, 2017 at 11:04

Foxtrotfoxtrotfoxtrot

1.0261 Huy hiệu vàng8 Huy hiệu bạc24 Huy hiệu đồng1 gold badge8 silver badges24 bronze badges

1

Sử dụng


3 như Pekka đã nói:


http://codepad.org/NBBf4yTB

Trong ví dụ này, tên người dùng và mật khẩu tùy chọn không phải là đầu ra!

Đã trả lời ngày 21 tháng 10 năm 2011 lúc 16:30Oct 21, 2011 at 16:30

Hướng dẫn get file name from url php - lấy tên tệp từ url php

ComfaletcomfaletComFreek

28.5K18 Huy hiệu vàng103 Huy hiệu bạc153 Huy hiệu đồng18 gold badges103 silver badges153 bronze badges

4

URL của bạn:

$url = 'http://learner.com/learningphp.php?lid=1348';
$file_name = basename(parse_url($url, PHP_URL_PATH));
echo $file_name;

Đầu ra: LearningPhp.php learningphp.php

Đã trả lời ngày 27 tháng 2 năm 2020 lúc 7:26Feb 27, 2020 at 7:26

Hướng dẫn get file name from url php - lấy tên tệp từ url php

Udhav Sarvaiyaudhav SarvaiyaUdhav Sarvaiya

8.84712 Huy hiệu vàng54 Huy hiệu bạc62 Huy hiệu Đồng12 gold badges54 silver badges62 bronze badges

1

Bạn có thể dùng,

$directoryURI =basename($_SERVER['SCRIPT_NAME']);

echo $directoryURI;

Hướng dẫn get file name from url php - lấy tên tệp từ url php

Đã trả lời ngày 8 tháng 10 năm 2013 lúc 7:52Oct 8, 2013 at 7:52

Hướng dẫn get file name from url php - lấy tên tệp từ url php

SwrswrSwR

5781 Huy hiệu vàng7 Huy hiệu bạc 20 Huy hiệu Đồng1 gold badge7 silver badges20 bronze badges

1

Một cách khác để chỉ nhận tên tệp mà không cần truy vấn là bằng cách sử dụng các hàm parse_url và basename:

$parts = parse_url("http://example.com/foo/bar/baz/file.php?a=b&c=d");
$filename = basename($parts["path"]); // this will return 'file.php'

Đã trả lời ngày 12 tháng 10 năm 2016 lúc 8:57Oct 12, 2016 at 8:57

ᴄʀᴏᴢᴇᴛᴄʀᴏᴢᴇᴛᴄʀᴏᴢᴇᴛ

2.85826 huy hiệu bạc43 huy hiệu đồng26 silver badges43 bronze badges

1

$filename = pathinfo( parse_url( $url, PHP_URL_PATH ), PATHINFO_FILENAME ); 

Sử dụng parse_url để trích xuất đường dẫn từ url, sau đó pathinfo trả về tên tệp từ đường dẫn

Hướng dẫn get file name from url php - lấy tên tệp từ url php

Al Fohnce

4.07512 Huy hiệu vàng37 Huy hiệu bạc49 Huy hiệu đồng12 gold badges37 silver badges49 bronze badges

Đã trả lời ngày 13 tháng 10 năm 2016 lúc 20:35Oct 13, 2016 at 20:35

JamesjamesJames

Huy hiệu 411 Đồng1 bronze badge

1

Hãy thử mã sau:

Cho Php 5.4.0 trở lên:

$filename = basename(parse_url('http://learner.com/learningphp.php?lid=1348')['path']);

Cho phiên bản PHP <5.4.0

$parsed = parse_url('http://learner.com/learningphp.php?lid=1348');
$filename = basename($parsed['path']);

Đã trả lời ngày 7 tháng 1 năm 2017 lúc 8:38Jan 7, 2017 at 8:38

SultansultanSultan

6095 Huy hiệu bạc13 Huy hiệu Đồng5 silver badges13 bronze badges

1

Câu trả lời ở đó cho rằng bạn biết rằng URL đến từ một yêu cầu, điều này rất có thể không. Câu trả lời tổng quát sẽ giống như:

  $url = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png';
        $file = file_get_contents($url); // to get file
        $name = basename($url); // to get file name
        $ext = pathinfo($url, PATHINFO_EXTENSION); // to get extension
        $name2 =pathinfo($url, PATHINFO_FILENAME); //file name without extension
0

Ở đây nó chỉ đi theo con đường cơ sở, và chia ra và bỏ qua bất cứ điều gì? và sau.

Đã trả lời ngày 28 tháng 4 năm 2021 lúc 3:57Apr 28, 2021 at 3:57

Jeremy L.Jeremy L.Jeremy L.

8066 Huy hiệu bạc14 Huy hiệu Đồng6 silver badges14 bronze badges

0

  $url = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png';
        $file = file_get_contents($url); // to get file
        $name = basename($url); // to get file name
        $ext = pathinfo($url, PATHINFO_EXTENSION); // to get extension
        $name2 =pathinfo($url, PATHINFO_FILENAME); //file name without extension
1

Đã trả lời ngày 21 tháng 10 năm 2011 lúc 17:24Oct 21, 2011 at 17:24

MobmobMob

10,8K6 Huy hiệu vàng40 Huy hiệu bạc57 Huy hiệu đồng6 gold badges40 silver badges57 bronze badges

Sử dụng chức năng này:

  $url = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png';
        $file = file_get_contents($url); // to get file
        $name = basename($url); // to get file name
        $ext = pathinfo($url, PATHINFO_EXTENSION); // to get extension
        $name2 =pathinfo($url, PATHINFO_FILENAME); //file name without extension
2

Đã trả lời ngày 30 tháng 7 năm 2017 lúc 20:46Jul 30, 2017 at 20:46

Hướng dẫn get file name from url php - lấy tên tệp từ url php

Có thể tôi đến muộn

  $url = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png';
        $file = file_get_contents($url); // to get file
        $name = basename($url); // to get file name
        $ext = pathinfo($url, PATHINFO_EXTENSION); // to get extension
        $name2 =pathinfo($url, PATHINFO_FILENAME); //file name without extension
3

Đã trả lời ngày 8 tháng 8 năm 2020 lúc 11:53Aug 8, 2020 at 11:53

Làm thế nào để lấy tên tệp bằng PHP?

Bạn có thể sử dụng hàm basename () để lấy tên tệp mà không có đường dẫn; hoặc basename (đường dẫn, hậu tố) để lấy tên tệp mà không cần mở rộng. Ví dụ,use function basename() to get the filename without the path; or basename(path, suffix) to get the filename without the extension. For example,

Làm thế nào để lấy tên tệp từ đường dẫn bằng PHP?

Hàm basename () là một hàm sẵn có trả về tên cơ sở của tệp nếu đường dẫn của tệp được cung cấp dưới dạng tham số cho hàm basename (). Cú pháp: $ fileName = basename (đường dẫn, hậu tố); Đường dẫn là một trường bắt buộc chỉ định đường dẫn cần được kiểm tra.$filename = basename(path, suffix); The path is a required field that specifies the path which is to be checked.

__ Tệp __ trong PHP là gì?

__File__ chỉ đơn giản là tên của tệp hiện tại.RealPath (Dirname (__ File__)) có tên của thư mục mà tệp đang ở - về bản chất, thư mục mà ứng dụng được cài đặt.the name of the current file. realpath(dirname(__FILE__)) gets the name of the directory that the file is in -- in essence, the directory that the app is installed in.

Tại sao sử dụng basename trong PHP?

Hàm basename là hàm PHP sẵn có chủ yếu được sử dụng để trả về tên cơ sở của một tệp đã cho trên một điều kiện nhất định khi đường dẫn của tệp mong muốn được đưa ra dưới dạng tham số bên trong hàm tên cơ sở.tức là, nó cho tên dấu vết của đường dẫn.Cú pháp: chuỗi basename ($ path, $ hậu tố)to return the base name of a given file on a certain condition when the path of the desired file is given as a parameter inside the base name function. i.e., it gives the trailing name of the path. Syntax: String basename ( $ path , $ suffix )