0

How can I access uploaded images/ docs outside my public_html?

for instance, I have this structure,

lib/
upload/ (uploaded images and pdfs)
public/index.php
bootstrap.php

public/index.php,

// Include application bootstrap
require_once dirname(__FILE__) . '/../bootstrap.php';

$app->get('/about', function () use ($app, $log) {
    echo '<p><img src="' . '/../upload/image/cat.jpg' . '"/></p>';
});

// Important: run the app
$app->run();

I can access bootstrap.php via dirname(__FILE__) but I can't do it with the images.

I follow this framework idea from this. But can I store my images and docs outside the public folder?

What about the javascript file?

I notise that most frameworks store these items in the public folder.

oasis
  • 197
  • 2
  • 13

2 Answers2

1

There is a big difference in how you reference the bootstrap.php and the images. The first is loaded by the PHP processor, the latter by the browser. Of course you can open an image file with, for example, fopen() in PHP if you have permissions, but the client side browser is not running on the server.

You either have to provide a script to send the files to the browser, or have the uploaded images that you want to reference in HTML under the document root, or set up an alias on the web server.

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
  • `You either have to provide a script to send the files to the browser, or have the uploaded images that you want to reference in HTML under the document root, or set up an alias on the web server.` how can I do that? – oasis Apr 03 '15 at 10:23
1
<?php
header('Content-type: image/jpg');
readfile('/path/to/upload/image/cat.jpg');

maybe work this

or

<?php
header('Content-Type: image/jpeg');
$_file = 'cat.jpg'; // or $_GET['img']
echo file_get_contents('/upload/image/'.$_file);
?>
Sameed Alam Qureshi
  • 293
  • 1
  • 3
  • 15