Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/206972-scope-of-_files/
Share on other sites

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".)

Link to comment
https://forums.phpfreaks.com/topic/206972-scope-of-_files/#findComment-1082326
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/206972-scope-of-_files/#findComment-1082328
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/206972-scope-of-_files/#findComment-1082346
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.