Solved

What does this PHP code do?

Posted on 2011-02-27
4
647 Views
Last Modified: 2012-05-11
Does this code create a series of nested folders using the variables and create a file to be inside the inner folder that has the filedata written to it?

Thanks




<?php # InternalServerReporting.php
# Copyright 2000-2008 Adobe Systems Incorporated. All rights reserved.
#
   print "<pre>\n";

#
   foreach ($_POST as $k => $v) 
   {
  	  if($k == "CompanyName")
	  {
	    $CompanyName = $v;
      }
      if($k == "DepartmentName")
	  {
	    $DepartmentName = $v;
      }
      if($k == "CourseName")
	  {
	    $CourseName = $v;
      }
      if($k == "Filename")
      {
      	$Filename = $v;
      }
      if($k == "Filedata")
      {
      	if(get_magic_quotes_gpc())
		$Filedata = stripslashes($v);
		else
		$Filedata = $v;
      }
   }

	$ResultFolder = "./"."CaptivateResults";
	mkdir($ResultFolder);
	$CompanyFolder = $ResultFolder."//".$CompanyName;
	mkdir($CompanyFolder);
	$DepartmentFolder = $CompanyFolder."//".$DepartmentName;
	mkdir($DepartmentFolder);
	$CourseFolder = $DepartmentFolder."//".$CourseName;
	mkdir($CourseFolder);
	$FilePath = $CourseFolder."//".$Filename;
	$Handle = fopen($FilePath, 'w');
	fwrite($Handle, $Filedata);
	fclose($Handle);


   print "</pre>\n";
?>

Open in new window

0
Comment
Question by:Karessa
  • 3
4 Comments
 
LVL 6

Accepted Solution

by:
MatthewP earned 500 total points
ID: 34993724
Pretty much yes.

It creates a folder called CaptivateResults in the lowest directory it has access to, then folders named after the values of the variables CompanyName, DepartmentName, CourseName - each nested in the previous one, then a file named after the value for the Filename.variable and dumps the value of Filedata into it. Im not sure why it's using two slashes as separators though normally only one would be required. It would also be wise to test that the results of each action to ensure that the folders are created and the file is actually writeable, also that the file doesn't exist already and is overwritten when this may not be the desired effect.
0
 

Author Comment

by:Karessa
ID: 34994046
Thanks, Matthew.

I can't get the file to work. Maybe the two slashes are the problem, so I'll take them out and see what happens. It would be strange because the file is just as it came from Adobe, but who knows.

Thanks again!
0
 
LVL 6

Expert Comment

by:MatthewP
ID: 34997184
It is accepting variables in POST format - meaning a form needs to be submitted via the POST method.

Near the top under your print "<pre>"; line add

var_dump($_REQUEST);

which will dump out all the data that has been sent so you can see if it's getting through.

It is also expecting the root directory to have write permissions to create the folders and write the data. On linux you must make sure the directory is CHMOD 777 if that means anything to you. I wouldn't know on Windows Im afraid.

Also change things like:

mkdir($ResultFolder);

to:

mkdir($ResultFolder) or die("Cannot create folder $ResultFolder")

so you can see where problems are occurring. At a guess it is either no write permissions or the data isn't getting there.
0
 
LVL 6

Expert Comment

by:MatthewP
ID: 34997192
sorry,

var_dump($_POST);

to display all the data that has been sent via POST. $_REQUEST shows all variables if they are sent by the GET method too, you only need to check for POST.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question