Codeigniter access forbidden to image folder at project root - php

I'd like to put my images/assets in folders at the same level as my application directory, but every I try to link to an image, I get this error:
Forbidden
You don't have permission to access (my base url)images/myimage.jpg on this server
This how I'm trying to access the image in my view:
<img src="<?php echo base_url(); ?>images/myimage.jpg">
Is this an issue with my routes.php? I haven't really touched it, nor have I created a .htaccess file.
If the issue is that I haven't told it how to handle the base_url + some_asset_folder, where and how do I go about configuring that? I only know how to tell the routes to use a particular controller class and method, not to access a particular directory.
I'm new to PHP frameworks and a new to CodeIgniter – any help is much appreciated! Thanks so much.

The best way to keep your images, stylesheets, javascript, uploads etc. files are separate from the application. Create a folder assets in the root folder that contains application and systems.
So it'll contain the folders application, assets, system etc.
Add folders inside assets -- images, css, js, uploads etc.
and you can access it using
<img src='<?= base_url() ?>assets/images/image.jpg' />
This would probably be the best way to do it. This is what I've been doing for my applications, so for me this is the best way. Hope this helps.

Please check your .htaccess file that might be causing problems .
Allow your file types in .htaccess .

If you have CI installed outside of your public http web folder, you will not be able to access your images directory through a browser due to server permissions. One way around this would be to create a controller to fetch and serve the image file.

Related

Where place data file in Symfony 4?

I have a web application and I want to save data in a file instead of in the database.
My doubt is, where I should save the file? If I save it under the public directory, I could refer them with the assets to make the correct URL, but in the public directory, any person could see the file writing the complete URL. This shouldn't be because de data is reserved.
I think about the var directory, but I couldn't use the assets to make the correct URL to get or manage the files.
Where I should save the files and how I get/manage them?
If the data-file is local and hosted on the server's file system, the most logical place to store something like that would be the var directory. Although nothing stops you from creating your own "var-like" directory and naming any way you like it. E.g. data. But I would follow the conventions and create var/data, easier for everyone.
That you can't use the asset() function is irrelevant, because a data-file is not a web asset, but an infrastructure concern.
A file like this should not have a URL at all, but you would only access it through it's file system path.

I can' upload file on my shared folder using laravel

I have a laravel project that i hosted on a shared host. using the system i saw on Laravel Deploy Instruction
So am suppose to upload image, -- user's image. To the public_html folder but since am not in the public_html so the users can't upload their image. since my code lie out side the public_html folder and it doesn't allow write permission for general users. so i tried uploading it to 'storage folder' (ie the my laravel project folder, that is in the sane level as the www 'public_html' folder.) it works i can see the image in the storage folder but i cant access because it is behind the public_html folder. and whatever can't be point to. Example project/storage/app/public is the folder that has the user image if i point to it in <img src="project/storage/app/public/user.png" > it wont display because the browser looks for project/storage/app/public inside the public_html folder whereas it is outside the public_html. So can someone touch on this. Thanks guys. Or is htaccess use in this case?
you need to create symlink to Your storage folder .
please see this answer:
Laravel 5 - How to access image uploaded in storage within View?
the above method given by the link works for offline however not all shared hosting site support artisan Laravel sysmlink. so the absolute and best way to do this is with php symlink() function From PHP MANUAL
so just add this code to your web.php ie project\route\web.php
Route::get('/symlink', function()
{
if(symlink('/home/username/projectfolder/storage/app/public', '/home/username/public_html/storage')){
echo "We rock yea stackoverflow!!!! best programming software";}
});
so the username is the name of your home dir

accessing files outside baseUrl with Yii

I'm using the Yii framework. This is my directory structure:
folder
images
yii-framework
mysite
index.php
protected
assets
.
'
'
"folder" is the webroot.My subdomain a.subdomain.com points to mysite. The problem is I want to use the images from the "image" folder in "mysite".
The following code works well for localhost but not on the webserver:
echo "<img src='".Yii::app()->request->baseUrl."/../images/image1.png' height='500' width='500'>";
Actually Yii makes this relatively simple provided you have the right setup. If you have the websites webroots running on the same file system, you can pre publish them with their true file system paths and Yii will pass you a publicly available URL to serve them with having copied them to the sites assets folder.
$url = Yii::app()->assetManager->publish('/path/to/webroot/images/imageIWant.jpg')
echo "<img src='".Yii::app()->assetManager->baseUrl.'/'.$url."' height='500' width='500'>";
That should do it for you, make sure to prepend the assetManager's baseURL too just in case it changes.
EDIT:
If you do decide this route, this is worth a read, actually the entire wiki is worth a read :p
Understanding "Assets"
You can only use:
<img src="/images/image1.png"/>
This should work. No need to give the absolute URL.
This is by definition not possible, neither with Yii nor without. There is no command that can make a browser get an image outside the web root.
You would have to build a script that loads the image for you, and passes it through. That is a very inefficient way to serve an image, though. It's probably much better to move the images into the web root.

copying a zendframework project in to it's subfolder

Hi
I have a zendframework project. Now it is running in my domain (http://www.mydomain.com).
I want to modify some portion of the site. So I decided to copy all the files and folders to its sub folder (http://www.mydomain.com/zendwork). Is it possible?
I heard that there is a problem in the relative path? Thus so how I change the entire path in an easiest way?
Thanks in advance
Only the files under the "public" directory need be under your document root. The rest of the codebase ("application", "library", etc) can exist anywhere.
The APPLICATION_PATH constant and include path defined in the public/index.php file dictate where to find the rest of the application.
The other consideration is to make use of the BaseUrl view helper when linking to JavaScript, CSS, image and other "static" assets in your views, eg
// view.phtml
<img src="<?php echo $this->baseUrl('/images/foo.jpg') ?>" alt="foo">
To move your application then only involves moving the "public" contents and editing index.php.
Yes.its possible but u need to set the path in .htacess file.

Absolute Relative path issues in referencing web assets - need help - php related

Hi guys I'm in a bit of a pickle here now. Well to start with I built a simple CMS in PHP with an admin panel the directory structure is like this:
root/
->admin/
->images/
It worked fine as is however the client requirements changed and they wanted that instead of having to access the admin folder as a folder within the root it be accessed as a web subdomain. so www.site.com/admin becomes admin.site.com
However this has terrible messed up and destroyed practically all the referencing I had done. Like I upload images on the CMS - however now uploading on ../images doesn't work as its now under a subdomain and I'm all messed up in trying to relatively reference images from there too. I've been trying to hack away at my config file for weeks and can't get to fix this :( - help please - on the front end the site is o.k. but my admin section is all messed up :(
I'm using PHP and MySQL.
Sounds like you've learned how toxic relative paths can be.
Possible quick fix: what happens if you copy/symlink/alias admin.domain.com/images to point at the same images folder that lives on your front-end site? I think that extra "../"es will basically be ignored.
More permanently, and in general, don't use relative paths. They will cause you nothing but pain. A couple of strategies:
1) Define some constant that points at the right location for images, css, etc:
define('IMG_DIR','/images');
define('CSS_DIR','/images');
// ... some time later
echo '<img src="' . IMG_DIR . '/myimage.jpg'"/>';
2) Much better: just maintain one constant that tells you where your application lives.
define('APP_ROOT','/myapp'); //could be chanted to just '/' if it doesn't live in some folder on the server
// ... later that day ...
echo '<img src=\"' . APP_ROOT . '/images/myimage.jpg"/>';
// ... or maybe you need to link to a logout script?
echo 'Log Out';
It's important to assume you application might need to run from the root ("/") or some directory on the server, etc.
The same goes for any filesystem operations you might do purely on the server side. Use absolute filesystem paths. If your main application has a script like "config/config.php", you could stick this at the top:
define('APP_FS_ROOT',realpath(dirname(__FILE__) . '/..'));
Assuming both the frontend and the admin are on the same file system, you will need to use absolute paths for everything in the admin. In the admin's config create a define that maps to the frontend's physical upload/image folder. For example, from the fontend you can access uploads folder with the relative path ./upload but from the admin.example.com site you will be required to use the absolute path /user/example.com/upload.
The fontend's config would look like (www.site.com/config.php):
define("UPLOAD_FOLDER", "./uploads");
define("WEB_UPLOAD_FOLDER", "/uploads");
The admin's config would look like (admin.site.com/config.php):
define("UPLOAD_FOLDER", "/user/site.com/upload");
define("WEB_UPLOAD_FOLDER", "http://www.site.com/uploads");
Then both the frontend and admin would reference the physical folder with:
$filename = UPLOAD_FOLDER . "/myupload.mp3";
And to create hyperlinks to the upload you would use this:
My Upload
Another possible solution would be to define a directory alias in apache for the directories you've moved.
Lets say your sub domain root is
/subdomains/images
<VirtualHost>
...
Alias /images "/subdomains/images"
...
</VirtualHost>
Both www.yourDomain.com/images and images.yourDomain.com would load the same files.
Or, if your using linux, a symlink could accomplish the same thing.

Categories