ArshSingh Posted February 21, 2014 Share Posted February 21, 2014 i want to retrieve username from database table and display it where i put the code $username , i'm using the following code to make it work but its giving me an error : <?php require_once("models/config.php"); if (!securePage($_SERVER['PHP_SELF'])){die();} require("models/db-settings.php"); $mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name); $result = $mysqli->query("SELECT user_name FROM upl_users"); // This will move the internal pointer and skip the first row, we don't want that. //$row = mysql_fetch_assoc($result); //echo $row['user_name']; while ( $row = $result->fetch_assoc() ) { $username = $row['user_name'];} $dir = 'uploads/$username/'; if (file_exists($UploadedDirectory)) { mkdir('uploads/$username/', 0777, true); } if(isset($_FILES['FileInput']) && $_FILES['FileInput']['error']== UPLOAD_ERR_OK) { ############ Edit settings ############## $UploadDirectory = 'uploads/$username/'; //specify upload directory ends with / (slash) ########################################## /* Note : You will run into errors or blank page if "memory_limit" or "upload_max_filesize" is set to low in "php.ini". Open "php.ini" file, and search for "memory_limit" or "upload_max_filesize" limit and set them adequately, also check "post_max_size". */ //check if this is an ajax request if (!isset($_SERVER['HTTP_X_REQUESTED_WITH'])){ die(); } //Is file size is less than allowed size. if ($_FILES["FileInput"]["size"] > 5242880) { die("File size is too big!"); } //allowed file type Server side check switch(strtolower($_FILES['FileInput']['type'])) { //allowed file types case 'image/png': case 'image/gif': case 'image/jpeg': case 'image/pjpeg': case 'text/plain': case 'text/html': //html file case 'application/x-zip-compressed': case 'application/pdf': case 'application/msword': case 'application/vnd.ms-excel': case 'video/mp4': case 'audio/mp3'; break; default: die('Unsupported File!'); //output error } $File_Name = strtolower($_FILES['FileInput']['name']); $File_Ext = substr($File_Name, strrpos($File_Name, '.')); //get file extention $Random_Number = uniqid(); //Random number to be added to name. $NewFileName = $Random_Number.$File_Ext; //new file name if(move_uploaded_file($_FILES['FileInput']['tmp_name'], $UploadDirectory.$NewFileName )) { die(' Success! File Uploaded.'); }else{ die('error uploading File!'); } } else { die('Something wrong with upload! Is "upload_max_filesize" set correctly?'); } ?> error: it creates the folder named : $username and not retirieves it from database , if i'm logged in and i upload a file then script need to create a folder with my name : admin ; uploads/admin/file.jpg but it makes ; uploads/$username/file.jpg any help thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/286381-cant-get-username-from-database-and-display-it/ Share on other sites More sharing options...
cyberRobot Posted February 21, 2014 Share Posted February 21, 2014 When including variables within a string, you'll need to use double quotes. For example, this $dir = 'uploads/$username/'; Should be changed to this. $dir = "uploads/$username/"; Quote Link to comment https://forums.phpfreaks.com/topic/286381-cant-get-username-from-database-and-display-it/#findComment-1469870 Share on other sites More sharing options...
ArshSingh Posted February 21, 2014 Author Share Posted February 21, 2014 (edited) When including variables within a string, you'll need to use double quotes. For example, this $dir = 'uploads/$username/'; Should be changed to this. $dir = "uploads/$username/"; thanks it works i got the username but got this error as well : Warning: move_uploaded_file(uploads/singharsh74/53075ae05d887.jpg): failed to open stream: No such file or directory in /home/u381071273/public_html/upload/upload.php on line 72 Warning: move_uploaded_file(): Unable to move '/tmp/php7cgNVw' to 'uploads/singharsh74/53075ae05d887.jpg' in /home/u381071273/public_html/upload/upload.php on line 72 error uploading File! Edited February 21, 2014 by ArshSingh Quote Link to comment https://forums.phpfreaks.com/topic/286381-cant-get-username-from-database-and-display-it/#findComment-1469871 Share on other sites More sharing options...
mac_gyver Posted February 21, 2014 Share Posted February 21, 2014 you have to test for the existence of the folder and create it if it doesn't exist before you can move a file into it. Quote Link to comment https://forums.phpfreaks.com/topic/286381-cant-get-username-from-database-and-display-it/#findComment-1469918 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.