daverobo01 Posted April 9, 2008 Share Posted April 9, 2008 hi ive been stuck with this a while so i thought id get some help. I am trying to have a upload file website so when a user logs in they can upload to there folder ive got the upload working fine and when they register it creates a folder and adds it into mysql but the file thats been uploaded isnt going in to there folder its going somewhere else. so i think i need to make the upload retrieve the folder name from mysql ill post some scripts below so you can see whats happening. This is my upload.php <?php $base_directory = "uploaded-files/"; $directory = $_REQUEST['directory']; $upload_dir = ""; $file = ""; $dir = ""; $POST_MAX_SIZE = ini_get('post_max_size'); $mul = substr($POST_MAX_SIZE, -1); $mul = ($mul == 'M' ? 1048576 : ($mul == 'K' ? 1024 : ($mul == 'G' ? 1073741824 : 1))); // check if the size is not larger than the size of the php.ini file if ($_SERVER['CONTENT_LENGTH'] > $mul*(int)$POST_MAX_SIZE && $POST_MAX_SIZE) { error_log("Total upload size is larger than post_max_size directive in the " . "php.ini file ($POST_MAX_SIZE)"); error_log("The php.ini file is the configuration file of PHP"); error_log("Please check and change the post_max_size, upload_max_filesize " . "and memory_limit directives"); error_log("The maximum value of these directives is 1990M"); error_log("Increase also the max_execution_time and the max_input_time directives"); header("HTTP/1.0 500 Internal Server Error"); exit; } // check if $base_directory exist if (!is_dir($base_directory)) { error_log('The $base_directory \'' . $base_directory . "' does not exist"); error_log('Please check the $base_directory in the upload.php script'); header("HTTP/1.0 500 Internal Server Error"); exit; } // check if the $base_directory is writeable if (!is_writeable($base_directory)) { error_log("The base_directory '" . $base_directory . "' has no write permissions."); header("HTTP/1.0 500 Internal Server Error"); exit; } determineUploadDirectory(); foreach ($_FILES["userfile"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["userfile"]["tmp_name"][$key]; $name = $_FILES["userfile"]["name"][$key]; $decoded_name = urldecode($name); $pos = strrpos($decoded_name, "/"); if ($pos === false) { $file = $decoded_name; $path = $upload_dir; } else { $file = substr($decoded_name, $pos + 1); $dir = substr($decoded_name, 0, $pos); $path = $upload_dir . "/" . $dir; } mkdir_recursive($path); if(!move_uploaded_file($tmp_name, $path . "/" . $file)) { if (!is_dir($path)) { error_log("File: '" . $file . "' cannot be saved because the directory '" . $path . "' does not exist."); } else if (!is_writeable($path)) { error_log("File: '" . $file . "' cannot be saved because the directory '" . $path . "' has no write permissions."); } } } else { switch ($error) { case UPLOAD_ERR_INI_SIZE: error_log("The uploaded file exceeds the upload_max_filesize directive (" . ini_get("upload_max_filesize") . ") in php.ini."); break; case UPLOAD_ERR_FORM_SIZE: error_log("The uploaded file exceeds the MAX_FILE_SIZE directive that" . " was specified in the HTML form."); break; case UPLOAD_ERR_PARTIAL: error_log("The uploaded file was only partially uploaded."); break; case UPLOAD_ERR_NO_FILE: error_log("No file was uploaded."); break; case UPLOAD_ERR_NO_TMP_DIR: error_log("Missing a temporary folder."); break; case UPLOAD_ERR_CANT_WRITE: error_log("Failed to write file to disk"); break; } header("HTTP/1.0 500 Internal Server Error"); exit; } } //----------------------------------------------------------------------------- // FUNCTIONS //----------------------------------------------------------------------------- // Creates the uploaddirectory. The uploaddirectory is based on the // base_directory and the directory. The directory is the unique // directory for every user. You can specify this directory in the // param tag of the applet tag. See http://www.javaatwork.com/parameters.html // The base_directory is specified in this php script. // This method ensures that there's only one slash between the directories. // e.g. c:/temp/files instead of c:/temp//files function determineUploadDirectory() { global $base_directory, $directory, $upload_dir; //remove the slash of base_directory $len = strlen ($base_directory); $charAt = $base_directory{$len -1}; if ($charAt == '/') { $base_directory = substr ($base_directory, 0, $len -1); } // remove the slashes from $directory $charAt = $directory{0}; if ($charAt == '/') { $directory = substr($directory, 1); } $len = strlen ($directory); if ($len != 0) { $charAt = $directory{$len - 1}; if ($charAt == '/') { $directory = substr($directory, 0, $len -1); } $len = strlen ($directory ); $upload_dir = $base_directory . "/" . $directory; } else { $upload_dir = $base_directory; } } // Creates the directories if needed. function mkdir_recursive($dir){ do { $tempdir = $dir; while (!is_dir($tempdir)) { $basedir = dirname($tempdir); if ($basedir == '/' || is_dir($basedir)) { mkdir($tempdir,0757); // sometimes the chmod in the mkdir method doesn't work chmod($tempdir, 0757); } else { $tempdir=$basedir; } } } while ($tempdir != $dir); } ?> And This is Authenticate.php which is the registering basically. <?php // Include the database connection file. include("connection.php"); // Check if a person has clicked on submit. if(isset($_POST['submit'])) { // Check if a person has filled every form. if(empty($_POST['username']) || empty($_POST['password']) || empty($_POST['password2']) || empty($_POST['email'])|| empty($_POST['folder'])) { echo "You have to fill in everything in the form."; // Display the error message. header("Location: register.php"); // Redirect to the form. exit; // Stop the code to prevent the code running after redirecting. } // Create variables from each $_POST. $username = $_POST['username']; $password = $_POST['password']; $password2 = $_POST['password2']; $email = $_POST['email']; $folder = $_POST['folder']; // Now, compare passwords and check if they're the same. if($password != $password2) { // If the passwords are NOT the same. Again display an error message and redirect. echo "Sorry, wrong password."; header("Location: register.php"); exit; } // Secure the password using an md5 hash. $password = md5($password); // Create a variable containing the SQL query. $query = "INSERT INTO `users` (username, password, email, folder) VALUES ('$username', '$password', '$email', '$folder')"; // Perform the SQL query on the database. $result = mysql_query($query); $mypath="uploaded-files/$folder"; mkdir($mypath,0777,TRUE); // If the query failed, display an error. if(!$result) { echo "Your query failed. " . mysql_error(); // The dot seperates PHP code and plain text. } else { // Display a success message! echo "Welcome " . $username . " You are now registered"; } } ?> <?php Any Help would be appreciated add me to msn if you can help [email protected] Link to comment https://forums.phpfreaks.com/topic/100320-upload-file-to-user-folder/ Share on other sites More sharing options...
craygo Posted April 9, 2008 Share Posted April 9, 2008 your upload directory should be an absolute path. $path = "/home/johnny/public_html/users/"; then grab the username from the database and add it to the absolute path $path .= $username; now you can add it to the move_uploaded_file if(!move_uploaded_file($tmp_name, $path . "/" . $file)) { Ray Link to comment https://forums.phpfreaks.com/topic/100320-upload-file-to-user-folder/#findComment-512983 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.