rename('/images/old_name.jpg', '/images/new_name.jpg');
This code gives file not found.
Script, where files are called is placed inside /source/ folder.
Files can be opened from http://site.com/images/old_name.jpg
How to get these files from root?
rename is a filesystem function and requires filesystem paths. But it seems that you’re using URI paths.
You can use $_SERVER['DOCUMENT_ROOT'] to prepend the path to the document root:
rename($_SERVER['DOCUMENT_ROOT'].'/images/old_name.jpg', $_SERVER['DOCUMENT_ROOT'].'/images/new_name.jpg');
Or for more flexibility, use dirname on the path to the current file __FILE__:
rename(dirname(__FILE__).'/images/old_name.jpg', dirname(__FILE__).'/images/new_name.jpg');
Or use relative paths. As you’re in the /script folder, .. walks one directory level up:
rename('../images/old_name.jpg', '../images/new_name.jpg');
In PHP the root (/) is the root of the filesystem not the "webroot". If the php-file is in the /source/ directory and images are in /source/images/ then this will work:
rename('images/old_name.jpg', 'images/new_name.jpg');
Related
I recently was watching a php video tutorial and the author was showing how to do include a file. He was using XAMPP for the demonstrations and had many files.
When he was showing how to include a file, he mentioned something about putting two dots (..) in front of the file path (/xampp/content/example.html) because of something having to do with where the files were located, assuming that I already had knowledge of this principle. But i don't.
Can anyone explain what is up with having one dot or two dots in front of file paths?
What is the difference between include("/xampp/content/example.html");, include("./xampp/content/example.html");, and include("../xampp/content/example.html");
In Linux / Unix environment,
/xampp/content/example.html means absolute path
./xampp/content/example.html means relative path of current directory
../xampp/content/example.html means relative path of parent directory
For the folder structure: /var/www/xampp/content/example3.html:
If your current folder is /var/www/...
../ (goes up 1 level) will be /var/
./ (in current level) will be /var/www/
/ will be / (in Linux, / means the root of the server, the outermost structure of the filesystem)
../../ (goes up 2 level) will be /
There are 2 types of paths: Relative Path & Absolute Path.
For Relative path, it's relative to your current directory. For absolute path, it's not related to your current directory.
. means the same directory as the script that's doing the including, .. means the parent directory of the one containing the script. So ../xampp/content/example.html means to go up one folder level from the current script, then go into its xampp/content subdirectory to find example.html.
A path beginning with / is an absolute path from the root of the server. Using absolute paths makes it harder to move your project to a new directory, because you'll need to update all the paths. Relative paths allow you to move everything as a group without changing the paths, because the directory relationships will stay the same.
I'm using apahce server and I have files that I want to keep private. I've read that they should be put in a directory outside of the document root.
I could access them from the root directory using:
<?php include('../includes/somefile.php');?>
To simplify navigation I want to use paths relative to the root. This works fine for directories within the site such as:
define('ROOT_PATH', $_SERVER['DOCUMENT_ROOT']);
include(ROOT_PATH.'/path/file.php');
but I get errors when I try going up the directory tree like this:
include(ROOT_PATH.'../includes/somefile.php');
Am I doing something wrong here?
This is because .. is taking to the previous directory of the DOCUMENT_ROOT which you do not have permission to access.
Okay so I'm learning about uploads and need to copy the tmp file to a specified directory so I use the copy command.
I use this inside my index.php of the main directory (learningupload/ folder)
copy($_FILES['photo']['tmp_name']['file'], './pics/'.$photoname.'.jpg');
Now I'm doing this on Xampp local host, and my uploads go to the tmp folder which is of course different from where I want the upload to go
so there's
C:\xampp\tmp
C:\xampp\htdocs\learningupload\pics\
My question is this: What is this ./ inside the destination string I need to have? Doesn't it usually mean you're moving UP a directory? Why not have it '/pics' no dot? I tried that and said it couldn't open the stream. Does the dot here mean it's referring to whatever directory index.php is in? Or am I thinking ../ moves up a directory? And single dot is different?
Thanks.
./ means "current working directory". Which directory is current working one you can see with echo getcwd();
The parent directory (the upper one) is ../
You can change your working directory using chdir() or you can just specify the full path instead.
What is set_include_path Relative to, in PHP? Is it the folder where the PHP.exe resides? Is it the webroot? In other words, what folder would set_include_path('/') or set_include_path('.') be referring to?
Relative paths are resolved from the location of the file where include or another function that uses include_path is used in (see description of include_path):
Using a . in the include path allows for relative includes as it means the current directory. However, it is more efficient to explicitly use include './file' than having PHP always check the current directory for every include.
/ would describe the root of your filesystem and . the current directory.
The filesystem root and the current directory, respectively.
set_include_path("/") would make the include path be the root folder of the filesystem, and I would take a guess that you'd probably not want to do that as there might be issues with exposing files that you don't want to be seen.
If your php file was /home/users/joebloggs/htmlroot/index.php, then set_include_path(".") would make the include path the directory that the php file is in, ie the "htmlroot" directory.
On *nix systems and Windows Apache the / is the root of the file system. While on IIS / points to the root of the vhost.
What I do to handle this is define a LOC constant in my index.php so I never get confused when including files.
define('LOC', dirname(__FILE__));
include(LOC . '/files/file.php');
OK when I save uploaded files with PHP via move_uploaded_file() I cannot use an absolute URL I have to use a relative one. My site has 2 root directories one for the http side and one for the https side: httpdocs and httpsdocs respectively. So if my script is on the https side how can I save the file to a location on the http side?
Thanks!
UPDATE
OK so it seems like I am using the wrong absolute path convention I am doing it like this:
$dir = 'https://www.mydomain.com/masonic_images/';
move_uploaded_file($_FILES['blue_image']['tmp_name'], $dir.$new_name);
move_uploaded_file() doesn't accept URLs for either parameter. The destination is an absolute path on your filesystem.
<?php
$dir = '/var/www/httpsdocs/'; // Adjust to your configuration
move_uploaded_file($_FILES['blue_image']['tmp_name'], $dir.$new_name);
As #apphacker suggested. you can use realpath(__FILE__) to determine the absolute path to a file.
If you cannot use the absolute path because you don't know what the absolute path is, use PHP's realpath() to figure out what it is and then use it.
Are the httpdocs and httpsdocs directories both located in the same parent folder? If so, just use a relative path for the second parameter in move_uploaded_file to place the file in the other root directory.
For example:
$uploaddir = '../httpdocs/';
$uploadfile = $uploaddir . basename($_FILES['myfile']['name']);
This code assumes that the uploading script is located in the httpsdocs root directory, and that you want to save the file into the httpdocs directory.
Note that since you put uploaded files inside httpdocs it could be possible to upload a php file and execute arbitrary code.