Forum Moderators: coopster

Message Too Old, No Replies

PHP APC causing duplicate file upload tmp name values

Breaks file uploads as simultaneous users trample over each other

         

whoisgregg

9:51 pm on Sep 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Does anyone have any experience with PHP APC?

I've run into a very odd situation where having APC installed and running caused PHP to always choose the same tmp_name for file uploads.

Here's the settings I'm using:

extension=apc.so
apc.enabled=0
apc.shm_segments=1
apc.shm_size=256
apc.ttl=7200
apc.user_ttl=7200
apc.num_files_hint=1024
apc.mmap_file_mask=/tmp/apc.XXXXXX
apc.stat=1

Any ideas are welcome. APC is much needed on this server, but the file upload problem is a deal killer.

If you know when/where/how php chooses the tmp_name value, that might also help me sort this out. :)

eelixduppy

10:07 pm on Sep 11, 2009 (gmt 0)



Since the tmp_name is the same each time does the file successfully upload to the temp directory? If it does then everything should technically be working, right? Is the file being uploaded to the temp dir with a different name than what the tmp_name is? If the file is there and you can find it, you should be able to handle it correctly, no?

whoisgregg

10:07 pm on Sep 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have also attempted this setting for apc.mmap_file_mask, with no effect:

apc.mmap_file_mask=/tmp/apc.shm.XXXXXX

I also tried leaving it blank which, according to the documentation, forces it to use /dev/zero. Which also had no effect.

whoisgregg

10:12 pm on Sep 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What ends up happening is that every single file upload goes to the same location:

/var/tmp/phpGs5taU

I could move the file once it's done to a new random file name, but I'm still concerned about concurrent uploads (which this site actually gets).

Added: Some additional details. :) Right now my code does a non-trivial amount of validation and processing on that temporary file before moving it to it's final location (if the result of the processing is successful). So it assumes that the provided tmp_name is going to be unique. The reason this was discovered is because when multiple people in-house were using the system at the same time, they would occasionally see someone else's file in the finished result instead of seeing their own.

[edited by: whoisgregg at 10:20 pm (utc) on Sep. 11, 2009]

eelixduppy

10:18 pm on Sep 11, 2009 (gmt 0)



I'm going to try to reproduce the problem on my box. Get back to ya. :)

whoisgregg

10:22 pm on Sep 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I appreciate it. :)

whoisgregg

10:55 pm on Sep 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One of my crazy "what could cause this" theories with this was that perhaps APC was caching the seed of the random number generator... which would cause other problems.

I setup a test page to generate the output of rand() and mt_rand() to see if the values would stay the same on refreshes. Each time I refreshed they would change. So no worries there.

However, then I made this script:

<?php echo tempnam("/var/tmp", "php"); ?>

Each time I refresh that page the output changes. However, if I upload a file then immediately refresh that page, the output of tempnam() is the *exact same* as the tmp_name of the file which was just uploaded! However, refreshing that page then causes the next uploaded file to have a new name instead of the same old name.

So APC seems to be caching the output/seed of the the tempnam script but only when it's called during a file upload. My current workaround is to call tempnam() in my upload script to force the value to change.

whoisgregg

11:15 pm on Sep 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



After rereading my description of the situation, I think I should try to clarify. :) Here's the actions in order and the output:

1. Upload file #1, logged tmp_name => /var/tmp/phppukmKe
2. Upload file #2, logged tmp_name => /var/tmp/phppukmKe
3. Refresh tempnam() script, output => /var/tmp/phppukmKe
4. Refresh tempnam() script, output => /var/tmp/phprukmKe
5. Upload file #1, logged tmp_name => /var/tmp/php99B66G
6. Upload file #2, logged tmp_name => /var/tmp/php99B66G
7. Refresh tempnam() script, output => /var/tmp/php99B66G
8. Refresh tempnam() script, output => /var/tmp/phpxukmKe

So, basically my workaround is just to call tempnam() in my upload script to force a new name to be generated, then delete that file, then handle the file upload like normal.

eelixduppy

9:22 pm on Sep 15, 2009 (gmt 0)



Been sorta busy with a bunch of stuff. I'm assuming this hacky solution is working for you? I'd really like to figure out what the actual problem is. I checked known issues and couldn't really find anything about it, which makes me believe that it is specific to your box and not a bug...but who knows. In any case, if you do happen to stumble upon the solution I'd be really interested in learning what it was.

So I couldn't offer more help, Gregg.

whoisgregg

3:09 pm on Sep 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yup, it's working for now. I also still want to know the problem, because I do have concerns there are other bugs that APC is causing that just haven't shown themselves yet.

Thanks for your help, it was good to have someone to bounce stuff off of. I'll definitely post back if I come up with an explanation. :)

coopster

7:28 pm on Sep 16, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Have you looked through all the apc config directives? This one looks like reviewing ...
[php.net...]