Jump to content

[SOLVED] Upload - If same filename exists


sotusotusotu

Recommended Posts

Hey guys,

 

I am trying to upload files.  The uploading is no problem, but if a file with the same name is uploaded it obviously overwrites the existing one.  Is there a way of checking and if the is a file already in the dir, add a letter to the end of it?

 

<?
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;

	if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) {
		echo "The file ". basename( $_FILES['uploaded']['name']). " has been uploaded<br />";
	} 
	else {
		echo "Sorry, there was a problem uploading your file.";
	}
?>

 

Thanks

 

 

Link to comment
https://forums.phpfreaks.com/topic/74220-solved-upload-if-same-filename-exists/
Share on other sites

I put on an md5'd string of the date when i upload files:

 

<?php

$date = md5(date("YmdHis"));

if(file_exists($date .$HTTP_POST_FILES['userfile']['name']))
{ 
echo "error.. Please refresh the page..";

}else{				

if(@copy($HTTP_POST_FILES['userfile']['tmp_name'], $date .$HTTP_POST_FILES['userfile']['name']))
{
	echo "successfully..";

}else{

	echo "error..";

}
}
?>
<form enctype="multipart/form-data" action="?view=Upload" method="post" name="upload">
<input type="file" name="userfile" />

Just give the files your own name based on a primary key or something.  PEAR also has a class where you can give every file a unique name if you do not have a database for this.  It is a security risk to leave the files with the same name the user gave them.

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.