Jump to content


Photo

A few functions


  • Please log in to reply
4 replies to this topic

#1 Drezard

Drezard
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts

Posted 07 August 2006 - 06:11 AM

Hello im new here and am just looking for somehelp with a few functions:

[A] To create a file (directory) through php.
[B] To delete a file (directory) through php.
[C] Copy a file (.php) through php from one folder to another.

- Thanks, Daniel



#2 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 07 August 2006 - 06:15 AM

ok well you can use mkdir for the creation of a directory. Now as far as Deleting a file. I don't know and would love to know. Give me a minute and I will write you a function for createing a directory and copying a file to it. Is this script going to be on a linux/unix server or windows?


Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#3 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 07 August 2006 - 06:52 AM

here you go. This will create a dir with the name of a session variable and will read an old file and then copy that file to the new directory.

<?php
function newDir() {
   function openfile() {
      $filename = "path\to\old\file\whatever.php";
	  $handle = fopen($filename, "r+b");
	  $res = fread($handle, filesize($filename));
	  fclose($handle)
	  return $res;
   }
   function newfile() {
      $file = "path/to/new/dir/" . $_SESSION['username'] . "/whatever.php";
	  $fp = fopen($file, "x+b");
	  $contents = openfile();
	  $write = fwrite($fp, $contents);
	  if (!$write) {
	      $result = "false";
	  }else{
	      $result = "true";
	  fclose($fp);
	  return $result;
   }	  	  	  	  
   /*I am going to assume that you are going to 
   use session variables to name the directory.
   I will use $_SESSION['username'] */
   $dir = mkdir("path/to/new/dir/" . $_SESSION['username'] . "", 0700);
   //this is assuming that you are using a linux box
   if (!$dir) {
       $result = "Unable to create the directory. You may not have permission to do so";
       exit(1);
   }
   $create = newfile();
   
   if ($create = "false") {
       $result = "Unable to ether create the new file or the old file does not exsit";
   }else{
       $result = "Your new directory was created";
   }
   return $result;
}
$result = newDir();
echo "$result";      	   
?>

I hope this helps.  If anyone knows how to delete a directory or file, please post a snippett.

Good Luck,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#4 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 07 August 2006 - 06:53 AM

I just looked at my post and the damn layout of the script got all messed up when I posted it. Why does this happen? Is there a way around it?

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#5 Drezard

Drezard
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts

Posted 07 August 2006 - 09:14 AM

Wouldnt have a clue sorry.
Thanks So much.

Cheers, Daniel




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users