Jump to content

Add a username to this image upload script?


roldahayes

Recommended Posts

Hi there, I am using this very simple image upload script where a user can upload images to a floder on my server.

 

Would it be possible to have a form on the page where a user enters their username and then all the images are renamed accordingly? 

 

i.e username_image1.jpg, username_image2.jpg etc.

 

code is here

 

<?php

	# The script is designed to allow you to upload multiple files in one go, the script also presents you with the variable option
# of keeping a files name or randomly renaming it.
# Remember, there maybe a a upload limit set by the server of 2MB, you can change this by changing the php.ini if you have access
#**************************************************************************************************


###/ VARIABLES - CHANGE ACCORDINGLY
define("VAR_BASE_DIRECTORY",	"/var/www/vhosts/*******/subdomains/test/httpdocs/"); 				#/ Your webhosting directory
define("VAR_UPLOAD_FOLDER",		"uploads/"); 				#/ Chmod directory 777 for successful upload
define("VAR_UPLOAD_DIRECTORY",	VAR_BASE_DIRECTORY.VAR_UPLOAD_FOLDER); 		#/ DO NOT EDIT
define("VAR_UPLOAD_FIELDS",		4); 										#/ Set number of upload fields
define("VAR_FILENAME_KEEP",		1);											#/ If set to 0 (Zero) the filename will generate randomly, if 1 (One) it will maintain filename


##/ Function that displays forms and is called by default
function defaultForm()
{

	echo "<form method=\"post\" enctype=\"multipart/form-data\">\n";

		for($i=0; $i < VAR_UPLOAD_FIELDS; $i++)
		{        
			  echo "Upload field ".$i."  <input name=\"file[]\" type=\"file\" id=\"file[]\" /><br />\n";
		}

	echo "<input name=\"Submit\" type=\"submit\" value=\"Submit\">\n";
	echo "<input name=\"filter\" type=\"hidden\" value=\"processForm\">\n";		##/ hidden value points the switch to processing
	echo "</form>\n";

	return;

}
#/ End of defaultForm

##/ Function that displays forms and is called by default
function processForm()
{

	for($i=0; $i < VAR_UPLOAD_FIELDS; $i++)
	{
		echo "Upload field $i ";

		if(!empty($_FILES[file][size][$i])) 
		{ 
			if(VAR_FILENAME_KEEP==1)
			{
				##/ File maintaining upload name
				$fileName	 = $_FILES[file][name][$i];
			}
			else
			{
				##/ Filename randomized
				$fileName	 = rand(1,4000).rand(1,4000).rand(1,4000).rand(1,4000).rand(1,4000).'.' . substr($_FILES[file][name][$i], -3);
			}

			##/ Creating reference address 
			$newLocation	 = VAR_UPLOAD_DIRECTORY.$fileName;

			if(!copy($_FILES[file][tmp_name][$i],$newLocation)) 
			{
				echo "<b>Failed - ".$_FILES[file][name][$i]." would not copy to ".$newLocation."</b> (Check your upload directory and permissions)";
			}
			else
			{
				###/ SUCCESS /###

				#/ Stripping of VAR_BASE_DIRECTORY for better viewing and linking
				$urlShow = str_replace(VAR_BASE_DIRECTORY,'',$newLocation); 

				echo "<b>Uploaded successfully - <a href=\"$urlShow\" target=\"_blank\">$urlShow</a></b>";
			}
		} 
		else
		{
			echo "<b>No file uploaded</b>";
		}
		echo "<br />";
	}
	return;
}
#/ End of processForm


    ##/ This object handles which function the application should call
switch($_POST[filter]) {
	case "processForm":
		processForm();
	break;
	default:
		defaultForm();
	break;
}
#/ End of Handling

?>

Link to comment
Share on other sites

Do you want to base this on a login scenario for the username, or just have a field that someone can type a name and then place that in front of the file name?

 

In either case, you'll change this line:

              $fileName    = rand(1,4000).rand(1,4000).rand(1,4000).rand(1,4000).rand(1,4000).'.' . substr($_FILES[file][name][$i], -3);

 

to

              $fileName    = $username."_".rand(1,4000).rand(1,4000).rand(1,4000).rand(1,4000).rand(1,4000).'.' . substr($_FILES[file][name][$i], -3);

 

Let me know what you're thinking and we can work through how you get to that username

Link to comment
Share on other sites

Changed my mind again!

 

I want to have the users login name appear before the image title.

 

can i use something like $_SESSION['LOGIN_NAME'];  ???

 

in the line;

 

##/ Filename randomized
				$fileName	 = rand(1,4000).rand(1,4000).rand(1,4000).rand(1,4000).rand(1,4000).'.' . substr($_FILES[file][name][$i], -3);
			}

 

Instead of it being a random name?

Link to comment
Share on other sites

Absolutely. Whatever session is storing their username, just prepend it to the filename string like this:

 

##/ Filename randomized

              $fileName    = $_SESSION['LOGIN_NAME'].rand(1,4000).rand(1,4000).rand(1,4000).rand(1,4000).rand(1,4000).'.' . substr($_FILES[file][name][$i], -3);

            }

Link to comment
Share on other sites

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.