Drupal 7 で files ディレクトリーへのパスを取得する

プログラム的に files ディレクトリーへのパスを取得するには、以下のようにします。

  • パブリックファイル

    dsm(variable_get('file_public_path', NULL));
  • プライベートファイル

    dsm(variable_get('file_private_path', NULL));
  • とっても簡単なことですけど、api.drupal.org を調べてもすぐには出てこなかったので、覚え書き。

    Comments

    file_directory_path

    こんにちは、pixtureと申します。Drupal 7関係のサーチしていてたまたまこのページに来ました。varibale_get()では必ずしもファイルディレクトリのパスは取得できないと思いますので、正しくは以下のようにするほうがよいかと思います。$public_path = drupal_realpath('public://'); $private_path = drupal_realpath('private://'); privateのほうはテストしたことがないので設定していない場合に何が戻ってくるかはわかりませんがパブリックの方はこれで動作すると思います。デフォルト以外のファイルディレクトリを指定した場合にのみvaribaleテーブルにエントリーが作成されるようになっていると思います。 この戻りパスはシステムのルートからの絶対パス(例:/www/mysite/html/sites/default/files )になりますので、使う用途によってDrupalのルートからのパスに加工したりする必要があるかと思います。

    Post new comment

    The content of this field is kept private and will not be shown publicly.
    • Web page addresses and e-mail addresses turn into links automatically.
    • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
    • Lines and paragraphs break automatically.

    More information about formatting options

    By submitting this form, you accept the Mollom privacy policy.