Jump to content

Create Directory and File upload


sanchez77

Recommended Posts

So I'm a little confused and hoping someone can help me understand. I have a form that I use to upload a file, html with browse button, etc. The php file creates a directory based on the a value (lastname) from the form, so the folder files creates a directory named the value lastname. My problem is that I can't get the upload function to put the file in the newly created directory. So here is the upload script.

 


$foldername = $_POST['lastname'];
// Desired folder structure
$structure = 'files/'.$foldername.'';
// To create the nested structure, the $recursive parameter 
// to mkdir() must be specified.
if (!mkdir($structure, 0777, true)) {
    die('Failed to create folders...');
}

$target = "files/"; 
$target = $target . basename( $_FILES['userfile']['name']) ; 

if(move_uploaded_file($_FILES['userfile']['tmp_name'], $target)) 
{ 
echo "<center>The file ". basename( $_FILES['userfile']['name']). " has been uploaded.</center>"; 
} 
else 
{ 
echo "No File was uploaded"; 
} 

 

So when I edited the target, it just changed the filename . What should I change in the code above to upload the file to the new directory?

 

Thanks again,

Sanchez

Link to comment
https://forums.phpfreaks.com/topic/219643-create-directory-and-file-upload/
Share on other sites

it's untested but should work.

 

<?php
$target = '/files/';
$foldername = $_POST['lastname'];
$structure = $target.$foldername.'/';
if (!mkdir($structure, 0777, true))
   {
      die('Failed to create folders...');
   }
if(move_uploaded_file($_FILES['userfile']['tmp_name'], $structure.basename( $_FILES['userfile']['name']))
   {
      echo "<center>The file ". basename( $_FILES['userfile']['name']). " has been uploaded.</center>"; 
   } 
else 
   { 
      echo "No File was uploaded";
   }
?>

If there was no error, you have error reporting/display errors set to off, or only enabled in using ini_set(). You should be developing with error reporting set at E_ALL && E_STRICT in the php.ini file whenever possible.

 

[hint] Count the parentheses on the line that starts with: if(move_uploaded_file

opps, thanks for the hint.

 

But now it says it uploaded the file, but it didn't upload the file or create the directory.

 


<?php
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);
$target = '/files/';
$foldername = $_POST['lastname'];
$structure = $target.$foldername.'/';
if (!mkdir($structure, 0777, true))   {
      die('Failed to create folders...');  
  }
if(move_uploaded_file($_FILES['userfile']['tmp_name'], $structure.basename( $_FILES['userfile']['name'])))
  {      echo "<center>The file ". basename( $_FILES['userfile']['name']). " has been uploaded.</center>";    }
   else
       {       echo "No File was uploaded";   }
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.