Jump to content

File uploading - random file name


peter_anderson

Recommended Posts

Right, I need to give the filenames a random name, just incase it is already existing in my directory.

 

Here is the file upload code

 

<?php
//snipped
case "uploadProcess":
	//if the user no logged in, show error
	if (!isset($_SESSION['active'])) { 
		$content = '<h2>For security reasons, you must be logged in to upload a file.<br />
			<br />Go back and login before attempting to upload.</h2>';
		$content .= '<p style="text-align: center; ">Powered by <a href="http://www.scriptboxer.com/download-site-script.php">DownloadBoxer v1</a>.<br />
		© <a href="http://www.scriptboxer.com">ScriptBoxer</a> - <a href="http://www.scriptboxer.com">PHP Scripts</a></p>';
		$html = str_replace('{content}', $content, $html);
		$html = str_replace('{title}', 'Upload File :: Upload FAILED', $html);
		echo $html;
		exit();
	}
	//else, let's up-fucking-load baby!
	$target_path = $config['uploaddir'];
	$target_path = $target_path . basename( $_FILES['uploadfile']['name']); 
	$filelocation = $_FILES['uploadfile']['tmp_name'];
	$ipadd = $_POST['ipadd'];
	if(move_uploaded_file($_FILES['uploadfile']['tmp_name'], $target_path)) {
		$fid = rand(1690, 16901690);
		$username = $_SESSION['username'];
		$filel = ''.$config['uploaddir'].''.basename( $_FILES['uploadfile']['name']).'';
		$query = "INSERT INTO files(owner, filelocation, ipadd, fid)
		 VALUES('$username', '$filel', '$ipadd', '$fid')";
		$result = $sql->query($query);
		$content = '<h1>The file has been uploaded!</h1>
			<h2>You may download it <a href="?action=downloadFile&file='.$fid.'" target="_blank">here.</a></h2>
			<p>Thank you for uploading the file to our file sharing service.</p>
			<p>It has now been uploaded and is ready for downloads.</p>
			<p>Copy and paste this link for distributing the file.</p>
			<p>http://www.'.$_SERVER['SERVER_NAME'].'/?action=downloadFile&file='.$fid.'</p>';
		//echo "The file ".  basename( $_FILES['uploadfile']['name'])." has been uploaded";
	} else{
		$content = '<h1>File Upload Error</h1>
			<h2>Oops, the file couldn&#39;t be uploaded!</h2>
			<p>The file you tried to upload has NOT been uploaded.</p>
			<p>This could be because:<br />
			- the file is already on the server<br />
			- the server is full<br />
			- the file was named the same as an already existing file<br />
			- the server is not accepting new uploads.</p>
			<p>Please contact our support team or the webmaster.</p>';
	}
	$content .= '<p style="text-align: center; ">Powered by <a href="http://www.scriptboxer.com/download-site-script.php">DownloadBoxer v1</a>.<br />
	© <a href="http://www.scriptboxer.com">ScriptBoxer</a> - <a href="http://www.scriptboxer.com">PHP Scripts</a></p>';
	$html = str_replace('{content}', $content, $html);
	$html = str_replace('{title}', 'Upload File :: Upload Results', $html);
	echo $html;
break;
default:
	$content = '<h1>Welcome!</h1>
		'.$config['welcomemsg'].'
		<h2>Upload your file</h2>';
	if (isset($_SESSION['active'])) { 
		$content .= '<form action="?action=uploadProcess" enctype="multipart/form-data" method="post" name="uploadForm">
			<p><input id="upload" style="width:80%;" name="uploadfile" type="file" />
			<input type="hidden" name="username" value="'.$_SESSION['username'].'" />
			<input type="hidden" name="ipadd" value="'.$_SERVER["REMOTE_ADDR"].'" />
			<input id="upload" name="upload" type="submit" value="Upload Now!" /></p>
			</form>';
	}else{
		$content .= '<h2>You must be logged in to upload!<br />
			<a href="?action=Members">Click here to login</a>.</h2>';
	}
	$content .= '<p style="text-align: center; ">Powered by <a href="http://www.scriptboxer.com/download-site-script.php">DownloadBoxer v1</a>.<br />
	© <a href="http://www.scriptboxer.com">ScriptBoxer</a> - <a href="http://www.scriptboxer.com">PHP Scripts</a></p>';
	$html = str_replace('{content}', $content, $html);
	$html = str_replace('{title}', 'Home :: Upload Now!', $html);
	echo $html;
break;
?>

 

For the filename, I could use the rand() function I use to generate an ID for the script?

 

And to get the file extension (so I keep it) I should use

$extension = end(explode('.', $filename));

.

 

But how do I put this altogether into my script?

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/177331-file-uploading-random-file-name/
Share on other sites

you specify the path to the uploaded file via the move_uploaded_file() function. If you supply a different path, it will have a different name. IE instead of the path being path/to/myfile.txt, you can change the path to be path/to/otherfile.txt

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.