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
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";
   }
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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";   }
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.