I have created image uploading codes and they're only allowing me to upload image into only directory that is in the same directory as PHP file.
$profile = 'profiles/'.$_FILES['profile']['name'];
if I change it like this:
$profile = 'php_codes/profiles/'.$_FILES['profile']['name'];
it shows me error. I'm using copy() function to upload it.
Please help me to get to know how to upload it in any directory even into any other partition. Thanks for your help.
You will need to provide the error, but it's very likely that the error you are getting is that the directory that you are attempting to copy the file to does not exist. It could also be permission problems, but your confusion seems to be over building a path.
The problem with your code is that your path is relative to the PHP file's location, rather than being a direct path from your root directory. You should read a little bit about how to navigate file structures, but these are the three key things to remember when working in a *nix file system (such as Linux):
If your file path does not start with a slash, then the path will be relative to the directory that the PHP script is in.
If you start your file path with a slash, the path will be relative to the root directory.
You start a path with one or more ../, to traverse to a parent directory.
So for example, let's say you have these three directories, with your PHP script residing in /php_codes:
/php_codes
/php_codes/code_snippets
/profiles
If you wanted to copy the file to php_codes, your path would be relative to the PHP script:
$profiles = $_FILES['profile']['name'];
If you wanted to copy the file to php_codes/code_snippets, again you could just do it relative to your PHP script:
$profiles = "code_snippets/" . $_FILES['profile']['name'];
However, this is an opportunity to also show how you might do it with an absolute path from the root directory. You could use this (note the slash at the beginning of the path):
$profiles = "/php_scripts/code_snippets/" . $_FILES['profile']['name'];
If you want to copy the file to /profiles, which is outside of the /php_codes directory, there are two ways you can do it.
The first way is with an absolute path from the root directory (path begins with a slash), just like the example above:
$profiles = "/profiles/ " . $_FILES['profile']['name'];
Or, you can make it relative, by using ../ to go up one level to the parent directory:
$profiles = "../profiles/ " . $_FILES['profile']['name'];
A quick note about using ../ to go up one directory: you can repeat that as many times as needed, to continue going up a level. For example, if your PHP script was located inside of /php_codes/code_snippets, but you wanted to copy a file to /profiles, then you would have to go up two levels:
$profiles = "../../profiles/ " . $_FILES['profile']['name'];
you can use
$profiles=__DIR__ . "/profiles/" . $_FILES['profile']['name'];
that will work as DIR will be your script directory and then it will be absolute path
I have finally got it! Just using ../profiles and then directory is making it. You just write dots (2) then times you want to go back. If two times, ../../profiles. Thanks for your help.
Related
I want to unlink an image file from children dir of a parent dir.
I developing this project in a director folder under my website (mysite/project) but when i complete the project i will move to another hosting package.
Whic code i need use for stable running in all directories?
I get this error:
Warning: unlink(../../images/urunler/deneme-urun.jpg): No such file or directory in /home/admin/public_html/eticaret/admin/includes/updateproduct.php on line 120
My folder structure:
click here to see my folder structure
$delete = unlink("../../images/products/".$img);
Before running unlink() command you can check the existence of the file to get rid of such error
$path = "../../images/products/".$img;
if (file_exists(path)) {
unlink($path)
}
Better is using the full directory path (absolute path). An example, let your project structure is
and your unlinking code is in script.php, then your can get full absolute path the following way
$path = __DIR__ . "/../../images/products/{$img}";
if (file_exists(path)) {
unlink($path)
}
Thanks for all answers.
I have a fault on this topic.
Image name is xxx.jpg in my database but xxx.jpeg in the folder.
but there is an interesting situation: if you have an image as .jpeg format, you can open on browser as .jpg format.
I've tried a lot of methods to solve the above error, but I've found that.
I using chrome browser now.
So i solved my problem yet :)
as the script is run from "public_html/eticaret/admin/includes/" you need to .. traverse back 3 directories to get to public_html which is a common ancestor folder for both image and script
according to your directory image your images are in public_html/myproject/images/products/ and according to error detailed in your comment you are trying to delete from images/urunler/ I assume urunler is the actual project name.
if the folder containg your images is urunler try $delete = unlink("../../../images/urunler/".$img);
if the container folder is products try $delete = unlink("../../../images/products/".$img);
if that fails try using an absolute path instead of a relative path.
I'm having some trouble with glob in php and although I did some research, I just couldn't seem to find an answer to my problem.
First of all to my directory structure:
/ (root)
/images (folder)
- gallery/ (sub folder containing image files)
/pages (folder)
- gallery.php
I want to access the subfolder 'gallery' in the folder 'images'. So: In my gallery.php file I got the following line:
$images = glob("/images/gallery/*.*");
And this doesn't work.
What does work is, if I change the path to "../"
$images = glob("../images/gallery/*.*");
or if I change the code to:
define('BASE', $_SERVER['DOCUMENT_ROOT']);
$images = glob(BASE."/images/gallery/*.*");
Unfortunatly the sourcecode then shows some info I'm not sure I want to actually make public
e.g.
/home/scecjwkh/htdocs/images/gallery/3.JPG
I hope the information I provided is enough to actually understand my problem. Not sure why I have so much trouble with a relative path o.O
Thank you in advance,
Stuben
What you usually do is use __DIR__ to build relative paths from the current script's folder. You can also use __DIR__ to know what part to cut from the final paths, because you should know where in the project your current file is and thus know where the root is relative to it.
$images = glob(__DIR__."/../images/gallery/*.*"); gets the image list without caring about current working dir, after which you can use realpath(__DIR__.'/..') to figure out how much to snip from each file path.
I work in responsivefilemanager and in config file I Have two line:
$upload_dir = '/user/uploads/files/'; // path from base_url to base of upload folder (with start and final /)
$current_path = '../../../../uploads/files/'; // relative path from filemanager folder to upload folder (with final /)
This wroked In xammp system (with sub folder. /user/ is sub folder). if i move in real server i need to edit this two line and remove sub folder and one ../ from two line.
Now, I need to auto detect url path. My mean is : if i install in sub folder Or root folder this two line worked in my script without manual editing.
How do can i create this?
There is no particular way to just autodetect what directory to upload to. The easiest is to solve this by just checking if the directory exists and use the other directory if it was not found.
However, I'd suggest having the same relative structure on both systems is the best solution. With the same relative structure this should work across all servers and systems without having to change anything.
I have a php function that is moving files for me. It requires absolute paths to place those files (/Applications/MAMP/HTdocs/mysite/myfolder)
how can a turn this folder path string into a url (http://mysite.com/myfolder) so that I can create links to the files on the fly?
I do not know necessarily the names of the folders, as the software could be run in many locations.
Many thanks.
Obviously, you need to know server root for such a calculation.
Luclikly, $_SERVER['DOCUMENT_ROOT'] contains this path.
So, just subtract server root path from the given path:
$path = '/Applications/MAMP/HTdocs/mysite/myfolder';
$approot = substr($path,strlen($_SERVER['DOCUMENT_ROOT']));
check if you have a drive letter in the DOCUMENT_ROOT, and correct the code if necessary
Note that adding http://mysite.com is unnecessary and useless. just /myfolder/ is what you really need.
You can check for this value: $_SERVER["DOCUMENT_ROOT"];
That is the root of your website. If you have the folder and replace the $_SERVER["DOCUMENT_ROOT"] with the $_SERVER["HTTP_HOST"] you will get the URL to the folder/file
If mysite folder is within HTdocs then you can access it using http://yourdomain/mysite, (if HTodcs is your home directory)
I'm testing a website on my local machine and I'm wondering what would be the best way to write paths to make sure they work when I upload the site to its final location.
In general I'm a bit confused about paths, and in many cases I have to 'tweak' each path until it works, so wouldn't want to be forced to do the same on a production site!
I'm not clear when to use $_SERVER['DOCUMENT_ROOT'].
For example, I have a directory that I want to scan, which is just under the root. So why can't I just use "/dirname"?
$dir = $_SERVER['DOCUMENT_ROOT'] . '/uploads'; //this works
// $dir = "/uploads"; //this doesn't work
if (is_dir($dir)) {
//do something..
}
I'm not clear when to use
$_SERVER['DOCUMENT_ROOT']. For
example, I have a directory that I
want to scan, which is just under the
root. So why can't I just use
"/dirname"?
When you work with paths in the php file the root (/) is the root of the filesystem, not the root you get when you visit your website.
So $dir = "/uploads"; gives you the filesystem root.
To minify your problems I would declare a variable in a configuration file that specifies the root of your php application, and use that path+whatever more is needed.
As adamse mentioned, the reason you can't use the '/path' is because it points to the root of the filesystem.
However, instead of declaring a variable that defines the root, I recommend using dirname(__FILE__) to retrieve the full path to the directory that the calling file is in.
From there, append relative path information to the file you want and you end up with a complete path, fully dynamically.
For example, if you want to include the 'header.php' file in the directory above the file that you wish to include it in use:
include(dirname(__FILE__) . '/../header.php');
The beauty of that is that PHP will always automatically convert the forward slash to the directory separator required for the host OS.
I would define a variable/constant that describes the absolute filesystem path to the application. Something like this:
$appDir = rtrim(str_replace('\\', '/', realpath(dirname(__FILE__))), '/');
Then you have this base path you can address your application’s files from:
include $appDir.'/library/foo/bar.php';
Or you even change your include path to that directory:
set_include_path($appDir);