Jump to content

Recommended Posts

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

 

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 by ArshSingh
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.