Solved

What does this PHP code do?

Posted on 2011-02-27
4
644 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

864 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now