worldly Posted July 7, 2010 Share Posted July 7, 2010 I have a page that has two iframes on it - The first iframe contains an image upload form that submits to a php script that does nothing more than check for errors. The purpose of this form submission is (or is supposed to be) to just create a $_FILES array containing the image. The second iframe contains another form that submits to another php script. This script needs to use the $_FILES array generated by submitting the image upload form in the first iframe. My understanding of $_FILES is that it is superglobal, but I find that when the php script that is the action of the form in the second iframe tries to access $_FILES, it is empty. If I do a var_dump on $_FILES in the script that is the action of the form in the first iframe, it shows that $_FILES contains values as it should. However when I do a var_dump on $_FILES in the script that is the action of the form in the second iframe, $_FILES is empty. If I start a session in the script accessed by the first form, and read $_FILES into a session variable, I can then retrieve the value of the session variable in the second script. What I don't understand is why can't I retrieve the $_FILES array directly in the second script without having to read it into a session variable? Isn't it a superglobal just like $_session? Can someone educate me on this? TVM Quote Link to comment https://forums.phpfreaks.com/topic/206972-scope-of-_files/ Share on other sites More sharing options...
Adam Posted July 7, 2010 Share Posted July 7, 2010 $_FILES is a super global yes, but it's only available to that particular request. Once PHP has processed the code and sent the output to the browser, the array is removed from memory along with the temporary file you uploaded. Quote Link to comment https://forums.phpfreaks.com/topic/206972-scope-of-_files/#findComment-1082310 Share on other sites More sharing options...
worldly Posted July 7, 2010 Author Share Posted July 7, 2010 It appears that there is no alternative but to create a folder, explicitly save the image to it, then unlink the folder when the script is done with it. I would however, like to fully understand in what sense $_FILES is a "superglobal". Sorry to be so dense, but could you elaborate on what you mean by "that particular request". Do you mean that only the script that is the action of the upload form can access the $_FILES array? (There is no "output to the browser".) Quote Link to comment https://forums.phpfreaks.com/topic/206972-scope-of-_files/#findComment-1082326 Share on other sites More sharing options...
Adam Posted July 7, 2010 Share Posted July 7, 2010 Super global refers to the scope of the variable, meaning that it's available globally throughout the code without needing to pass it to a function or class method (or anywhere you'd be in a different scope to the main application flow) in order to access it. By request I meant a request to the web server where the PHP is then interpreted. That instance of PHP stores what it needs in temporary memory, then the garbage collector will remove it after. It's the same with files you upload to the web server, originally they're stored in a temporary directory and then removed after. If you wish to do something with the file you need to do it there and then; it won't be available on the next request. Session variables can be used to store data you wish to share over an entire session for a particular user, or failing that yes; you'll need to copy the image to your own temporary dir that isn't cleaned up after each request. Can't you just do what you want with the file originally though instead of requiring a different page to call the right action? Also by the way, whether there's visible output or not, response headers are still sent to the browser. Quote Link to comment https://forums.phpfreaks.com/topic/206972-scope-of-_files/#findComment-1082328 Share on other sites More sharing options...
worldly Posted July 7, 2010 Author Share Posted July 7, 2010 The problem I have is that the image that is uploaded by the first form in the sequence must be stored in a folder with a unique name that is dynamically generated (with a timestamp) by the script that is the action of the second form in the sequence. This folder is inside another folder whose name is also dynamically generated by the second script. Right now I can't see any other way than to first, in the script called by the upload form, create a temporary folder with a unique name (so that it will not get overwritten) and save the image to it. Then, in the script called by the second form, move the image to its final location and delete the temporary folder. It seems like there should be an easier way. Quote Link to comment https://forums.phpfreaks.com/topic/206972-scope-of-_files/#findComment-1082346 Share on other sites More sharing options...
Adam Posted July 7, 2010 Share Posted July 7, 2010 Is there a reason why it has to be done over multiple steps like this? Quote Link to comment https://forums.phpfreaks.com/topic/206972-scope-of-_files/#findComment-1082349 Share on other sites More sharing options...
worldly Posted July 7, 2010 Author Share Posted July 7, 2010 Yes. The time it takes the image to upload would delay the completion of the execution of the script called by the second form (this script does a lot of things), and this would be a problem. There is also a need for two forms in separate iframes. Quote Link to comment https://forums.phpfreaks.com/topic/206972-scope-of-_files/#findComment-1082352 Share on other sites More sharing options...
Adam Posted July 7, 2010 Share Posted July 7, 2010 Why can't you generate the final file name for the uploaded file within your first script (where it's actually uploaded), and store it within a session variable for use in the second script? Quote Link to comment https://forums.phpfreaks.com/topic/206972-scope-of-_files/#findComment-1082360 Share on other sites More sharing options...
worldly Posted July 7, 2010 Author Share Posted July 7, 2010 That might be a possibility. I'll give that some thought. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/206972-scope-of-_files/#findComment-1082361 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.