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";
Related
I'm creating a .php file that will be uploaded to the root directory of a server. I need that .php file to then figure out the path to the public_html folder or it's equivalent.
I need to do this because I want my .php file to be able to be uploaded to the root and used on any hosting account. Because many hosting companies use different file paths to the public_html folder or even call it something different, I'm trying to figure out how to detect it.
Preferable there is a server variable or easy test to do this. If not, the public_html folder will always contain a particular file so maybe I could search for this particular file and get the path that way. I'm just worried about a filename search being heavy on memory.
The .php file that is being executed is located inside the ROOT directory and needs to locate the public_html folder.
Like this: /home/user/file.php
needs to detect
/home/user/public_html/ or /home/user/var/www/ or /home/user/website.com/html/ etc.
The challenge with this is that a server can have very many public_html's so outside of the context of a request there is no real way to find out what that is.
One thing that you might be able to do to get this information from a php script (if you know the url to get to the host) is to create a php file called docroot.php that looks like this.
<?php
if ($_SERVER["REMOTE_ADDR"] == '127.0.0.1'){
echo $_SERVER["DOCUMENT_ROOT"];
}
Then within your file.php your would do something like
$docRoot = trim(file_get_contents("http://www.mydomain.com/docroot.php"));
This makes the assumption that the server can resolve to itself via the local interface by name.
I found this website which provided me with the only good solution I have found after scouring the web...
$root = preg_replace("!${_SERVER['SCRIPT_NAME']}$!", "", $_SERVER['SCRIPT_FILENAME']);
The way this works is by getting the full path of the file and then removing the relative path of the file from the full path.
I have a snippet that I want to use to upload a file.
The script seems to be running fine until it gets to the point where PHP transfers the file from temp docs to my own folder.
My folder is called 'uploads' and is the root.
On ModX the PHP files directory is
/core/cache/includes/elements/modsnippet
I cannot seem to figure out a way to direct the script to send the file back from the modsnippet directory to the public_html/uploads directory.
So far I have tried:
move_uploaded_file($_FILES['instlogo']['tmp_name'], dirname(__FILE__) . "/../../../../uploads".$new_file_name)
;
as well as absolute paths, eg:
http://mysite.com/uploads
To no avail.
Would anyone know a way of doing this correctly? Thanks!
(ps: permissions on that folder are 777)
can you try something like:
$my_uploads = $modx->getOption('base_path').'uploads/';
move_uploaded_file($_FILES['instlogo']['tmp_name'], $my_uploads.$new_file_name)
the base_path option will give you the full file system path [from the server root]
if not & you are getting a filesystem error, the server logs [aapche] should be telling you why. Do you have access to them?
I'm trying to adapt an upload script to fit on 000webhost. I keep getting errors about the destination folder not existing. This is what I have:
define('DESTINATION_FOLDER','/uploads/');
I've also tried
/public_html/uploads/
and
/subdomain/domain/com/uploads/
In examples I've seen people were using /www/ but I don't know where that goes.
What is the correct syntax to use in this case?
Try this:
define('DESTINATION_FOLDER',$_SERVER['DOCUMENT_ROOT'].'/uploads/');
Consider using the PHP constant DIRECTORY_SEPARATOR, if platform independence matters to you:
define('DESTINATION_FOLDER',$_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.
'uploads'.DIRECTORY_SEPARATOR);
http://php.net/manual/en/dir.constants.php
EDIT:
Storing uploads under the document root can be a security risk. Unless you want those files to be directly accessible by the web server, you should consider storing them outside of the document root within your webspace. If you created a folder named "uploads" along side your "public_html" folder with your host, you could access it like this:
define('DESTINATION_FOLDER',$_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.
'..'.DIRECTORY_SEPARATOR. // (up one level)
'uploads'.DIRECTORY_SEPARATOR);
Or just specify an absolute path (leading with /) directly to the folder.
I'm using OpenSuse 12.2 now. To learn and test PHP code I use public_html directory in my home folder: /home/wojtek/public_html/ I access project files in the following way:
http://localhost/~wojtek/projects/foo/bar.php
But I have not clue how to make move_uploaded_file to work. The server root is /srv/www. I set upload_tmp_dir to /srv/www/tmp (I have really no idea where it should go).
When I set destination of move_upladed_file to /srv/www/images, files landed there. But I'd like to make use of public_html folder as I'm keeping all my files there.
For example having:
public_html/projects/foo/
public_html/projects/foo/bar.php
public_html/projects/foo/images/baz.jpg
Can I somehow use the relative destination ('images/') of move_uploaded_file in the bar.php? So that I can access images relatively in IMG tag like from bar.php?
Sorry if it sounds a bit chaotic but I'm new to PHP.
No you wont be able to access the /srv/www/images folder from the /home/wojtek/public_html/projects/foo/bar.php web page.
Id suggest either
1) setting the move_uploaded_file to /home/wojtek/public_html/projects/foo/images
or
2) using a symbolic link to access the images folder from your public_html folder:
cd /home/wojtek/public_html/projects/foo
ln -s /srv/www/images/
and then it will appear that the images are stored in /home/wojtek/public_html/projects/foo/images, but they will actually still be here /srv/www/images
Its possible that you may run into trouble with permissions using this method however
I have been struggling for a few days trying to find out how come I cannot move a file(move_uploaded_file) from temp to the directory I have setup (/img/uploads/photos).
After alot of testing and head scratching, I decided to write into CakePHP's CakeLog whatever is happening in the upload function.
Although I have physically created :/img/uploads/photos, when I use CakeLog::write('debug', 'Does directory exist?: '.json_encode(file_exists('/img/uploads/photos/'))); it logs false. Or is_dir, also returns false
Why is this happening... Can anyone help me out!
I doubt you have made a directory /img/uploads/photos/ ? It is probably inside the same dir as your other files, somewhere like /var/www/yoursite/img/uploads/photos/ or something like that.
You can use some tricks like $_SERVER{'DOCUMENT_ROOT'}, as you can see over at http://php.net/manual/en/function.file-exists.php
I don't believe you when you say you have created the /img/uploads/photos/ directory. That's an absolute path, counting from the root of the machine. It's more probable that you have created the folder in the web directory somewhere (such as /var/www/img/uploads/photos/ or /home/caboone/public_html/img/uploads/photos/).
The path /img/... means the path is relative to the disk root, i.e. it denotes the img directory in the very top level of your hard disk. I doubt that's where the directory is. Don't start the path with a / to make it relative to the file you're working in. File paths are not URLs!
Use absolute path when moving files from one directory to another.
Use dirname(__FILE__) to get the absolute current working directory, then add
the rest of the path.
Set appropriate permission to the directory, as suggested by #Alex. use chmod() and
set permission to 777 (ugo+w) [user+group+others world-writeable].