PHP: mysql query skipped/ignored after large file uploads? - php

Weird... Originally i thought that php was not correctly handling large file uploads (800mb-2gb)
now i've figured out that the file is moved correctly, but the mysql query that enters the file info into the DB seems to be skipped when large files are uploaded.
The mysql query is executed like it should be when small files are uploaded. This problem only seems to arise with larger files
Also, the mysql queries before the file is moved seem to work fine.
Process:
Wait for uploaded file,
check file size,
get md5 of file,
move file from temp folder to uploads folder,
if moving file is successful then mysql query.
The file is where is should be, but the query isnt executed.
Should i put a 10 second delay between after the file is moved and when the mysql query is called?

If the file move is working than the problem is most likely on the last step - the MySQL query. Max out the error level with error_reporting(E_ALL) and setup a PHP error log - this will record any MySQL warnings and any other problems. Log the SQL query you're trying to execute. Does it work from a MySQL client?

It sounds like large files are not being moved from the temp folder successfully? Can you confirm this by seeing if the file is still there in /tmp or wherever?

Related

The action cannot be completed because the file is open in httpd.exe

I wrote JS to upload really large videos in chunks and PHP script to assemble the pieces on the server. The file process works except that I can't play the file unless I "Restart All Services" from the PHP taskbar. After that the file plays fine. If I try to rename the file in Windows Explorer I get the follow error:
The action cannot be completed because the file is open in httpd.exe
I suspect it may have something to do with never calling move_uploaded_File(), but that calling that procedure won't work on the assembled file.
Found it myself. Nothing to do with move_uploaded_File not being called. There was a syntax error that stopped the script before the file was closed. Since the server had been set to not display errors it wasn't apparent that an error occurred before the file was closed.
The temporary files are closed(explicitly) after being appended to large file being assembled. It seems that this accomplishes part what move_uploaded_file does, that is, disposing of the temporary file after using it. The script ending probably does that too.

phpmyadmin hangs and only imports about 50% of the database?

I am using linux with phpadmin and trying to import a database. However it hangs about halfway through and just keeps constantly loading. I actually have to reset my computer and when I go check the tables, I get to about letter "F" on the tables list, each and every time. No errors, just a constant loading and a incomplete imported database.
I have already went into php config files and updated the post sizes and upload sizes, ect. But I am still getting this issue. Any help?
phpMyAdmin is limited by the PHP resource limits; memory, execution timeout, and so on. Depending on your situation, your file upload may take so long that the timeout is reached, or uncompressing the file may result in exceeding the memory limits.
You can try a few things:
Uncompress the file locally, then upload the .sql file
Use the "UploadDir" feature to place the file in a folder on your web server, which phpMyAdmin can then access locally.
Upload the .sql file and use the command line client, as suggested by #dognose. I tend to use the 'source' syntax; after connecting to the mysql command line client just use source database.sql. Depending on the .sql file, you may first have to create and/or use the database.

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.

Is it necessary to destroy the temp uploaded file if requirements are not met

I'm uploading files using php. The file has to be below a certain size. If it's not, the script returns an error.
I've noticed that php stores the uploaded file in (my case) C:\Windows\Temp\filename.extension
Right now, if the file is above the Max size, the script only returns an error. So what happens to the file that got uploaded? Does php delete it automatically or do I have to do that? Even if the the file was the right size, the temp file still remains there I guess. Should I be deleting this file in my script?
No, from the manual:
The file will be deleted from the temporary directory at the end of
the request if it has not been moved away or renamed.

Get PHP to wait until a file is done transferring before moving it

I have a PHP script that moves files out of a specific folder on the server(an IBM AS400). The problem I am running into is that sometimes the script runs while the file is still in the process of being moved in to the folder.
Poor logic on my part assumed that if a file was "in use" that PHP wouldn't attempt to move it but it does which results in a corrupted file.
I thought I could do this:
$oldModifyTime = filemtime('thefile.pdf');
sleep(2);
if($oldModifyTime === filemtime('thefile.pdf'){
rename('thefile.pdf','/folder2/thefile.pdf');
}
But the filemtime functions come up with the same value even while the file is being written. I have also tried fileatime with the same results.
If I do Right Click->Properties in Windows the Modified Date and Access Date are constantly changing as the file is being written.
Any ideas how to determine if a file is finished transferring before doing anything to it?
From the PHP manual entry for filemtime():
Note: The results of this function are cached. See clearstatcache() for more details.
I would also suggest that 2 seconds is a bit short to detect whether the file transfer is complete due to network congestion, buffering, etc.
Transfer it as a temporary name or to a different folder, then rename/copy it to the correct folder after the transfer is complete.

Categories