sanchez77 Posted November 24, 2010 Share Posted November 24, 2010 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 Quote Link to comment Share on other sites More sharing options...
Unirawan Posted November 24, 2010 Share Posted November 24, 2010 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"; } ?> Quote Link to comment Share on other sites More sharing options...
sanchez77 Posted November 24, 2010 Author Share Posted November 24, 2010 thanks for your response, but it didn't work. Wouldnt even return an error message. Anyone know how to download a file from a MySQL db? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted November 24, 2010 Share Posted November 24, 2010 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 Quote Link to comment Share on other sites More sharing options...
sanchez77 Posted November 24, 2010 Author Share Posted November 24, 2010 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"; } ?> Quote Link to comment Share on other sites More sharing options...
sanchez77 Posted November 24, 2010 Author Share Posted November 24, 2010 Thank you everyone for your help. It does work, it was putting the file in another folder. Thanks again. Cheers Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.