0

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
3
  • 1
    Why would you include an image? Commented Aug 22, 2012 at 15:30
  • I'm guessing that the image file name in the question is just an example, and that it could be extended to any file type (such as other *.php files) Commented Aug 22, 2012 at 15:57
  • Yeh sorry I didnt mean to put .jpg I will edit it now Commented Aug 24, 2012 at 10:56

4 Answers 4

6

It should be just as easy as

<?php
include('pictures/face.jpg');
?>
Sign up to request clarification or add additional context in comments.

Comments

4

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");

1 Comment

A note of warning when using $_SERVER: It returns "The document root directory under which the current script is executing, as defined in the server's configuration file.", so in some cases might not be entirely what you'd expect it to be
1

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.

Comments

0

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');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.