create folder / subfolder on the fly to store files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
inosent1
Forum Commoner
Posts: 97
Joined: Wed Jan 28, 2009 12:18 pm

create folder / subfolder on the fly to store files

Post by inosent1 »

here is my code so far

Code: Select all

<?php
include("dbinfo.inc.php");


mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$term  = mysql_real_escape_string($_POST['term']);
$term2  = mysql_real_escape_string($_POST['term2']);

  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    //echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    //echo "Type: " . $_FILES["file"]["type"] . "<br />";
    //echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    //echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("efiles/" . $term . "/" . $term2 . "/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "efiles/" . $term . "/" . $_FILES["file"]["name"]);
      //echo "Stored in: " . "efiles/" . $_FILES["file"]["name"];
      }
    }
  
echo $term;

?> 
<html><body>
<script type="text/javascript">

</script>
</body></html>



where you see 'term and 'term2' are from user inputs. the concept is to have the files uploaded to folders with the terms the user states, which may not be created yet, but if they are i dont want to create them, just check to see if the file, then, has been uploaded to that location, etc

any ideas are welcome
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: create folder / subfolder on the fly to store files

Post by phphelpme »

Here is an example of creating a directory:

http://php.net/manual/en/function.mkdir.php

Best wishes
inosent1
Forum Commoner
Posts: 97
Joined: Wed Jan 28, 2009 12:18 pm

Re: create folder / subfolder on the fly to store files

Post by inosent1 »

thanks, i got it
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: create folder / subfolder on the fly to store files

Post by califdon »

Do you really want to allow users to create directories on your server?? Generally I would consider this a really really bad practice. A hacker could write a simple little script to create thousands of directories on your server, unless you have taken other precautions to prevent it.
Post Reply