Welcome Guest, Not a member yet? Register   Sign In
Need help serving images from above root folder.
#1

I'm new with codeigniter and I'm facing problem trying to serve images from an above root folder in codeigniter.
There are few comments about it on the web but none seems to be working for me exept a proxy script.

Is there a working way to use a controller/routes method instead?

I appreciate any help.
Thank You!.
Reply
#2

Structure of Directories:

/myApp
/.htaccess "Example of .htaccess"
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /myApp/index.php?/$1 [L]
</IfModule>

/myApp/config.php "Remove index.php of index_page"
$config['index_page'] = '';

/uploads/photos/img1"My files here"
/uploads/photos/img2"My files here"
/uploads/photos/img...n


The base_url() is equal to "/myApp"
In my file View
<?php echo "<img src='".base_url()."/uploads/photos/img1.jpg'>";?>
<?php echo "<img src='".base_url()."/uploads/photos/img2.jpg'>";?>

I hope you help
Regards
Reply
#3

(11-22-2014, 09:35 AM)Adrian Miranda Wrote: Structure of Directories:

/myApp
/.htaccess "Example of .htaccess"
  <IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase /

   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*)$ /myApp/index.php?/$1 [L]
  </IfModule>

/myApp/config.php "Remove index.php of index_page"
   $config['index_page'] = '';

/uploads/photos/img1"My files here"
/uploads/photos/img2"My files here"
/uploads/photos/img...n


The base_url() is equal to "/myApp"
In my file View
<?php echo "<img src='".base_url()."/uploads/photos/img1.jpg'>";?>
<?php echo "<img src='".base_url()."/uploads/photos/img2.jpg'>";?>

I hope you help
Regards



I appreciated your responce Thanks!

It seems I am missing something. I follow your suggestion and does not work for me serving images from above root. It also disable the images proxy scirp I have.

Here are my settings:

-----main site: /opt/lampp/htdocs/codeigniter
-----above root images folder: /opt/lampp/htdocs/uploads/page_images/

-----.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /

RewriteRule ^show-image/page-image/([^/]+)/?$ /opt/lampp/htdocs/uploads/proxy_script.php?file=$1

RewriteCond $1 !^(index\.php|assets|images|uploads|robots\.txt|sitemap\.xml)$ [NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>


-------the codeigniter/application/config/config.php
$config['index_page'] = '';

------- image links at the codeigniter/application/views
<a href="<?php echo base_url(); ?>show-image/page-image/img1.jpg" >

------ the images proxy script at the /opt/lampp/htdocs/uploads/   folder
/*
* ------------------------------------------------------
*  Image folder (above root)
* ------------------------------------------------------
*/
$image_folder_path = "/opt/lampp/htdocs/uploads/page_images/";

/*
* ------------------------------------------------------
*  Clean Image Name
* ------------------------------------------------------
*/
function cleanImageName($string)
{

// allow letters numbers and .
return preg_replace( "/[^A-Za-z0-9\_\.]/i", "", $string );

}


/*
* ------------------------------------------------------
*  Get the image name from the query string
*  and make sure it's not trying to probe your file system
* ------------------------------------------------------
*/
if (isset($_GET['file']) && basename(urldecode($_GET['file']))==$_GET['file'])
{

/*
* ------------------------------------------------------
*  Clean image name.
* ------------------------------------------------------
*/
$realname = cleanImageName(trim($_GET['file']));
$realname = str_replace(" ", "", $realname);

/*
* ------------------------------------------------------
*  Set full image path
* ------------------------------------------------------
*/
$pic = $image_folder_path.$realname;

if (file_exists($pic) && is_readable($pic))
{

/*
* ------------------------------------------------------
*  Get image extension and set its mime type
* ------------------------------------------------------
*/
$ext = strtolower(substr($pic, strrpos($pic, '.') + 0));

switch($ext)
{

case '.jpeg': $mime = 'image/jpeg'; break;
case '.jpg': $mime = 'image/jpeg'; break;
case '.gif': $mime = 'image/gif'; break;
case '.png': $mime = 'image/png'; break;

default: $mime = false;

}

/*
* ------------------------------------------------------
*  If valid set appropiated headers
* ------------------------------------------------------
*/
if ($mime)
{

header('Content-type: '.$mime);
header('Content-length: '.filesize($pic));

$file = @fopen($pic, 'rb');
if ($file)
{

fpassthru($file);
exit;

}

}

}

}

The proxy script is not mine. I found it on the internet here http://foundationphp.com/tutorials/image_proxy.php so the credit is for its creator.

So far this proxy script is working fine serving images form above root.

However I need to manually configure the proxy script's  $image_folder_path variable and have multiple proxy scripts files and rewrite rules on the .htaccess for each section ( pages, gallery, news and so on ).

That is why I am looking a way to do the same thing but using a codeigniter's controller or helper instead. So far I have not been able to serve images from above root using a controller or helper.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB