PHP ile tam yolu dosya adını almak nasıl?

9 Cevap php

Örneğin, nasıl Output.map

from

F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map

PHP ile?

9 Cevap

Sen basename arıyoruz.

PHP manuel örnek:

<?php
$path = "/home/httpd/html/index.php";
$file = basename($path);         // $file is set to "index.php"
$file = basename($path, ".php"); // $file is set to "index"
?>

Ben fonksiyonunu PATHINFO wich kullanmak için yolun parçaları ile bir dizi oluşturur kullanarak yaptık! Örneğin bunu yapabilirsiniz:

<?php
$xmlFile = pathinfo('/usr/admin/config/test.xml');

function filePathParts($arg1) {
echo $arg1['dirname'], "\n";
echo $arg1['basename'], "\n";
echo $arg1['extension'], "\n";
echo $arg1['filename'], "\n";
}

filePathParts($xmlFile);
?>

Bu dönecektir:

/usr/admin/config
test.xml
xml
test

The use of this function is available since PHP 5.2.0! Then you can manipulate all the parts as you need. For example to use the full path you can do this:

$fullPath = $xmlFile['dirname'].'/'.$xmlSchema['basename'];

Ben de bu yöntemi kullanabilirsiniz, bu cevabı çok yararlı olmuştur diliyorum. Iyi günler ;)

basename () bir hata olduğunda Çince gibi süreçler Asya karakterleri.

Ben bunu kullanın:

function get_basename($filename)
{
    return preg_replace('/^.+[\\\\\\/]/', '', $filename);
}

Kullanmak basename() function olabilir

Uri dan tam dosya adını almak için. ben bu yöntemi kullanır.

<?php
$file1 =basename("http://localhost/eFEIS/agency_application_form.php?formid=1&task=edit") ;

//basename($_SERVER['REQUEST_URI']); //or use this to get the uri dynamically.

echo $basename = substr($file1, 0, strpos($file1, '?'));
?>

Basename benim için çalışmıyor. Ben bir formu (dosyası) dosya var. Google chrome (mac os x lion) üzerine dosya değişken olur:

c:\fakepath\file.txt

Ben kullandığınızda:

basename($_GET['file'])

döndürür:

c:\fakepath\file.txt

Yani bu durumda güneş Junwen cevap iyi çalışır.

Firefox dosyanın değişken bu fakepath içermez.

aldım ama aynı zamanda (bir değil burada) başka bir tane bulmak, bu yüzden
ilanıyla bir cevap geldi

Şimdi ile SplFileInfo

SplFileInfo The SplFileInfo class offers a high-level object oriented interface to information for an individual file.

Ref, http://php.net/manual/en/splfileinfo.getfilename.php

$info = new SplFileInfo('/path/to/foo.txt');
var_dump($info->getFilename());

o / p: string (7) "foo.txt"

Bu deneyin:

echo basename($_SERVER["SCRIPT_FILENAME"], '.php')