Jump to content

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.

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.