How to set file directory slashes in php? - php

I want to specify the location of the 'upload' folder which contains the images uploaded. Now I want to display an image in post.php >>myblog is the root folder in htdocs xampserver >>'myblog/folder1/folder2/admin/Post.php' and the upload folder exists in the root folder 'myblog/upload'
I Only want to know how to understand the logic of slashes(forward and back slashes)
I'have tried first
<td><img src="../Uploads/<?php echo $Image ; ?>"</td>
Then
<td><img src="../..//Uploads/<?php echo $Image ; ?>"</td>
Then
<td><img src="./../..//Uploads/<?php echo $Image ; ?>" width="170px;" height="50px"</td>
But none of these slashes worked
<td><img src="..//Uploads/<?php echo $Image ; ?>" width="170px;" height="50px"</td>

There are different ways to write URL's to assets in HTML.
Relative from the current URL
If you write
<img src="Uploads/image.jpg" />
and the URL is example.com/some/subpage, then the browser will try to load the image from example.com/some/subpage/Uploads/image.jpg.
If you write
<img src="../Uploads/image.jpg" />
the browser will, just like a file system, try to go up one level and load it from there: example.com/some/Uploads/image.jpg.
So this method is very much dependent on the current URL, which means that the image URL would need to be different (how many ../ you need etc) on different pages (if they are sub pages in sub pages etc). This is not a good way of doing it.
Relative from the document root
If you write
<img src="/Uploads/image.jpg" />
(Notice the / in the beginning)
the browser will try to load it from the document root (the same folder you have your index.php in): example.com/Uploads/image.jpg.
The good thing about this method is that the current page URL is irrelevant. If the Upload-folder is in the document root, just do /Uploads/image.jpg.

When you create a website with multiple pages is a good idea to put a "base tag" from which all other links are concatenated.
Here is a very good example that suits your needs:
https://www.w3schools.com/tags/tag_base.asp
If your folder is somewhere in the public directory. Let's say
xampp/project/uploads/
Your base tag is probably "http://localhost/project/". After that you can set a relative url for the images like "uploads/image-name.jpg"

Related

PHP - image src path show broken image

I defined a constant:
define('ROOT_URL', dirname(__FILE__));
I am using it with a second part of a path to call on a png image.
<img src="<?php echo ROOT_URL ?>/images/folder1/logo-black.png" nosend="1" border="none" width="129" height="34" />
Image returns broken and my path is not as it should be. (But still when I link it, it shows me a image).
It's like:
C:\wamp64\www\email-signatures/images/folder1/logo-black.png"
It's with \ and / .
What am I doing wrong?
If you pass the browser an address like this C:\wamp64\www\email-signatures/images/folder1/logo-black.png it will look on the other guys PC for that folder and file. Remember you are passing text (HTML) to a browser on someone elses PC, for the browser to interpret and paint on that other PC's Screen. You need to pass a URL so the browser knows to go to your WebServer for the image.
You could use the whole http://domain........ but if you just use the relative folder name, even if you move this code to another domain, it should still work just fine.
So assuming the images folder is just below your DocumentRoot try
<img src="images/folder1/logo-black.png" nosend="1" border="none" width="129" height="34" />

Reviewing uploaded images with PHP and MySQL

I am using PHP to upload images in my web application. The images are stored in some directory on the server while their paths are stored directly in MySQL database.
The upload goes very well and images get in the folder but the problem is accessing thoses images with their path field stored in the database : i am not yet able to find the correct form of the path i should use, now am using the realPath and dirname functions to help me get the path so finally an example of path is C:\wamp\www\webroot\img.png (since am on Windows using wampserver) So when i do something like :
<img src="$image->path" />
i get no image shown in the browser and when i inspect it i get the expected code like :
<img src="C:\wamp\www\webroot\img.png" />
which means that this path format is not correct to show the image.
I have tried many things : i took the same path and acced it with the browser and it showed me the image (with the file protocol automatically) so i added file:// to the image path but nothing was new. I have also tried to acces it as a web url and with that it goes will for example it shows the image when putting
localhost/webroot/img.png
But what i need exactly is being able to store and retrive the image file again. So is it a file system probelm ? is the code platform independant ?
I will be very grateful for any help
Thank you.
You can't show an image with its path, but with its URL.
In PHP, we use to save the filename only in MySQL and after, you display the image with <img src="path/to/img/<?php echo $image->filename; ?>" />
That's all!
Add the file protocal to your path.
<img src="file:///<?php echo $image->path; ?>" />.
The best thing you could have to done is save the image in a folder inside your project/website folder. Then save the path to the image from the root folder in the DB.
Example: I uploaded a an image of filename "image.png" inside "Img" folder in the Website Root Folder. Then I will save "Img/image.png" inside my DB.
So anytime I want to reference the image, I will just use
<img src="/<?php echo $image->path; ?>" />
Or
<img src="<?php echo $image->path; ?>" />
Sorry for wasting your time, i have solved the problem. It was an issue with how my application is accessed. It should be accessed from only a directory called web so every path should be referenced from that directory !!.

How to to access images on main domain from subdomain

I have a main domain that users can upload images to which are then viewed in a slider so multiple images (sometimes up to 50) are loaded by the page on loading. I am trying to access the same images on a subdomain but it isn't working very well.
If I use
<img src="<? echo 'http://www.mysite.co.uk/'.$row['imageLocation']; ?>">
the images load but it is very slow
If I use
<img src="<? echo '/home/myname/public_html/mysite.co.uk/'.$row['imageLocation']; ?>">
the images aren't displayed despite the path being correct. If I use the same path for getting the images sizes (getimagesize) it returns the correct results so I'm sure the path is correct.
The images arent shown because you echo the path to the user first, but not the file itself and then the users browser loads them asynchronously. He cannot access any parent directory from mysite.co.uk only child directories.
The PHP can load them because it runs on the server itself before it is returning anything.
What you could do is using the readfile() function from PHP in an extra PHP file like getimages.php or just use the first solution, which cannot be improved in speed except you will change the image sizes, which might be your main problem here.
If you want to directly echo image file content, use this.
<img src="data:image/jpeg;base64,<?php echo base64_encode('/home/myname/public_html/mysite.co.uk/'.$row['imageLocation']); ?>" />

Make wordpress auto-redirect to the theme directory as root path in URLS

In my wordpress site, let's say I have an image that is located in:
/wp-content/themes/root-theme/assets/img/image-1.png
If I want to access this image in a blog post, when using the html editor in Wordpress I have to type out:
<img src="/wp-content/themes/root-theme/assets/img/image-1.png" />
This generates a url of:
http://my-site.com/wp-content/themes/root-theme/assets/img/image-1.png"
This is a massive pain. I'd much rather be able to write:
<img src="/img/image-1.png" />
That is, I want to make it possible to access my assets folder from the root directory, so that the image above could be accessed via:
http://my-site.com/img/image-1.png
Is this possible to do or would it majorly screw up the rest of the site?
You'll have to use the media manager, it will write the full link out for you, no need to type anything. Just above the editor click "Add media", select your image, click insert into post and you're done.
Use bloginfo function to get full url of your theme :
<img src="<?php bloginfo('template_directory');?>/assets/img/image-1.png" />
Output: http://my-site.com/wp-content/themes/root-theme/assets/img/image-1.png
Reference

Html image source from a local folder on windows options

On my html page, I make use of images which are housed in a local folder. The paths are coming directly from the database field. How can I do this? I know it wont work if the images are not in the web root directory. Can symlinks work?
For example,
The physical path to the images is c:/Images and the database field will contain the path like this, photo/image1.jpg
I will fetch the image source path from php as shown below,
<img id="image1" src="<?php echo $this->object->imagePath; ?>" class="img-polaroid">
$this->object->imagePath here will be the string concatenation of c:/Images and photo/image1.jpg. So, putting it together t will be c:/Images/photo/image1.jpg
The problem is it does not show up. I have tried this as well to test,
<img id="image1" src="file:///C:/Images/photo/image1.jpg" class="img-polaroid">
But no luck.
first let me know if putting just C:/Images/photo/image1.jpg in a browser address bar brings an image or not. If not then that's mean you are giving wrong path. Let me know so i can edit this answer. But for now this is the answer i hope.
Symlink to the folder worked like a charm.

Categories