zipp Posted July 28, 2007 Share Posted July 28, 2007 I am trying to have a file uploaded into a directory, based on user input. If the folder they are uploading to doesn't exist, it is created. The only thing I can think of, is that this method of file uploading will only work to 1 folder deep. That is only a guess though. Also the "destination" will only be 1 word long. <?php //find out if the user specified a target location, other then default. if (!$_REQUEST['destination']){ $destination = ""; } else { $destination = $_REQUEST['destination'] . "/"; } //where the file is getting uploaded $target = "upload/" . $destination; $target = $target . basename( $_FILES['uploaded']['name']) ; //check to see if file exists if (!file_exists($target)) { mkdir($target, 0777); } //upload file if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { echo '<html><head><meta http-equiv="refresh" content="5;url=index.php"></head><body>'; echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded<br />"; echo "You will be taken back to the admin page in 5 seconds."; }else{ echo "Sorry, there was a problem uploading your file.<br />"; //echo $target; } ?> Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted July 28, 2007 Share Posted July 28, 2007 This function will allow you to create directories infinitely deep: <?php function mkdir_recursive($folder) { $folders = explode('/',str_replace('\\','/',$folder)); $temp_folder = null; for($i=0; $i<count($folders); $i++) { $temp_folder .= $folders[$i].'/'; if(!is_dir($temp_folder) && !file_exists($temp_folder)) { mkdir($temp_folder); } } } ?> Quote Link to comment Share on other sites More sharing options...
zipp Posted July 28, 2007 Author Share Posted July 28, 2007 The folders seem to be made correctly already. I'm sorry I should have specified. The issue occurs when trying to upload the file. Quote Link to comment Share on other sites More sharing options...
zipp Posted July 29, 2007 Author Share Posted July 29, 2007 I have been stuck on this for a while now, its really starting to wear me down. Here is the newest code that I am working with. index.html <form enctype='multipart/form-data' action='upload.php' method='POST' id='form'> Please choose a file: <input name='up' type='file' /><br /> User to upload file to: <select name='destination'> <option value="jackie">jackie</option> <option value="test">test</option> <option value="tom">tom</option> </select><input type='submit' value='Upload' /> </form> upload.php <?php //check destination to see if its different then default if (!$_REQUEST['destination']){ $destination = ""; } else { $destination = $_REQUEST['destination'] . "/"; } //check upload directory chmod("upload", 0777); //where the file is getting uploaded $target = "upload/" . $destination; $target = $target . basename( $_FILES['up']['name']) ; //make sure target tirectory exists if (!file_exists($target)) { mkdir($target, 0777); } if(move_uploaded_file($_FILES['up']['tmp_name'], $target)) { //worked echo '<html><head><meta http-equiv="refresh" content="5;url=index.php"></head><body>'; echo "The file ". basename( $_FILES['uploadfile']['name']). " has been uploaded<br />"; echo "You will be taken back to the admin page in 5 seconds."; }else{ //error echo "Sorry, there was a problem uploading your file.<br />"; echo $_REQUEST['up']; echo $target; print_r($_FILES); } ?> this will print out: Sorry, there was a problem uploading your file. upload/test/Array ( ) 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.