set the upload_tmp_dir but file no where to be found - php

I have a few image upload scripts and wordpress blog as well.
I think the client tried to upload a 3mb+ images or so and since then everythings stopped working, to further inspection and adding error handlers i found out that
it was UPLOAD TEMP DIR not FOUND
i cant touch the ini file, its shared hosting but i can create a local ini file
i've been told to add php.ini in the public_html folder
it only works i.e prints out the upload_tmp_dir when its in the same folder as the script anyway
i put this in the ini file
upload_tmp_dir = "/home/USER/tmp"
also tried
upload_tmp_dir = "/home/USER/public_html/tmp"
but the first one is where the tmp folder was but i tried creating it else where with 777 permissions
leave ini in same folder as the script and calling
echo ini_get('upload_tmp_dir');
gives me: [given i set it to that]
/home/USER/tmp
sys_get_temp_dir();
gave me
/tmp
anyway NOW i get no errors, says file is uploaded but its no where to be found.
not sure WTF is going on but clients wrecking my head and so is this.

PHP will delete uploaded files when the script exits, unless you've moved/copied the file somewhere else yourself. The upload process isn't a "do it now and come back later to deal with it" system. It's "deal with it now, or it's gone".
To retrieve the file's temporary storage location/name, you use $_FILES['namegiveninform']['tmp_name'], and generally would use move_uploaded_file() to move the file into its permanent storage location.

Related

PHP - finfo class not found. unable to upload file after deploying site online

I am building a web application using ExtJS4 and PHP5. There is a part where I upload a photo and as I was testing it locally, I am able to upload photos properly to a folder in the file directory.
After I deployed the file to our server online, I tested it. At first, I was getting a class finfo not found, at it pointed to my upload.php file and and to the line:
$finfo = new finfo(FILEINFO_MIME_TYPE);
Upon googling the problem, I needed to make sure that my php.ini file allowed file uploads. So what I did was I made my own php.ini file in the server and I copy pasted the contents from the php.ini file located in ../MAMP/conf/php5.5.10/php.ini but I got an error that I'm trying to decode an invalid JSON String. I also tried ../MAMP/bin/php/php5.5.10/conf/php.ini but I still get the same error.
I don't understand what's happening. I think I'm copying the wrong php.ini file to my server. I am using Freehostia and we are not allowed to access the php.ini file and it was advised to create my own php.ini file. However, so far, copy pasting my php.ini file did not work. It may be that I'm constructing the file wrong.
It's the fist time I've deployed a site that required a file upload, usually I just work with database CRUD and that works.
I found a PHP Settings Page in the home page. I set the PHP to 5.5 and made sure it allowed file uploading.

Changing PHP Temp Directory

I have a site for media conversion where users can upload video, audio or image files and it is converted in to 3 popular formats. Conversion script works fine but I am having some issues with the tmp directory where the files get uploaded to. I have tried 2 scenarios I am fine with either but neither works and it seems to me to be permissions related but I can seem to fix the problem.
I am working locally right now while I work out the kinks.
Scenario 1:
File is uploaded to default local tmp directory (C:\WINDOWS\tmp) - works fine
Attempt to run conversion script using tmp file that was uploaded and it doesn't work - run from command line works perfectly fine though
Scenario 2:
File is uploaded to directory I have given IIS_IUSRS full control of (for testing) and file won't upload - yes the directory is spelt correctly (I changed the upload_tmp_dir value in php.ini)
Both the site the javascript that send the XMLHttpRequest to the PHP file, as well as the site the PHP file itself reside on are IIS sites so I assume the script is being run as IIS_IUSRS.
EDIT: Temp file is no longer being created at all for Scenario 1, can't figure out why I am assuming playing with permission messed something up because the code hasn't changed. I've given Modify to IIS_USRS and USERS to try and get it working again but no luck :( although the error log is still writing to the same folder...weird
NOTE: The "tmp_name" value of the $_FILES variable I am sending still has a value of "C:\WINDOWS\Temp\'filename'" but the file is not there
EDIT: Another new development, it appears it is NOT a permissions issue because I can create a temp file via $temp_file = tempnam(sys_get_temp_dir(), 'Test'); however it obviously does not contain the uploaded data so it does not solve my problem
PHP is ignoring the upload_tmp_dir because of one setting on APPLICATION POOLS.
It's not php-cgi.exe nor php.ini or a permissions issue.
Go to the application pool of the website experiencing the issue:
1. right click
2. select advanced settings
3. scroll to LOAD USER PROFILE and set it to FALSE.
that did the trick for me.
This is less of a problem solved and more of a workaround. The issue seems to be something with the
$_FILES['file']['tmp_name'];
When I echo the contents it looks as I expect however no file appears. So rather than taking advantage of that process that happens naturally, I have had to take a manual approach. What I have done is the following:
create a temp file
$temp_file = tempnam(ini_get('upload_tmp_dir'), 'php');
then add the content from the temp file created during the $_POST request. (which some how are in the $_FILES variable even though the file is not when I look in the directory)
file_put_contents($temp_file, file_get_contents($_FILES['file']['tmp_name']));
the I have my temporary file for processing.

Where does PHP save temporary files during uploading?

I am using XAMPP on Windows. By printing $_FILES["file"]["tmp_name"], it seems that the temporary file was saved at C:\xampp\tmp\phpABCD.tmp. But I cannot see it on the filesystem of the server. However, the file can be moved or copied via move_uploaded_file(), rename(), or copy(). So where does PHP actually save temporary files during uploading?
It saves it at the path specified in $_FILES["file"]["tmp_name"], but deletes it after the script is done executing. It's up to you to move the file elsewhere if you want to preserve it.
Its specified in upload_tmp_dir in your php.ini file. It is deleted by the system automatically after use.
You can check where php is currently saving your temp files $_FILES["file"]["tmp_name"] by printing
sys_get_temp_dir()
Use move_uploaded_file(file, path), specify the file and the path where you want to store the file.
A copy of that file is created and gets stored.
php stores all temporary files, that includes uploaded files, in the temporary files directory as specified in the php.ini. Note that for uploads, those files might be removed as soon as the script the file was uploaded to was terminated (so unless you delay that script, you probably won't see the uploaded file). Another reason might be that the file is simply hidden on the file system.
So if you want to see the file, you might want to make sure you see all hidden files in the explorer and delay the script as long as you need to find the file.
from http://www.php.net/manual/en/features.file-upload.php#93602, "...the uploaded file will inherit the permissions of the directory specified in the directive upload_tmp_dir of php.ini. If this directive isn't set, the default of C:\Windows\Temp is used..."
Note that the file is saved binary in $_FILES["file"]["tmp_name"], so you may open it maybe with file_get_contents if it is an image or something like this...
IF you are asking the file location then it depend on the setting of server. but if you are asking whether it saved first in local system or in server. then answer is it save in temp folder in server.

file uploaded via php: no such file or directory

i'm working on a website wherein the users can upload images (uses php 4.3.11). the files are uploaded with no problem as i can see them in the upload directory and i don't get any error message, but when i try to access the uploaded files via ftp, i get an error: no such file or directory. sometimes, i am able to access the file sometimes i get this error. what could be the problem here?
[update]
thanks for the help guys. i'm not familiar with the ftp daemon stuff. but i do access my files via ftp using FireFTP. the files are there but when try to download them or change the file properties, i get the said error. i also tried uploading a file in the folder through ftp and i was able to download it with no problem.
here is some of the code i'm working on, its kind of roundabout but i'll see on how to improve it.
my working directory is something like this www.domain.com/register/
and the upload directory is here www.domain.com/register/uploads/
users are required to register and upon sign-up, a folder is created for them in the uploads directory. i couldn't find a way to create a folder without having to be in the uploads folder itself so i redirect to a create-user-folder.php file in the uploads dir.
the file just contained this code:
$user_foldername = rawurldecode($_GET['name']);
mkdir($user_foldername);
header("Location: ../form.php"); // redirect back to the page
i checked and the created folder's permission is set to 775.
and here's part of the code i use in uploading ( /register/function/function.php ):
$path = "../uploads/$user_foldername/";
for($j = 0; $j < $num_of_uploads; $j++){
if(is_uploaded_file($_FILES[$file]['tmp_name'][$j])){
$filename = $_FILES[$file]['name'][$j];
copy($_FILES[$file]['tmp_name'][$j],$path.$filename);
}
}
i checked using FireFTP and the files are in the /uploads/user_foldername/ directory and its permission is set to 664. the strange thing is that when i try to download the files, at times there would be no problem at all but there are times when the error will appear.
[another update]
i added chmod() after the copy() function,
$filename = $_FILES[$file]['name'][$j];
copy($_FILES[$file]['tmp_name'][$j],$path.$filename);
chmod($path.$filename, 0755);
but i still get the error.
another thing is that when i access /register/uploads/user_foldername/ through the url, i can see all of the uploaded files and view them, but how is it that i can't access them via ftp?
thanks again!
This is either a permission issue, or a configuration error. Here are things you should try:
What are the permission of the uploaded files? Does the FTP user has access to these files? Have you tried logging in as the user the FTP daemon would use and see if you could read the file that way?
Do you really see the correct directory? Have you verified by putting a file in that directory yourself and downloading it? Have you used the ftp command ls to verify the presence of the folder/folders/files?
You might need to chmod the folder the files are in, or in some cases the files themselves.
try chmoding them to 775
You can chmod files and folders through PHP it's self, with the chmod function. Or, you could use a FTP program such as filezilla.
Also check to make sure the intermediate directories are also permissioned as 755, as all the directories in the path need to be executable to be traversed.
i just figured out the problem. it was all because of the file name having accented characters in it, which explains why i do not always get the error message :|
<sigh> i should have seen this earlier, but anyway i hope this helps in case someone ran into the same problem.
thanks again! i really appreciate it :)

PHP - UPLOAD_ERR_NO_TMP_DIR

I am using Cbeyond (www.cbeyond.com, some of you might be familiar with them) as a PHP Enabled webhost, I'm having issues with my PHP Upload functions, when I check as follows:
$error = $_FILES['uploadedfile']['error'];
echo $error;
I get "6", for the error message: "UPLOAD_ERR_NO_TMP_DIR"
I have a /tmp directory at my root, here is the file structure:
/ <--- ftp root (contains a working /tmp)
/www/htdocs/ <-- webroot
I've tried creating:
/www/htdocs/tmp (no luck)
Is there anyway to create a tmp location on the fly, I just need to parse an uploaded .txt file into my MySQL DB.
You probably need to check with CBeyond whether they allow file uploading or not. If so, check with them whether their php.ini File Uploads section has upload_tmp_dir properly defined.

Categories