fagnonk Posted May 24, 2009 Share Posted May 24, 2009 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?! Quote Link to comment Share on other sites More sharing options...
Andy-H Posted May 24, 2009 Share Posted May 24, 2009 error_reporting(E_ALL); // Add at the top... Quote Link to comment Share on other sites More sharing options...
fagnonk Posted May 24, 2009 Author Share Posted May 24, 2009 hmmm... here is the error I am getting: 'Notice: Undefined variable: targetFile in ...' Im not sure how this could be affecting session variables, this part of the script handles thumbnail generation. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted May 24, 2009 Share Posted May 24, 2009 if (!empty($_FILES)) { print_r($_FILES); // etc... } Add that to debug and post back the output. Also, read up on it http://www.w3schools.com/PHP/php_file_upload.asp << Quote Link to comment Share on other sites More sharing options...
fagnonk Posted May 24, 2009 Author Share Posted May 24, 2009 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? Quote Link to comment Share on other sites More sharing options...
Andy-H Posted May 24, 2009 Share Posted May 24, 2009 Print the data out from the $_FILES array. It must be an empty array in that case... Quote Link to comment Share on other sites More sharing options...
fagnonk Posted May 24, 2009 Author Share Posted May 24, 2009 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. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted May 24, 2009 Share Posted May 24, 2009 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... Quote Link to comment Share on other sites More sharing options...
fagnonk Posted May 24, 2009 Author Share Posted May 24, 2009 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 Quote Link to comment Share on other sites More sharing options...
Andy-H Posted May 24, 2009 Share Posted May 24, 2009 <?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'; ?> Quote Link to comment Share on other sites More sharing options...
fagnonk Posted May 24, 2009 Author Share Posted May 24, 2009 Wow that worked! Thanks alot. You added: $str = (empty($_FILES)) ? "Test" : $_FILES['Filedata']['name']; How did this change the array problem? Quote Link to comment Share on other sites More sharing options...
fagnonk Posted May 24, 2009 Author Share Posted May 24, 2009 Also, how do I replace the 'Test' text with the filenames of uploaded files? I know you set it up with '$_FILES['Filedata']['name'];' but it doesn't seem to be write that data to the xml file. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted May 24, 2009 Share Posted May 24, 2009 Obviously if there are no files uploaded the $_FILES array is unpopulated... Thats probably because I am not uploading any files when I directly run upload.php You answered your own question. lol Quote Link to comment Share on other sites More sharing options...
fagnonk Posted May 24, 2009 Author Share Posted May 24, 2009 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 Quote Link to comment Share on other sites More sharing options...
Andy-H Posted May 24, 2009 Share Posted May 24, 2009 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> Quote Link to comment Share on other sites More sharing options...
fagnonk Posted May 24, 2009 Author Share Posted May 24, 2009 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'; ?> Quote Link to comment Share on other sites More sharing options...
fagnonk Posted May 26, 2009 Author Share Posted May 26, 2009 Ok....If anyone is interested, this is a bug with flash. I solved it by creating an extra variable and passing it through flash. Flash clears all session variables, cookies, etc.... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.