Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> PHP

PHP में Path से File Name प्राप्त करना


File path से filename (और extension के बिना) निकालने के तरीके।

विधि 1: pathinfo() - सबसे पूर्ण जानकारी

<?php
$path = '/home/user/documents/report.pdf';

$info = pathinfo($path);

print_r($info);
/*
Array
(
    [dirname] => /home/user/documents
    [basename] => report.pdf
    [extension] => pdf
    [filename] => report
)
*/

// Specific elements
echo $info['basename'];   // report.pdf (with extension)
echo $info['filename'];   // report (without extension)
echo $info['extension'];  // pdf
echo $info['dirname'];    // /home/user/documents

विधि 2: basename() - सिर्फ Filename

<?php
$path = '/home/user/documents/report.pdf';

echo basename($path);        // report.pdf
echo basename($path, '.pdf'); // report (extension remove करता है)

// Multiple extensions के लिए
echo basename($path, '.tar.gz'); // report.tar (केवल .gz हटाता है!)

विधि 3: SplFileInfo (Object-Oriented)

<?php
$file = new SplFileInfo('/home/user/documents/report.pdf');

echo $file->getFilename();        // report.pdf
echo $file->getBasename('.pdf');  // report
echo $file->getExtension();       // pdf
echo $file->getPath();            // /home/user/documents
echo $file->getPathname();        // /home/user/documents/report.pdf
echo $file->getRealPath();        // Real path (symlinks resolved)

// Directory iteration के लिए उपयोगी
foreach (new DirectoryIterator('/home/user/docs') as $file) {
    if ($file->isFile()) {
        echo $file->getFilename() . "\n";
    }
}

विधि 4: Manual Parsing (Performance Critical)

<?php
function getFileName($path) {
    $len = strlen($path);
    $end = $len;
    
    // Find last slash
    for ($i = $len - 1; $i >= 0; $i--) {
        if ($path[$i] === '/' || $path[$i] === '\\') {
            $start = $i + 1;
            return substr($path, $start, $end - $start);
        }
    }
    return $path; // No directory separator
}

function getFileNameWithoutExt($path) {
    $name = getFileName($path);
    $dotPos = strrpos($name, '.');
    return $dotPos !== false ? substr($name, 0, $dotPos) : $name;
}

Edge Cases Handle करना

<?php
$paths = [
    '/home/user/file.txt',
    '/home/user/.hidden',
    '/home/user/file.tar.gz',
    '/home/user/file.',
    '/home/user/',
    'file.txt',           // Relative
    'C:\Windows\file.ini' // Windows path
];

foreach ($paths as $path) {
    echo "Path: $path\n";
    echo "  basename: " . basename($path) . "\n";
    echo "  filename: " . (pathinfo($path)['filename'] ?? 'N/A') . "\n";
    echo "  ext: " . (pathinfo($path)['extension'] ?? 'none') . "\n";
    echo "\n";
}

/*
Path: /home/user/file.txt
  basename: file.txt
  filename: file
  ext: txt

Path: /home/user/.hidden
  basename: .hidden
  filename: .hidden (no extension)
  ext: none

Path: /home/user/file.tar.gz
  basename: file.tar.gz
  filename: file.tar (pathinfo only removes last .ext)
  ext: gz

Path: /home/user/
  basename: user (directory name!)
  filename: user
  ext: none
*/

Windows Paths के लिए

<?php
// DIRECTORY_SEPARATOR उपयोग करें cross-platform के लिए
$path = 'C:\Users\Name\file.txt';

// Normalize करें
$normalized = str_replace('\\', '/', $path);
// या
$normalized = str_replace(DIRECTORY_SEPARATOR, '/', $path);

echo basename($normalized); // file.txt

// PHP 8.0+: pathinfo() handles both separators
$info = pathinfo($path); // Works on Windows too
  1. Python 3 में Tkinter filedialog से स्ट्रिंग कैसे प्राप्त करें? Python 3 में Tkinter filedialog से स्ट्रिंग कैसे प्राप्त करें?

    Tkinter एप्लिकेशन में फाइल सिस्टम के साथ इंटरैक्ट करने के लिए, आप Tkinter filedialog मॉड्यूल का उपयोग कर सकते हैं। यह सिस्टम में फ़ाइलों से निपटने का एक तरीका प्रदान करता है। filedialog मॉड्यूल एप्लिकेशन के लिए विभिन्न फ़ाइल डायलॉग बनाने में डेवलपर्स की मदद करने के लिए कई बिल्ट-इन फ़ंक्शन प्रदान करत

  1. - पायथन में वर्तमान फ़ाइल की डायरेक्टरी का फुल पथ कैसे प्राप्त करें?

    वर्तमान फ़ाइल का पूरा पथ जानने के लिए os मॉड्यूल का उपयोग करें। उदाहरण >>> import os >>> print(os.path.realpath(__file__)) /home/ayush/qna/path.py

  1. Excel में पाथ से फाइल नाम निकालें: 6 सरल, सिद्ध तरीके Excel में पाथ से फाइल नाम निकालें: 6 सरल, सिद्ध तरीके

    Microsoft Excel में काम करते समय कभी-कभी हमें दिए गए पाथ से फाइलनाम निकालने की आवश्यकता होती है ताकि हम अधिक प्रभावी ढंग से काम कर सकें। यह कठिन लग सकता है क्योंकि पाथ बड़ी या छोटी लंबाई का हो सकता है। आज इस लेख में, मैं आपके साथ शेयर कर रहा हूं कि Excel में पाथ से फाइलनाम कैसे प्राप्त करें। Excel