Move uploaded file to root directory - php

I'm making a content management system for a website I built. I want the system to be discrete, so I made it exist in only one PHP file, called '_admin.php'. All the content displayed in this file comes from includes that I store in a sub-folder called 'admin' (out of the way).
The photos used on the website are stored in an 'assets' folder that also sits in the root dir. The admin page has direct access to the assets folder, as it is also in the root. But the upload file script sits a few directories into the 'admin' folder and I want the uploaded files to be stored in the assets folder.
The move_uploaded_file() method takes the destination path for the file, but it requires a direct path. I try using $_SERVER['DOCUMENT_ROOT'], but the resulting directory doesn't seem to have any of my files. If I use getcwd() in a doc in the root, it returns the actual file structure that I can use. The same if I echo out __FILE__. But I've experimented with this SERVER constant a lot and I can't locate my website with it.
Since the script that uploads the images is called as a form action, I can't pass the root directory as a variable.
Not really sure what I'm doing wrong, anyone have any ideas?
Thanks
edit **
//See if Files array contains new files
if (!empty($_FILES['file'])){
foreach($_FILES['file']['name'] as $key => $name){
$error = $_FILES['file']['error'][$key];
$temp_name = $_FILES['file']['tmp_name'][$key];
$dir = getcwd();
$move_file = move_uploaded_file($temp_name, "$dir/temp/$name");
if (($error == 0) && ($move_file)){
$uploaded[] = $name;
}else{
die($error);
}
}
echo __FILE__;
echo "<br/>";
echo __DIR__;
echo "<br/>";
echo $_SERVER['DOCUMENT_ROOT'];
exit();
}
The upload script I'm currently using. This script works fine because I'm storing the images in the same directory as the script. I just don't know how to store them in my root.

I would specify the full absolute file path if you can. I set this via define() in a config file for my CMS. On install you figure out what that path is and set it.
define("BASEFILEPATH", "/home/.../[webroot]"); // The base file path for the website
You may be looking for something more general, but you could have some sort of install script where the user can enter basic info into a form, such as username, pwd, etc. and you could have them enter this path as well.

Related

How to save image in a subdirectory using file_put_contents and return the path in php

I am new to PHP but have experience in .net. I need to know a way to save images uploaded to a PHP page in a subdirectory of that page and return its path. I have checked and not found a way to give permission to an existing subdirectory in PHP. I am using wamp for development.
passed subdirectory in file_put_contents with the image file name.
got full path and appended the subdirectory.
$ImagePath = __DIR__.'/driverimages/'.$ImageName ;
file_put_contents($ImagePath, base64_decode($data->profilepic));
I know that if I create a directory from a PHP script with 0774 permission I can write to it. But I need to write to an existing subdirectory and return the image path so that it can be used in the front end to show the image.
I need to be able to write to a subdirectory from the current directory and return the path of the written image file.
$ImagePath = __DIR__.'/driverimages/'.$ImageName;
mkdir(dirname($ImagePath),777);
file_put_contents($ImagePath, base64_decode($data->profilepic));

How to upload file in any directory with PHP

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.

How to know the right path to the Joomla Configuration file on a external script

This is something that happens very often, the people moves the joomla configuration file from the root for security reasons but some external scripts can be affected.
Is possible to find the right path to the joomla configuration file in order to include this new path on a external script php script?
save this in a file with name path.php, and then upload it to root of the host, then call it like: http://www.test.com/path.php
<?php
$path = getcwd();
echo "Your Absoluthe Path is: ";
echo $path;
?>

How do I upload images to the correct path in PHP?

I have a website with the following path for images: domain.com/images
I am using a PHP script in the back-end to upload images to that folder BUT the page that lets me upload images to that path above is located at domain.com/administration/upload.php.
So now when I am using a script to write to domain.com/images this is what I am using:
$newname="/images/".$image_name;
For some reason it won't write to that folder and I get no errors and here's the link from where I got the script:
http://www.reconn.us/content/view/30/51/
What do I have to do to make it write to that domain.com/images path from domain.com/administration/upload.php?
You have to distinguish virtual server path from filesystem path.
There are no /images/ directory on your disk
but something like /home/www/user/public_html/images/
so, change your code to
$newname = $_SERVER['DOCUMENT_ROOT']."/images/".$image_name;
and it should be okay
You can try this for the exact path to folder
/* $_SERVER['DOCUMENT_ROOT'] will point to your root folder( www ) and after that your website folder name and them images folder name */
$newname = $_SERVER['DOCUMENT_ROOT']."/folder name/".$image_name;

site root relative path confusion in php

I'm trying to upload images to server, create a thumbnail from the uploaded image, and save the relative paths to mysql database . . . . The image path is
/mysite/images/
from my site root (which is wamp/www/mysite/images in my local testing environment)
Now, I'm performing a directory check with
if(!is_dir($path) && !is_writable($path)){
throw new Exception ("$path is not valid .. ");
}
where path is /mysite/images/ , thinking that initial '/' will denote the root.
But, the script was throwing an exception saying invalid path name, and my
move_uploaded_file($tmp_name, $path.$filename)
too was not working.
PS: the path mysite/images/ also throws an error and the file is not moved
Now, I have worked around by appending document root manually to path name for both directory checks and move file , which is now looking like
if(!is_dir($_SERVER['DOCUMENT_ROOT'].$path) && !is_writable($_SERVER['DOCUMENT_ROOT'].$path)){
throw new Exception ("$path is not valid .. ");
}
move_uploaded_file($tmp_name, $_SERVER['DOCUMENT_ROOT'].$path.$filename)
I also changed the path to
mysite/images/
removing the initial slash intended for site root. This is working perfectly.
But, I cannot understand why directory check and move uploaded file snippets are not taking site root relative links.
Any ideas is appreciated.
The problem is with the path representation.
In linux /mysite will try to access the directory in the root level of file system. Windows this might not create an error. for is_file() kind of API's it should the absolute path and not relative ones.
Your website path is not wamp/www/mysite/images.
You are using a web server, you cant access the drive like: c:/wamp ....
Once online, how will you access your files? Linux does not have c:/ ...
Your website path is: http://localhost/mysite/
Your image path is: http://localhost/mysite/images or ./images if your code is correctly written.
so, do this:
define('ROOT', $_SERVER['DOCUMENT_ROOT']);
include_once(ROOT."/mycfgfile.php");
or
include_once(ROOT."/includes/mycfgfile.php");

Categories