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

?>

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

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?

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);

            }

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.