Page 1 of 1

Sessions Variables

Posted: Sat May 23, 2009 10:39 pm
by fangonk
I can't get my sessions variables to work in a script I am working on:

Code: Select all

 
<?php
session_start(); // start up PHP session
$dor = $_SESSION['views'];
echo $dor;
 
    if (!empty($_FILES)) {
        $tempFile = $_FILES['Filedata']['tmp_name'];
        $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_GET['folder'] . '/';
        $targetPath =  str_replace('//','/',$targetPath);
        $targetFile =  $targetPath . $_FILES['Filedata']['name'];
        move_uploaded_file($tempFile,$targetFile);
    }
 
    $imgsize = getimagesize($targetFile);
    switch(strtolower(substr($targetFile, -3))){
        case "jpg":
            $image = imagecreatefromjpeg($targetFile);    
        break;
        case "png":
            $image = imagecreatefrompng($targetFile);
        break;
        case "gif":
            $image = imagecreatefromgif($targetFile);
        break;
        default:
            exit;
        break;
    }
 
    $width = 80; //New width of image    
    $height = $imgsize[1]/$imgsize[0]*$width; //This maintains proportions
 
    $src_w = $imgsize[0];
    $src_h = $imgsize[1];
        
 
    $picture = imagecreatetruecolor($width, $height);
    imagealphablending($picture, false);
    imagesavealpha($picture, true);
    $bool = imagecopyresampled($picture, $image, 0, 0, 0, 0, $width, $height, $src_w, $src_h); 
 
    if($bool){
        switch(strtolower(substr($targetFile, -3))){
            case "jpg":
                header("Content-Type: image/jpeg");
                $bool2 = imagejpeg($picture,$targetPath."Thumbs/".$_FILES['Filedata']['name'],80);
            break;
            case "png":
                header("Content-Type: image/png");
                imagepng($picture,$targetPath."Thumbs/".$_FILES['Filedata']['name']);
            break;
            case "gif":
                header("Content-Type: image/gif");
                imagegif($picture,$targetPath."Thumbs/".$_FILES['Filedata']['name']);
            break;
        }
    }
 
    imagedestroy($picture);
    imagedestroy($image);
 
    echo '1'; // Important so upload will work on OSX
 
$dor = $_SESSION['views'];
$str = $_FILES['Filedata']['name'];
$fp = fopen($dor, 'a+');
fwrite($fp, $str);
fclose($fp);
echo '1';
 
?>
 
The sessions variable contains the name of a file which I am trying to edit. When I put a static file name in $fp = fopen(myfile.xml, 'a+'); the script works just fine but when I try to feed my session variable in to this field, no file is being created. I have tried 'echo $dor' and the session variable seems to be successfully passed to the script. However this 'echo $dor' works only at the top of the file, if I place the same line near the bottom it no longer works. Any ideas what is going on here?!

Re: Sessions Variables

Posted: Sun May 24, 2009 8:22 am
by watson516
Does $_SESSION['views'] contain any data?

Re: Sessions Variables

Posted: Sun May 24, 2009 8:36 am
by straightman
well, you are actually passing a new value to $dor, namely $_SESSION['views']

then, what is the value of that $_SESSION['views'] ? you will not be expecting that $dor maintains its value as whet it was at the top of the file.

Re: Sessions Variables

Posted: Sun May 24, 2009 9:58 am
by fangonk
$_SESSION['views'] is a variable that contains the name of an xml file. Essentially this upload.php has three functions:
1. Upload images
2. Create Thumbnails
3. Record Filenames of Uploaded files to a dynamically selected xml file.
The $dor variable is meant to hold the name of the xml file contained in the sessions variable so that I can pass it in to the xml parser:

Code: Select all

# $str = $_FILES['Filedata']['name'];
# $fp = fopen($dor, 'a+');
# fwrite($fp, $str);
# fclose($fp);
I should probably also add this the data contained in $_SESSION['views'] is selected by the user from a drop down menu. I have used echo $_SESSIONS['views'] and it does contain the correct data. I have also echoed '$dor' - it only works at the top of the script. If I use echo $dor near the bottom of the scritpt, nothing happens. I have no way of knowing if this is significant.