Accessing/Saving form uploaded files

CommonToni

Joined: 2008-05-19
Posts: 2
Posted: Mon, 2008-05-19 16:47

Hi - I'm currently developing a module that needs to save a file that's been specified in an input tag of file type off a form. I have the form all setup, and normally in PHP at this point I would use move_uploaded_file and specify the $_FILES indexed at 'tmp_name' to grab the temporary file. However, I'm assuming that Gallery 2 intercepts this already and I'm required to use API to get the temporary file, since $_FILES appears to be empty by the time my controller code looks at it. Any ideas how to save an uploaded file? I apologize if this is already posted somewhere and I missed it.

Thanks!

 
alecmyers

Joined: 2006-08-01
Posts: 4342
Posted: Mon, 2008-05-19 23:42

The best way to answer queries like this is to deconstruct what Gallery already does in a similar situation - after all, you have the source code. So trace through what happens when you upload an image file, and you'll get the "Gallery" way of doing it.

 
CommonToni

Joined: 2008-05-19
Posts: 2
Posted: Tue, 2008-05-20 01:48
alecmyers wrote:
The best way to answer queries like this is to deconstruct what Gallery already does in a similar situation - after all, you have the source code. So trace through what happens when you upload an image file, and you'll get the "Gallery" way of doing it.

I appreciate your response - I actually took a look at the source before I posted this and found a call to a $files variable that appeared to mimic the functionality of $_FILES. However, when I made use of this variable from my module code, it was empty.

Since I ran into a number of dead ends, I was hoping that someone already had an answer to this question. I imagine there are other modules that make use of file uploads, I just don't know of any since I am new to Gallery 2. Thanks again

 
alecmyers

Joined: 2006-08-01
Posts: 4342
Posted: Tue, 2008-05-20 09:42

Looking at the way items are added in the ItemAddFromBrowser code:

Firstly main.php does all the work for you by calling $form = GalleryUtilities::getFormVariables('form'); which makes sure that the contents of $_FILES are returned to you automatically in the $form variable - see this snippet of modules/core/ItemAddFromBrowser.inc (lines 51-60):

	    for ($i = 1; $i <= count($form['name']); $i++) {
		$newItem = null;

		/* Placeholder for later */
		if (!empty($form['tmp_name'][$i]) && !empty($form['size'][$i])) {
		    $file = array('name' => $form['name'][$i],
				  'type' => $form['type'][$i],
				  'tmp_name' => $form['tmp_name'][$i],
				  'size' => $form['size'][$i],
				  'caption' => $form['caption'][$i]);

I guess you could just pretty much copy that approach.