user1616846
user1616846

Reputation: 191

php include from a subfolder

I have been using to get my files

<?php include 'example.html'; ?>

however how do you get them if they are in a subfolder: For example

pictures/example.html

Upvotes: 0

Views: 1314

Answers (4)

thesmart
thesmart

Reputation: 3073

If it were a source file, you could do:

include __DIR__ . '/pictures/src.php';

But because you're dealing with a binary file (raw data, ones and zeros) you may want to use the image library:

$resource = imagecreatefromjpeg( string $filename );

See http://www.php.net/manual/en/function.imagecreatefromjpeg.php

Or, if you just want the bytes, use:

$contents = file_get_contents(__DIR__ . '/pictures/src.php');

Upvotes: 0

dsgriffin
dsgriffin

Reputation: 68566

The best practice for this is to define a ABSOLUTE_PATH constant that contains the directory that everything is located under. After that, you can simply copy and paste everything, because it is defining the 'full' path, which doesn't change from directory to directory.

E.g

define("ABS_PATH", $_SERVER['DOCUMENT_ROOT']);

or

define("ABS_PATH", dirname(__FILE__)); // This defines the path as the directory the file is in. Then at any point you can simply do this to include a file

include(ABS_PATH . "/path/to/file");

Upvotes: 4

Undefined
Undefined

Reputation: 11431

All you need to do is include it like this:

<?php
  include('pictures/face.jpg');
?>

But why are you trying to include an image? This should be done through HTML.

Upvotes: 1

Chris Forrence
Chris Forrence

Reputation: 10084

It should be just as easy as

<?php
include('pictures/face.jpg');
?>

Upvotes: 6

Related Questions