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.
Related
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.
i'm trying to make an web site where the user can listen to different wav files.
I made a php script to get all .wav files that i want and i keep their path on an array.
Then im doing this to play the audio:
echo "<a href='teste.wav'>Play sample 1 </a>";
echo "<a href='" .$audios[$id][1] ."'>Play sample 2 </a>";
?>
<script src="http://mediaplayer.yahoo.com/js></script>
On the first case i got the audio on same folder as the script and it works fine. On the second case i have it in a completely different directory and it never finds the file.
I know that the sample 2 will ref to something directly under the current directory where the script is being executed, but i tried to make the $audios[$id][1] like http://home/.../file.wav and still doesn't work.
Any idea how to fix this?
/home/jorge/VOCE/Recordings/-1458206716/Tiago#Aug_31_2012_Tiago_ID_1204811836_prebaseline.wav
Do not use absolute paths. These are invalid outside your local file system. You have to use relative paths so browser can build request pointing to reachable file, so it should be more like this:
VOCE/Recordings/-1458206716/Tiago#Aug_31_2012_Tiago_ID_1204811836_prebaseline.wav
(assuming your project publicly visible folders is parent to VOCE)
but i tried to make the $audios[$id][1] like http://home/.../file.wav
and still doesnt work.
That would work ONLY if your browser is running physically on the same machine your server is running and filesystem is accessible to you.
EDIT
How to do this right - some assumptions (if real system differs, just make adjustments) Your filesystem structure is as follow
document-root/
scripts/
test.php
recordings/
audio.wav
index.html
document-root is location on your disk which your httpd serves all files from. It is irrelevant what it really is. If I do http://yourdomain/index.html then it shall show content of index.html. Your scripts are in scripts/ folder and your audio files are in recordings. Then relative path from test.php to audio.wav is ../recordings/audio.wav. If you really need to use absolute path (not recommended) then it shall be http://yourdomain/recordings/audio.wav. Choose what's simpliest for you.
From my previous experience, I've almost always had problems with linking files with my website projects.
For example, linking CSS styles, Javascript files and including files in PHP. The problem is, that on my PC, the directory of my project was /www/project-name/ and when I put the project on a server, the directory would be just /www/. When I uploaded the project to a server, images wouldn't show, styles wouldn't work, database connections wasn't set, functions were not defined etc...
So my question is: What is the best and most efficient way to link/include files?
Something that will work no matter what the directory of the project is, and possibly, if I include project/includes/mysql.class.php in file1.php, and I move that file to a different directory, it would still properly include project/includes/mysql.class.php
You should use relative paths.
Instead of specifying the full path ('/www/project-name/includes/whatever.php'), use a path relative to the current location:
'./includes/whatever.php'
you can define the document root directory of project and then, include all files depending on it
put
define(DOC_ROOT, realpath(direname(__FILE__));
in your front controller, and when you have to include a file
include(DOC_ROOT . "/includes/file.php");
all frameworks uses this method
I'd suggest using a relative path (eg ../style.css or ../../style.css)
The ../ references the parent directory to the current file.
This is what I do, in general.
I use root relative urls inside html (e.g. src="/images/logo.jpg"). This way I can just copy the html from one page and past it in another without having to worry about the link not working becase the other page is inside a folder.
I relative urls in css, because all the resources I use inside the css, like images, I keep in the same folder as the css file (or a sub-directory of it). I mostly do this because it is shorter (url(img/background.jpg); vs. url(/css/img/background.jpg);). Minor added bonus is you could just copy the css folder to create a new theme based on the old one, without having to change all the urls in the css.
In PHP I use include($_SERVER['DOCUMENT_ROOT'] . '/includes/mysql.php');. You can just copy past the code into another file in another folder and it will still work.
The only time I rarely need to hardcode paths is inside htaccess.
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.
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.