Hướng dẫn php get file name

Hi 🤓 Cảm ơn bạn đã ghé thăm blog này, nếu những bài viết trên blog giúp ích cho bạn. Bạn có thể giúp blog hiển thị quảng cáo bằng cách tạm ngừng ad blocker 😫 và để giúp blog duy trì hoạt động nếu bạn muốn.
Cảm ơn bạn!

Khi upload file với Laravel chắc hẳn các bạn muốn lấy tên file đã tải lên. Để lấy tên file rất đơn giản. Các bạn chỉ cần làm như sau:

$image = $request->file('thumbnail');
$file = $image->getClientOriginalName(); // lấy tên file bao gồm extension
echo $file; // tên file. đuôi exe,jpg,pdf...

Get file name không chứa extension:

$image = $request->file('thumbnail');
$file = $image->getClientOriginalName();
$fileName = pathinfo($file, PATHINFO_FILENAME);
echo $fileName; // tên file

Các hàm hữu ích khác khi làm việc với file trong Laravel:


namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UploadFileController extends Controller
{
    function upload(Request $request)
    {
        //Kiểm tra file
        if ($request->hasFile('thumbnail')) {
            $file = $request->file('thumbnail');

            //Lấy Tên file
            echo 'Tên File: ' . $file->getClientOriginalName();

            //Lấy đuôi file
            echo 'Đuôi file: ' . $file->getClientOriginalExtension();

            //Lấy đường dẫn tạm thời 
            echo 'Đường dẫn tạm: ' . $file->getRealPath();

            //Lấy kích cỡ của file (đơn vị bytes)
            echo 'Kích cỡ file: ' . $file->getSize();

            //Lấy kiểu file
            echo 'Kiểu file: ' . $file->getMimeType();
        }
    }
}

Có thể bạn thích ⚡

homiedev

About Me

Hi, I'm @devnav. Một người thích chia sẻ kiến thức, đặc biệt là về Frontend 🚀. Trang web này được tạo ra nhằm giúp các bạn học Frontend hiệu quả hơn 🎉😄.

Chúc các bạn tìm được kiến thức hữu ích trong blog này 😁😁.

In this article, we will see how to get the file name from the path in PHP, along with understanding its implementation through the examples. We have given the full path & we need to find the file name from the file path. For this, we will following below 2 methods:

  • Using the basename() function
  • Using the pathinfo( ) Function

Input : path = /testweb/var/www/mywebsite/htdocs/home.php
Output : home.php

Input : path = /testweb/var/www/mywebsite/htdocs/abc.txt
Output : abc.txt

We will understand both functions with the help of examples.

Method 1: Using basename() function:

The basename() function is an inbuilt function that returns the base name of a file if the path of the file is provided as a parameter to the basename() function. 

Syntax:

$filename = basename(path, suffix);

The path is a required field that specifies the path which is to be checked. The suffix is an optional field that specifies a file extension. If the filename has this file extension, the file extension will not show.

Example: This example describes the use of the basename() function that returns the base name of the file.

PHP

  $path = "/testweb/var/www/mywebsite/htdocs/home.php";

  $file1 = basename($path);

  $file2 = basename($path, ".php");

  echo $file1 . "\n";

  echo $file2;

?>

Output:

home.php
home

Method 2: Using pathinfo() function:

The pathinfo() is an inbuilt function that is used to return information about a path using an associative array or a string ie., It will create an array with the parts of the path we want to use.

Syntax:

$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.

PHP

  $myFile = pathinfo('/usr/admin/config/test.php');

  echo $myFile['basename'], "\n";

?>

Output:

test.php