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");
Related
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.
Whenever I use move_uploaded_file to my an uploaded file, the file always ends up in my web root. What am I doing wrong? Should the path be relative to my web root, or should it be an absolute path on my file system?
Ultimately what I'm trying to do, it have a folder for php to upload/dowload files. I don't want web bots and anyone else just to be able to access the files, i want only authenticated people using my website to be able to download the files. So this is how I have my file structure laid out:
/var/www/website/public_html
and
/var/www/website/files
and my move_uploaded_file command is like this:
move_uploaded_file($_FILES['txtFileSelector']['tmp_name'], "/var/www/website/files/".$_FILES['txtFileSelector']['name']);
but no matter what i've tried, the file always ends up in
/var/www/website/public_html
I've even tried sending the file in other sub folders of public_html but still no luck.
ah-ha! Destination path is relative!
So the solution for me is:
echo move_uploaded_file($_FILES['txtFileSelector']['tmp_name'], "../files/".$_FILES['txtFileSelector']['name']
Because of the relative pathing, use .../ to go up from the web root, and then move it to the desired storage folder.
CORRECTION
Absolute path or relative path either will work. It was a combination of folder permissions (www-data needs to either be owner or group member with read/write permissions) and me being an idiot and discovering a programming bug. My code was in a php class and the uploading was function. In my constructor I had a bug in my code. When doing OO there's a big difference between
$upload_dir = "/path/to/upload";
and
$this->upload_dir = "/path/to/upload";
My Following code is working fine when I set
$target = "size_images/14_20131216231522_cashew.jpg";
But Not Working in Actual php Code when full web address path is given though 777 Unix access is granted to system.
I am trying upload an image from SubDomain to Main Domain, So i need to give Full Path.
Page: http://subdomain.examples.com/
Code:
$target = "http://examples.com/size_images/14_20131216231522_cashew.jpg";
move_uploaded_file($_FILES["item_image"]["tmp_name"],$target);
For your reference, following are values of above code line...
echo $_FILES["item_image"]["tmp_name"]; --> "/tmp/php6RNC28"
echo $target --> "http://examples.com/size_images/14_20131216231522_cashew.jpg"
Even tried with relative path instead of http, No luck : /home/direc/www/size_images
No use of placing error code. its not returning any error.
error_reporting(E_ERROR | E_PARSE);
Try this, You need to use only relative path instead of domain path. Domain path doesn't work
$target = "size_images/14_20131216231522_cashew.jpg";
move_uploaded_file($_FILES["item_image"]["tmp_name"],$target);
instead of ,
$target = "http://examples.com/size_images/14_20131216231522_cashew.jpg";
move_uploaded_file($_FILES["item_image"]["tmp_name"],$target);
Send a file via POST with cURL and PHP Ref: http://blog.derakkilgo.com/2009/06/07/send-a-file-via-post-with-curl-and-php/
Try using:
$target = $_SERVER['DOCUMENT_ROOT']."/size_images/14_20131216231522_cashew.jpg";
If you want to upload from subdomain.mydomain.com to mydomain.com simply put the upload script on mydomain.com and then use a relative path.
You are misunderstanding how move-uploaded-file() works. It is similar to "File Save", namely you have to tell the PHP script a locally-writeable directory and file name where the file goes.
You mention you're trying to go from "subdomain" to "main domain"... if these two web urls are hosted on the same machine, this will be possible, you will just choose the directory that has the files for the "main domain" site.
It has to be a relative path.. absolute path would not work.
Also make sure that you know the difference between "/" and "./" But I would strongly recommend to either use configuration variables(in case of framework) OR constants to store file path.
Former point to root and later point to current directory..
This seems like a basic question, but it's stumping me. I have CodeIgniter installed, and I've got a model that manipulates and saves XMLs. My problem is when trying to save it to "/" I get a "Permission denied" PHP error. I need to save them to a separate directory relatively, but I'm not sure where exactly "/" is located on the server. Is it in the "/www" of Apache, or root of the whole server? Once I know this I should be able to navigate to the correct directory
/ is the root directory. The starting point of your directory structure. This is where the Linux system begins. Every other file and directory on your system is under the root directory. Usually the root directory contains only sub-directories, so it's a bad idea to store single files directly under root.
Try specifying the complete path in your application.
Example: /home/user/public_html/yourApplicationFolder/
Or specify a relative path:
Example: ../somePath/.
This article could be helpful.
To get the filesystem path to the document root just use $_SERVER["DOCUMENT_ROOT"];
i am working on a php project. I have uploaded the files in the server. When i login to the ftp account, it takes me to the main directory of files. I cannot implement the file download script. because i cannot obtain the root folder. when i use echo "$_SERVER["DOCUMENT_ROOT"]", it shows - "(/var/chroot/home/content/30/6323230/html/)". but this does not work for me. if i use file location like - "$_SERVER["DOCUMENT_ROOT"]/songs/", it simply gives error.
Its kinda unknown to me. because previously in another server outputs like - "/home/tskbdcom/public_html". when i log in to this ftp i get the root directory. then i go to "public_html" or "www" directory.
How can I solve this problem?
Try using
$_SERVER["DOCUMENT_ROOT"] . "songs/"
no leading slash before directory name because $_SERVER["DOCUMENT_ROOT"] has one slash at the and of path.
This is not answer to your question this is just reason why you got error using
"$_SERVER["DOCUMENT_ROOT"]/songs/"
Also you have double-quotes inside of double-quotes.