The Must Have for WordPress
The Must Have for WordPress
×
  • Home
December 5, 2019
PHP WordPress

Get the absolute File Path (Document Root-Path) in WordPress

by Martin Jost
No comments
1 min Reading Time
1.54 K Views
5 Score
3 Likes
Share on Facebook Tweet on Twitter Share on Linkedin Pin on Pinterest Send via E-Mail Send via WhatsApp

Wenn man als Webentwickler mit Dateien arbeitet, die auf dem Server liegen und diese innerhalb eines WordPress-Templates aufrufen möchte, benötigt man den absoluten Pfad zu einer Datei. Zum Beispiel kann dies hilfreich sein, wenn man bestimmte Dateieigenschaften oder Dateiinhalte auslesen muss.

Wie sieht ein absoluter Server-Pfad zu einer Datei aus?

Dieses Beispiel zeigt in welchem Server-Verzeichnis sich eine Datei innerhalb von WordPress befindet:

  • /homepages/xx/dxxxxxxxxx/htdocs/domain-order/wordpress-installation/meine-datei.pdf

Eine einfache PHP-Methode den absoluten Hauptpfad /homepages/xx/dxxxxxxxxx/htdocs/ des Servers zu erhalten, ist die folgende:

Wie erhalte ich den genauen Root-Pfad einer Datei?

WordPress selbst bietet für diesen Fall keine eigene Funktion, wenn wir als Grundlage nur den URL-Pfad einer Datei wissen, also “https://domain.de/meine-datei.pdf”. Doch es gibt eine Möglichkeit, alleine anhand der Datei-URL den gesamten absoluten Pfad zu erhalten, um bspw. mit PHP-Funktion wie file_get_contents(); zu arbeiten.

Die folgende PHP-Funktion ist dabei sehr hilfreich den absoluten Server-Pfad einer WordPress-Datei herauszufinden, wenn wir nur mit dem URL-Pfad einer Datei, als Ausgangswert arbeiten können.

Wie verwenden wir diese Funktion?

Nun erhalten wir ganz einfach den absoluten Dokumenten-Pfad einer Datei innerhalb von WordPress, indem wir die Datei-URL als Wert in der oben beschriebenen PHP-Funktion wordpress_get_full_path_of_url( 'https://domain.de/meine-datei.pdf' ); nutzen.

Last updated: December 5, 2019

Get the absolute File Path (Document Root-Path) in WordPress Score: 5 / 5 (1vote)
Share on Facebook Tweet on Twitter Share on Linkedin Pin on Pinterest Send via E-Mail Send via WhatsApp
The Author
Published by
Martin Jost

View all posts by Martin Jost

Related Posts
A
WordPress Add custom classes to the WordPress BODY tag

Anyone who develops their own theme with a customizable layout or settings a theme needs to add ...

Read More
G
PHP Get Metadata from WordPress Video and Audio files

In addition to images, videos and audio files are other formats that we can upload to WordPress ...

Read More
M
Javascript Minify and compress Javascript files in PHP

Loading time is an important factor in search engine optimization and for the user experience. E ...

Read More
Read more
C
Previous Post
Create a connection to WordPress MySQL database in PHP
T
Next Post
Trim a PHP string before the last special character
Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Imprint
  • Privacy Policy
© 2016-2022 - wphave - The Must Have for WordPress
Login

Lost your password?

RequiredEssential cookies enable basic functions and are necessary for the proper functioning of the website.
  • Name: Cookie Status
  • Provider: Owner of this website
  • Purpose: Is necessary to check whether the user's web browser allows cookies.
  • Privacy Policy: https://wphave.com/privacy-policy/
  • Cookie Name(s): cookie_status
  • Duration: 1 Minute
  • Name: Cookie Consent
  • Provider: Owner of this website
  • Purpose: Is necessary to check whether the user agrees to allow opt-in cookies or not.
  • Privacy Policy: https://wphave.com/privacy-policy/
  • Cookie Name(s): cookieconsent_analytics, cookieconsent_marketing, cookieconsent_status
  • Duration: 1 Year
  • Name: Like Button
  • Provider: Owner of this website
  • Purpose: Is necessary to check whether the user has already gave a like for a specific post.
  • Privacy Policy: https://wphave.com/privacy-policy/
  • Cookie Name(s): like_btn_{POST_ID}
  • Duration: 21 Days
  • Name: Post Rating
  • Provider: Owner of this website
  • Purpose: Is necessary to check whether the user has already rate a specific post.
  • Privacy Policy: https://wphave.com/privacy-policy/
  • Cookie Name(s): rating_{POST_ID}
  • Duration: 7 Days
AnalyticsStatistics cookies collect information anonymously. This information helps us to understand how our visitors use our website.
  • Name: Google Analytics
  • Provider: Google LLC
  • Purpose: Google cookie for website analysis. Generates statistical data on how the visitor uses the website.
  • Privacy Policy: https://policies.google.com/privacy
  • Cookie Name(s): _ga, _gat, _gid
  • Duration: 2 Years