Jump to content

[SOLVED] Session Variable Problem


fagnonk

Recommended Posts

I can't get my sessions variables to work in a script I am working on:

 

<?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?!

Link to comment
https://forums.phpfreaks.com/topic/159438-solved-session-variable-problem/
Share on other sites

I added it like this:

 


    if (!empty($_FILES)) {
        print_r($_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);
    }

 

There was no change in the output. Is this what you were asking me to do?

Thats probably because I am not uploading any files when I directly run upload.php. To make things clearer, I have a dropdown menu with a list of xml files next to my upload box. When the user selects an xml files from the dropdown box, that selection is loaded to a session variable and the user is then returned to the same upload screen. When the user uploads some files, they get passed to upload.php where two things happen: thumbnails are created and the filenames of products are added to the XML file which has been stored in the session variable.

 

So when I directly access upload.php with my browser, I suspect the array is empty because no images are being uploaded. Could this stil be a problem with the $_FILES array? I am looking for documentation about arrays but I can't seem to find any reason this could be causing interference.

Then the problem is that you are trying to use the $targetFile variable outside of its scope.

 

    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))){


 

As you can see, the value set to $targetFile is set if the $_FILES array is Not empty.

Then immidiately after closing that code block you are using the variable pretty much throughout the whole file without the variable having been created...

Ahh, I see.. Any theories as to why this would cause problems with my session variable? I got that part of script from the original creator of my flash uploader and no one else seems to be experiencing any problems with it:

 

http://www.uploadify.com/forum/viewtopic.php?f=5&t=33

<?php
error_reporting(E_ALL);
session_start();
$dor = $_SESSION['views'];


    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);
    }

if ( isSet($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

}

$str = (empty($_FILES)) ? "Test" : $_FILES['Filedata']['name'];
$fp = fopen($dor, 'a+');
fwrite($fp, $str);
fclose($fp);
echo '1';

?>

 

Haha yeah, I just realized that. Now I tried it with the uploading script and I am still not getting anything written to the xml file. Try it out:

 

http://cstang.hk/imgupload/dropdown.php

 

Make sure to select tester.xml at the bottom of the dropdown and hit back when you get redirected to xmlnamer.php.

 

the xml file wll be located at: uploadify/tester.xml

Thats because you are not creating the form correctly for use of the $_FILES array, as far as I know uploading files only works with postdata...

 

Change:

 		<form name="myform" action="uploadify/xmlnamer.php" method='get' onChange="document.myform.submit()">
     		<select name="selectBox" id="mySelect">
     			<option>loading</option>
     		</select>
 		</form>

To:

<form   name="myform" action="uploadify/xmlnamer.php" method="post"
enctype="multipart/form-data">
     		<select name="selectBox" id="mySelect" onChange="document.myform.submit()">
     			<option>loading</option>
     		</select>
 		</form>

I changed the drop menu to post instead of get and that seems to be passing the session variable successfully. However, upload.php still fails to create an xml file at upload. I should probably point out that neither the session variable nor the dropdown menu have anything to do with the uploader. The uploader is an opensource flash uploader (uploadify.org). I placed it on the page using some clever javascript that replaces:

<div id="fileUpload2">You have a problem with your javascript</div>

with my flash upload application. The only purpose of the session variable is to pass on the name of the xml file which the user would like to append uploaded filenames.

 

If I manually run upload.php it will write a file using the name contained in the session variable but since the  $_FILES array is empty, it just writes 'test'.

 

Just to keep track of changes, my most current upload.php looks like this:

<?php
error_reporting(E_ALL);
session_start();
$dor = $_SESSION['views'];


    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);
    }

if ( isSet($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

}

$str = (empty($_FILES)) ? "Test" : $_FILES['Filedata']['name'];
$fp = fopen($dor, 'a+');
fwrite($fp, $str);
fclose($fp);
echo '1';
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.