Hàm dirname trong php

     

mydomain name.com ->Folder-A ->Folder-BI have a string from Database that is "../Folder-B/image1.jpg", which points to lớn an image in Folder-B.

Inside a script in Folder-A, I am using dirname(FILE) khổng lồ fetch the filename và I getmytên miền.com/Folder-A. Inside this script, I need to get a string that says "mydomain.com/Folder-B/image1.jpg. I tried

$path=dirname(__FILE__)."/"."../Folder-B/image1.jpg";This shows up as mydomain name.com%2FFolder-A%2F..%2FFolder-B%2Fimage1.jpg

This is for a facebook nói qua button, và this fails to lớn fetch the correct image. Anyone know how to get the path correctly?


php dirname
Share
Follow
edited Jun 19 "12 at 11:19
*

CBroe
82k99 gold badges8282 silver badges132132 bronze badges
asked Jun 19 "12 at 5:18
*

aVCaVC
2,05622 gold badges1919 silver badges4444 bronze badges
1
Add a phản hồi |

9 Answers 9


Active Oldest Votes
222
For PHP. $upOne = realpath(dirname(__FILE__) . "/..");In PHPhường 5.3 to lớn 5.6 use:

$upOne = realpath(__DIR__ . "/..");In PHPhường >= 7.0 use:

$upOne = dirname(__DIR__, 1);
Share
Follow
edited Jan 14 "19 at 18:36
answered Jun 19 "12 at 5:22

*

PetahPetah
42.8k2626 gold badges149149 silver badges203203 bronze badges
Is this answer outdated?
Yes | No
5
Add a phản hồi |
21
If you happen to lớn have sầu php 7.0+ you could use levels.

dirname( __FILE__, 2 ) with the second parameter you can define the amount of levels you want to lớn go baông xã.

Bạn đang xem: Hàm dirname trong php

http://php.net/manual/en/function.dirname.php


Share
Follow
edited Jul 4 "18 at 9:39
answered Dec 22 "16 at 9:52
*

Bas van DijkBas van Dijk
72966 silver badges2020 bronze badges
Is this answer outdated?
Yes | No
Add a comment |
18
Try this

dirname(dirname( __ FILE__))Edit: removed "./" because it isn"t correct syntax. Without it, it works perfectly.


Share
Follow
edited May 5 "16 at 14:07
*

Daan van den Bergh
25422 silver badges1111 bronze badges
answered Jun 19 "12 at 5:27
AbhishekAbhishek
68133 silver badges1414 bronze badges
Is this answer outdated?
Yes | No
Add a phản hồi |
6
You could use PHP"s dirname function..That will give you the name of the parent directory of __DIR__, which stores the current directory.


Share
Follow
answered Jun 19 "12 at 5:23
ShaneShane
1,8061414 silver badges3131 bronze badges
Is this answer outdated?
Yes | No
5
Add a phản hồi |
1
You can use realpath lớn remove unnessesary part:

// One level upemang đến str_replace(realpath(dirname(__FILE__) . "/.."), "", realpath(dirname(__FILE__)));// Two levels etc.eđến str_replace(realpath(dirname(__FILE__) . "/../.."), "", realpath(dirname(__FILE__)));On windows also replace with / if need that in URL.

Xem thêm: Lỗi Kết Nối Tới Cơ Sở Dữ Liệu Wordpress (Cực Hay), Nguyên Nhân Và Cách Xử Lý


Share
Follow
answered Jul 1 "13 at 10:19
web1_user133408web1_user133408
Is this answer outdated?
Yes | No
Add a bình luận |
1
dirname(__DIR__,level);dirname(__DIR__,1);cấp độ is how many times will you go back to lớn the folder


Share
Follow
answered Jan 17 "19 at 11:01
dılo sürücüdılo sürücü
2,00111 gold badge1212 silver badges1919 bronze badges
Is this answer outdated?
Yes | No
Add a bình luận |
0
One màn chơi up, I have used:

str_replace(basename(__DIR__) . "/" . basename(__FILE__), "", realpath(__FILE__)) . "/required.php";or for php str_replace(basename(dirname(__FILE__)) . "/" . basename(__FILE__), "", realpath(__FILE__)) . "/required.php";
Share
Follow
answered Mar 3 "15 at 20:37
Calin RusuCalin Rusu
16711 silver badge55 bronze badges
Is this answer outdated?
Yes | No
Add a bình luận |
0
To Whom, deailing with chia sẻ hosting environment & still chance to lớn have Current PHP less than 7.0 Who does not have dirname( __FILE__, 2 ); it is possible khổng lồ use following.

function dirname_safe($path, $level = 0) $dir = explode(DIRECTORY_SEPARATOR, $path); $level = $cấp độ * -1; if($cấp độ == 0) $cấp độ = count($dir); array_splice($dir, $level); return implode($dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;print_r(dirname_safe(__DIR__, 2));
Share
Follow
answered Oct 29 "18 at 7:21
Deniz PorsukDeniz Porsuk
49411 gold badge66 silver badges1717 bronze badges
Is this answer outdated?
Yes | No
Add a comment |
-2
I use this, if there is an absolute path (this is an example):

$img = imagecreatefromjpeg($_SERVER<"DOCUMENT_ROOT">."https://nguyenminhchau.com/dirname-file-trong-php/imager_6_1714_700.jpg");if there is a picture to show, this is enough:

echo("

*
");
Share
Follow
edited Jun 19 "12 at 11:31
answered Jun 19 "12 at 11:25
web1_user669677web1_user669677
Is this answer outdated?
Yes | No
Add a bình luận |

Your Answer


Thanks for contributing an answer to Staông xã Overflow!

Please be sure khổng lồ answer the question. Provide details and nói qua your research!

But avoid

Asking for help, clarification, or responding to lớn other answers.Making statements based on opinion; baông chồng them up with references or personal experience.

To learn more, see our tips on writing great answers.


Draft saved
Draft discarded

Sign up or log in


Sign up using Google
Sign up using Facebook
Sign up using Email và Password
Submit

Post as a guest


Name
Email Required, but never shown


Post as a guest


Name
Email

Required, but never shown


Post Your Answer Disthẻ

By clicking “Post Your Answer”, you agree to lớn our terms of service, privacy policy và cookie policy


Not the answer you're looking for? Browse other questions tagged php dirname or ask your own question.


The Overflow Blog
Featured on Meta
Linked
0
How lớn remove sầu the last directory in the given path using PHP?
0
Wordpress plugin approval standards: ## Calling file locations poorly
1
PHP - It is possible to get the magic constant __FILE__ of the requirer script inside the required script?
0
how lớn link to same folder & different thư mục with html href using header.php template?
0
Custom Page Template is not shown as page section in Wordpress twentyseventeen theme?
1
php rename (move) from one dir khổng lồ another that is two levels back
Related
2773
How can I prsự kiện Squốc lộ injection in PHP?
2732
Deleting an element from an array in PHPhường
1367
How vị I make a redirect in PHP?
1592
startsWith() & endsWith() functions in PHP
1836
How vày I get PHPhường errors lớn display?
2193
How vì you parse and process HTML/XML in PHP?
4722
Reference — What does this symbol mean in PHP?
2660
How bởi vì I check if a string contains a specific word?
2126
How does PHPhường 'foreach' actually work?
2580
Why shouldn't I use mysql_* functions in PHP?
Hot Network Questions more hot questions

Question feed
Subscribe to RSS
Question feed To subscribe to lớn this RSS feed, copy & paste this URL into your RSS reader.


lang-php
Stachồng Overflow
Products
Company
Stack Exchange Network
site thiết kế / logo sản phẩm © 2021 Staông chồng Exchange Inc; web1_user contributions licensed under cc by-sa. rev2021.5.28.39383


Staông xã Overflow works best with JavaScript enabled
*

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and discthất bại information in accordance with our Cookie Policy.