PHP: Saving files with PHP to different root directory? - 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.

Related

PHP move_uploaded_file is Not uploading Images when full web path is given in Unix system

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..

How to upload a file using php to the parent directory

Hopefully easy question, I have a desktop application that allows the user to upload a file to a server using a form, the form sends the data to a protected file on the site like this. Site_root/protected_folder/myfile.php . If you use php file upload commands normally you'd be operating in the 'protected_folder' directory, which I don't want.
I want to add stuff to the images file on the root directory like this Site_root/images/ , how would you go about doing this without going the ftp root?
The usual method is to call move_uploaded_file(), where you set the destination path to your liking. The file name in $_FILES['tmp_name'] normally points to a temporary folder and it's subject to be removed without prior notice.
You can either use an absolute path like /path/to/images/ or use a relative path like ../images/
Assuming you're using move_uploaded_file the second paramater takes the directory that you wish to upload to. Perhaps showing you code may help if this post doesn't.
move_uploaded_file() will allow you to place uploads relative to the root directory if you simply start your path with a slash like
$newFileDir = '/username/public_html/websiteroot/subdir/yourfile.jpg';
move_uploaded_file($_FILES['postname']['tmp_name'],$newfileDir);
you can simply use copy() and double dot (../) in path to specify root directory to copy the uploaded file. I'm using the same. You may want to change the file name so that there will be unique filename in the directory error will occur. extension will also be same.
//
$filename = stripslashes($_FILES['postname']['name']);
$extension = getExtension($filename);
$newfilename ='photo_your_filename'.time().'.'.$extension;
$newFileDir = '../subdir/'.$newfilename;
copy($_FILES['postname']['tmp_name'],$newfileDir);

PHP root folder

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');

php mkdir windows relative path

I want to create a directory on windows from a PHP script.
My script is in the www/Test directory of Apache and I want to create a folder (fold1) inside www/downloads directory.
Inside the script, I'm using:
$dirName = "../downloads/fold1";
mkdir("{$dirName}");
If I use the full path of dirName like C:\Apache\www\downloads\fold1, it works fine.
But I want to use a relative path since this code will be sent to the client.
I would guess your current directory is different from your files folder, so you have to use a trick:
mkdir(dirname(__FILE__) . "/" . $relative_path);
dirname(__FILE___) returns the absolute path of your current php file. With this you can build an absolut path.

php paths that work on both local (mac) and remote server

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);

Categories