PHP, MoDX: Upload file from custom PHP Snippet - php

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?

Related

php move_uploaded_file always ends up in webroot

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

Failed to open stream when file is clearly there and accessible by URL

I'm getting an "unexplainable" error on this line:
require_once "../Http/Request.php";
Error is failed to open stream, file doesn't exist.
The file is in a directory in the parent folder called "Http". The file is called "Request.php".
I can access it directly by typing the absolute path into my browser, so I can confirm it is indeed uploaded to my server, in the correct folder.
At this point I have to rule out the possibility that the warning/fatal error is accurate and trying to figure out where the problem could be coming from.
Which steps do you suggest to start debugging this?
Google
¦_____ Auth
¦ ¦______ Abstract.php (<- this is the file requiring Request.php)
¦
¦_____ Http
¦_____ Request.php
The thing is that
require_once "../Http/Request.php";
includes the path relative to the current working directory. Which is not guaranteed to be equal to the current file directory.
What you presumably need is
require_once __DIR__ . "/Http/Request.php";
I can access it directly by typing the absolute path into my browser,
so I can confirm it is indeed uploaded to my server, in the correct
folder.
Here’s your problem: You are conflating browser URL path to server file path. And since you say you can access it directly via your browser, then it’s not a file system permissions issue since the web server itself can read the file.
So instead I am going to chalk this up to headache inducing relative path issues. So I would recommend getting rid of this:
require_once "../Http/Request.php";
And instead just set a base path manually in a config file & don’t worry about it—relative paths—ever again.
So as far as a file base path goes, you should explicitly set a $BASE_PATH like this:
$BASE_PATH = '/full/path/to/your/codebase/here/';
If you don’t know what your file system base path is, just place this line of code in your PHP code; like index.php:
echo "Your path is: " . realpath(dirname(__FILE__)) . "<br />";
Then load that page. Somewhere near the top will be this text:
Your path is: /full/path/to/your/codebase/here/
Then with that set you can change your code to be something like this:
And then set your require_once like this:
require_once $BASE_PATH . "Http/Request.php";
Some might say you should use $_SERVER['DOCUMENT_ROOT'] or even dirname(__FILE__) directly with the implication being that you can simplify code portability that way. But the way file paths are set for installs can vary so it just never works well & the chances of you getting snagged on an odd server quirk is high.
It’s always best to just to manually set a $BASE_PATH in a config file when you move code than deal with the headaches caused by PHP constants like $_SERVER not being consistent between installs, setups & configurations.
First You have to check permission on this folder.
If folder has not read permission then give permission for execute and read.
to one level up from google directory structure and type
ls -ltr
First column of result is permission access for google folder and itmust be like
drwxr-xr-x
if not like this then You have to change this by folloing command:
chmod 755 Google
Hope this help.

Find filepath to public_html directory or it's equivalent using PHP

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.

PHP: Cannot see upload directory

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

Trouble finding server root directory

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.

Categories