Jump to content

$_FILES into session variables


kirkh34

Recommended Posts

i am using uploadify to upload multiple images at once,  the javascript accesses a php file to that preforms the upload.. i want to be able to take certain information from the $_FILES array and put it into a session variable...

 

i need help understand how one would possible write this code...could it establish the first session variable, then when the next image comes to be uploaded it could check to see if the first sesssion variable exists and if so establish another session variable and so on....

 

 

EDIT: MORE LIKE...

maybe...

$_SESSION['var1'] = $_FILES['file']['name']; 
                        $_SESSION['var2'] = $_FILES['file']['name'];

 

an if statement? i would need a statement that could also work so i could unregister the sessions

 

also... how could i then grab this data when i need it on another page... i'm thinking maybe some kind of loop but i'm not sure... any help is appreciated.. thanks!

 

 

 

Link to comment
Share on other sites

You would use an array (always when you have sets of related values.)

$_SESSION['var1'][] = "data";
$_SESSION['var2'][] = "moredata";

 

Each time through another index 0, 1, 2, ... will be added to each base array variable. You will end up with -

$_SESSION['var1'][0] = "data"; // 1st set of data
$_SESSION['var2'][0] = "moredata"; // 1st set of data

$_SESSION['var1'][1] = "data"; // 2nd set of data
$_SESSION['var2'][1] = "moredata"; // 2nd set of data

$_SESSION['var1'][2] = "data"; // 3rd set of data
$_SESSION['var2'][2] = "moredata"; // 3rd set of data
...

 

You must do something (move to a folder, save as a session variable, store in a database table) with the temporary uploaded file because it will be deleted when the script on your page ends.

Link to comment
Share on other sites

im sorry i'm trying to wrap my head around this but can't seem to fully understand... maybe if i post my actual code as an example it would help me

<?php
/*
Uploadify v2.1.0
Release Date: August 24, 2009

Copyright (c) 2009 Ronnie Garcia, Travis Nickels

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/





session_id($_REQUEST['session_name']);
session_start();



if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . '/uploadify' . $_REQUEST['folder'] .  '/' ;
$targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];

$ext = pathinfo($_FILES['Filedata']['name'], PATHINFO_EXTENSION);  //figures out the extension
   
   $newFileName = md5($tempFile).'.'.$ext; //generates random filename, then adds the file extension
   $targetFile =  str_replace('//','/',$targetPath) . $newFileName;

// $fileTypes  = str_replace('*.','',$_REQUEST['fileext']);
// $fileTypes  = str_replace(';','|',$fileTypes);
// $typesArray = split('\|',$fileTypes);
// $fileParts  = pathinfo($_FILES['Filedata']['name']);

// if (in_array($fileParts['extension'],$typesArray)) {
	// Uncomment the following line if you want to make the directory if it doesn't exist
	// mkdir(str_replace('//','/',$targetPath), 0755, true);

	if ($newFileName)
   				echo $newFileName;
	else // Required to trigger onComplete function on Mac OSX
   				echo '1';



	move_uploaded_file($tempFile,$targetFile);
	echo "1";
// } else {
// 	echo 'Invalid file type.';
// }
}






?>

 

i would like

$_FILES['Filedata']['name']

=

$_SESSION['img_name']

 

...and how could i print this... it doesn't seem like printing $_SESSION['img_name'] would print all the names of the images that were uploaded

Link to comment
Share on other sites

You can't set $_SESSION['img_name'] with the name of each file for multiple uploads. Each will overwrite the previous.  It would have to go in a multidimensional array, such as this. I hope this makes more sense.

 

$_SESSION['img_name'][] = 'file1.jpg';
$_SESSION['img_name'][] = 'file2.jpg';
$_SESSION['img_name'][] = 'file3.jpg';
$_SESSION['img_name'][] = 'file4.jpg';
$_SESSION['img_name'][] = 'file5.jpg';

/*
print_r($_SESSION) returns:

Array
(
    [img_name] => Array
        (
            [0] => file1.jpg
            [1] => file2.jpg
            [2] => file3.jpg
            [3] => file4.jpg
            [4] => file5.jpg
        )

)

Link to comment
Share on other sites

i added this one line of code

 

$_SESSION['img_name'][] = $newFileName;

 

and that's enough to work for me...

 

 

<?php 
            
          

            foreach($_SESSION['img_name'] as $key=>$value)
    {
    //print out values
    echo $value . '<br />';
    }
            
            
            ?>

 

and i used this loop and everything works great, thanks for the help understanding how this all works!

Link to comment
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.