How to change the temp upload path runtime? - php

I would like to change my applications temp path to a subfolder, so that users on a shared server cannot see any uploaded files.
I would like to be able to do this run-time, or via .htaccess if possible (although I would like the new temp path to be a subdir of the original temp path). I can't edit the php.ini on the shared server.
I know I can check what the tmp path is via sys_get_temp_dir(), but there doesn't seem to be a way to set it.
Is this even possible?

ini_set('upload_tmp_dir','your/path/here/');
The temporary directory used for
storing files when doing file upload.
Must be writable by whatever user PHP
is running as. If not specified PHP
will use the system's default.
If the directory specified here is not
writable, PHP falls back to the system
default temporary directory. If
open_basedir is on, then the system
default directory must be allowed for
an upload to succeed.
upload_tmp_dir

maybe in '11, now not anymore.
As documented here ini.list
and with reference to modes,
'upload_tmp_dir' cannot be changed at run time.
thanks.

Related

PHP error uploading file to a folder error: failed to open stream: Permission denied (changing permissions on windows won't help either)

I'm currently having an issue trying to upload a file (an image) and sending it to a folder, this for a CMS/blog where people can comment and create profiles with pictures, unfortunately, XAMPP won't allow me to send it and it displays this message
`Warning: move_uploaded_file(../images/ ): failed to open stream: Permission denied in C:\xampp\htdocs\CMS\CMS_TEMPLATE\admin\includes\add_post.php on line 19
Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\php37A6.tmp' to '../images/ ' in C:\xampp\htdocs\CMS\CMS_TEMPLATE\admin\includes\add_post.php on line 19
I know is a problem with the permission or privileges to read and change files, but even when I changed those permissions on the folder to let it write and modify files, the error messages still appear, I'm working on windows 8.1 with XAMPP and I haven't found a solution to this, it would be really helpful if anyone could help me. Also here's the code if anyone needs to see it.
<?php
if(isset($_POST['create_post'])) {
$post_title = $_POST['title'];
$post_author = $_POST['author'];
$post_category_id = $_POST['post_category_id'];
$post_status = $_POST['post_status'];
$post_image = $_FILES['post_image']['name'];
$post_image_temp = $_FILES['post_image']['tmp_name'];
$post_tags = $_POST['post_tags'];
$post_content = $_POST['post_content'];
$post_date = date('d-m-y');
$post_comment_count = 4;
move_uploaded_file($post_image_temp, "../images/ " );
}
?>
<div class="form-group">
<input type="file" class="form-control" name="post_image">
</div>
Thank you!
The function move_uploaded_file is available in
(PHP 4 >= 4.0.3, PHP 5, PHP 7, PHP 8)
and in PHP's official documentation defined as following
move_uploaded_file(string $from, string $to): bool:
This function checks to ensure that the file designated by from is a
valid upload file (meaning that it was uploaded via PHP's HTTP POST
upload mechanism). If the file is valid, it will be moved to the
filename given by to. This sort of check is especially important if
there is any chance that anything done with uploaded files could
reveal their contents to the user, or even to other users on the same
system.
This function is open_basedir aware. However, restrictions are
placed only on the to path as to allow the moving of uploaded files in
which from may conflict with such restrictions. move_uploaded_file()
ensures the safety of this operation by allowing only those files
uploaded through PHP to be moved.
Return Values
This functon returns true on success.
1. If from is not a valid upload file
Then no action will occur, and move_uploaded_file(...) will return false.
2. If from is a valid upload file, but cannot be moved for some reason
Then no action will occur, and move_uploaded_file(...) will return false. Additionally, a warning will be issued (#MiguelDavid your case).
Referring to open_basedir string
Limit the files that can be accessed by PHP to the specified directory-tree, including the file itself. This directive is NOT affected by whether Safe Mode is turned On or Off.
When a script tries to access the filesystem, for example using include, or fopen(), the location of the file is checked. When the file is outside the specified directory-tree, PHP will refuse to access it. All symbolic links are resolved, so it's not possible to avoid this restriction with a symlink. If the file doesn't exist then the symlink couldn't be resolved and the filename is compared to (a resolved) open_basedir.
open_basedir can affect more than just filesystem functions; for example if MySQL is configured to use mysqlnd drivers, LOAD DATA INFILE will be affected by open_basedir. Much of the extended functionality of PHP uses open_basedir in this way.
The special value . indicates that the working directory of the script will be used as the base-directory. This is, however, a little dangerous as the working directory of the script can easily be changed with chdir().
In httpd.conf, open_basedir can be turned off (e.g. for some virtual hosts) the same way as any other configuration directive with "php_admin_value open_basedir none".
Under Windows, separate the directories with a semicolon. On all other systems, separate the directories with a colon. As an Apache module, open_basedir paths from parent directories are now automatically inherited.
The restriction specified with open_basedir is a directory name, not a prefix. The default is to allow all files to be opened.
open_basedir can be tightened at run-time. This means that if open_basedir is set to /www/ in php.ini a script can tighten the configuration to /www/tmp/ at run-time with ini_set(). When listing several directories, you can use the PATH_SEPARATOR constant as a separator regardless of the operating system.
Also take a look at upload_tmp_dir string
The temporary directory used for storing files when doing file upload. Must be writable by whatever user PHP is running as. If not specified PHP will use the system's default.
If the directory specified here is not writable, PHP falls back to the system default temporary directory. If open_basedir is on, then the system default directory must be allowed for an upload to succeed.
In your case xampp temporary directory is located:
C:\xampp\tmp and it,s also writable, so nothing to do there!
... To fix your issue / finish ...
Now that function move_uploaded_file is open_dir aware as already mentioned, give the directory for your images the appropriate owner permissions (e.g.: 0755):
../images/
This will get you out of the issue!
According to Dan Delaney on https://www.php.net/manual/en/function.move-uploaded-file.php#86332 you might need to set the "upload_tmp_dir" to an existing directory within you websites directory structure, since you are running on Windows.
Search for "upload_tmp_dir" in your php.ini file and set it to a path pointing to an existing directory:
upload_tmp_dir = "path_to_your_custom_tmp_dir"

tmp folder in apache is getting full with files named /tmp/php*. How can I find which session or file is creating them?

my server tmp folder (capacity of 4GB) gets full very fast (within a few hours). There are some small session files in there, but the ones which take up all the space are files named "php[UNIQUE_CODE]".
I already changed in php.ini both the "upload_tmp_dir" and "session.save_path" to another directory, but these "php*" files are still emerging. The session files are saved in the new directory.
I have owncloud and wordpress installed on the server since over a year, but this is something that occurs the last days.
How can I check which file or session is creating these large files on the tmp folder?
Thanks!
J
it is not related to session
these files are related to io
since i am using "fopen($filePath, 'rb')" in my code , these files are creating by system and will be delete automatically (if it has enough space on /tmp folder)
if you want to change /tmp for these type of file you can change below params in your .ini file and restart apache
upload_tmp_dir='/tmp2/'
sys_temp_dir='/tmp2/'
and be sure your folder has 777 + t permission , and user/group can be 'root'

Joomla tmp folder not writable? or open_basedir troubles?

i'm working on this joomla site and im not able to upload any extension. if i use normal upload method i get JFolder::create: Could not create directory
Unable to create destination
if i use upload from directory i get Copy failed
JInstaller: :Install: Failed to copy file
i have tried so many solutions found in joomla support forum but none worked for me.
in desperation i even changed tmp ermissions into 777 and now directory permissions (i know its bad) list shows that tmp is writable but show the warning The PHP temporary directory is not writeable by the Joomla! instance, which may cause issues when attempting to upload extensions to Joomla!. If you are having issues uploading extensions, check the '/tmp' and set it to be writeable and see if this fixes the issue. in extensions manager-> warnings
i was wondering whether open_basedir in defect. In my php info file i have
/srv/www/vhosts/domain/httpdocs/:/tmp/ - no value . how can i know open_basedir is in defect? and how can i solve this extensions matter?
The problem may be because upload_tmp_dir isn't set in php.
Look in SITE > SYSTEM INFORMATION > PHP INFORMATION and check if upload_tmp_dir has been set. If not, you need to edit php.ini
On our servers (which use open base dir), the setting is:
upload_tmp_dir=/tmp
This value could be different for you, depending on your server configuration.
Set permission to 777
use full path for logs and tmp e.g.:
/var/www/vhosts/mydomain/httpdocs/tmp
In the Joomla Backend, go to:
Site >> System Information >> Directory Permissions
and see if the "tmp" folder says "Writable"
I had the same problem with one of my shared hosts. The issue was that even though I had set literally everything to 777 (purely for testing purposes), I didn't have file ownership. If this is the case for you too, then you will have to talk to your hosting provider.

php access files outside of apache

I have a project where Red5 is recording videos. I need PHP to be able to access the videos and move them so they can be accessed by HTML.
How can I do this?
I found this post: Accessing files outside the document root with Apache
But it involves updating some file that was never specified. And I'm not sure it is a viable solution in this case anyway.
lee
PHP by default can already access files outside the web root, unless restricted with an open_basedir directive (or safe mode, but hope you're not in that cage).
It's normally a good practice to insert within a VirtualHost configuration an open_basedir restriction. You can specify multiple directories separated by : on Linux and ; on windows.
php_admin_value open_basedir /var/www/s/stage:/usr/share/php:/your/dir
To access those files either use an absolute path or a path relative to the position of the PHP file called. (So you'll have to ../ to reach levels above).
Also be sure that directories in which you want to write to are assigned to the webserver user and have write permission.

PHP way to find the web server's temp path?

When you handle an HTTP upload the file is uploaded to
$_FILES['field_name']['tmp_name']
I know I could extract the temp path from there. But I was expecting maybe a $_SERVER param that had the temp path (there's none) or other elegant way of knowing it.
Is there any?
ini_get('upload_tmp_dir');
The function sys_get_temp_dir() returns the directory path used by PHP to store temporary files.
Using XAMPP on my private computer, the ini_get method worked great, since php.ini specifies the value upload_tmp_dir. However, after uploading to my employer's server, this code didn't work because the value apparently didn't exist in his server's php.ini file. So, I used the system's temp dir as the default, and now the script works on my computer and on his server:
$ini_val = ini_get('upload_tmp_dir');
$upload_tmp_dir = $ini_val ? $ini_val : sys_get_temp_dir();
I personally would use Sbm007's suggestion of:
ini_get('upload_tmp_dir');
As this takes into account apache per virtualhost settings like the upload_tmp_dir and open_basedir restrictions, where as :
sys_get_temp_dir()
Would only return the OS's specific temp directory, and if inside a multi-hosted environment could give you a directory you can't write to.

Categories