Page 1 of 1

File overwrite?

Posted: Sat Aug 27, 2011 1:19 pm
by angelicodin
Morning fellow geeks of phpdn.

Quick question. I'm working on a file upload system and I've ran into the problem if a files exists on the server. I'm doing a check to see if it exists and if it dose then stops the upload (well move uploaded file in this case).

Code: Select all

if ((($_FILES["file1"]["type"] == "image/gif") || ($_FILES["file1"]["type"] == "image/jpeg") ||($_FILES["file1"]["type"] == "image/pjpeg")) && ($_FILES["file1"]["size"] < 100000)){ //check to see if the file uploaded is allowed
	if ($_FILES["file1"]["error"] > 0){
		echo "<br />There was an error with the file upload: <br />";
		echo "Return Code: " . $_FILES["file1"]["error"] . "<br />"; //print error
	}else{
		if(file_exists("upload/" . $_FILES["file1"]["name"])){ //check to see if the file name already exists in upload folder
			echo $_FILES["file1"]["name"] . " already exists. ";
		}else{
			move_uploaded_file($_FILES["file1"]["tmp_name"],
			"upload/" . $_FILES["file1"]["name"]);
			echo "Stored in: " . "upload/" . $_FILES["file1"]["name"]; //move file to upload dir
		}
	}
What I'm trying to figure out is there a way to over write it? I was curious if just doing something like moving it anyway after a prompt will overwrite it using "move_uploaded_file()"? Or was there something special I should do?

On a side note if anyone knows off hand, how long will a server by default keep the temp uploaded file before removing it if it was not renamed on the move?

Re: File overwrite?

Posted: Sat Aug 27, 2011 3:06 pm
by social_experiment
No idea on overwriting it but you could unlink() the existing file before moving the new one

Re: File overwrite?

Posted: Sat Aug 27, 2011 4:23 pm
by angelicodin
This is an idea, and I suppose I could that in, but I'm sure there is an easier way, maybe? ~shrugs~

Re: File overwrite?

Posted: Sun Aug 28, 2011 12:20 pm
by social_experiment
angelicodin wrote:I'm sure there is an easier way
It depends on the existing code you have, an better way would be to upload the file and change the name so no duplicate files could exists.

Re: File overwrite?

Posted: Sun Aug 28, 2011 1:05 pm
by phphelpme
Yeah, I agree with Social there and change the name as it uploads. But the unlink() is good idea for a backup plan too. Might be worth coding something in anyway... lol

Best wishes

Re: File overwrite?

Posted: Thu Sep 01, 2011 1:54 pm
by angelicodin
Well I'm trying out a few things, I'm going to include an unlink() to 'overwrite' the image in this case but I'm trying to make something that will give the user a way to check if they want to over write it. I'm doing a simple form with a radio button YES/NO, and I'm trying to pull a preview for the uploaded file, but it's not working because I don't know where the file is. Is there a way to pull the temp file as an image with out doing move_uploaded_file() then unlink() it with some extra variables?

Re: File overwrite?

Posted: Thu Sep 01, 2011 4:04 pm
by angelicodin
So after some minor hair pulling, I figures out something. This little bit of code will check to see if the file is uploaded, if an file already exists, then it will ask if you want to overwrite it with a preview, then if it dose not already exist then it just moves it form the server tmp file.

Code: Select all

<?php
if($_POST['overwrite_check'] === 'yes'){
	//process overwrite yes or no
	if($_POST['overwrite'] === 'yes'){
		unlink('upload/'.$_POST['overwrite_file_name']);
		rename('upload/tmp/'.$_POST['overwrite_file_name'],'upload/'.$_POST['overwrite_file_name']);
		echo "<br>The file was overwritten<br>";
	}else{
		unlink('upload/tmp/'.$_POST['overwrite_file_name']);
		echo "<br>the file was not overwritten<br>";
	}
}else{
	//check for file exisit
	if(file_exists("upload/".$_FILES['file1']['name'])){
		move_uploaded_file($_FILES['file1']['tmp_name'], "upload/tmp/".$_FILES['file1']['name']);
?>
		<form action="upload_ac.php" method="post">
		<table width="200" border="1">
		  <tr align="center">
		    <td>Uploaded Image</td>
		    <td>&nbsp;</td>
		    <td>Exsisting Image</td>
		  </tr>
		  <tr>
		    <td><img src="upload/tmp/<?php echo $_FILES['file1']['name']; ?>" width="200"  /></td>
		    <td>&nbsp;</td>
		    <td><img src="upload/<?php echo $_FILES['file1']['name']; ?>" width="200" /></td>
		  </tr>
		  <tr>
		    <td>&nbsp;</td>
		    <td><p>Overwrite File?</p>
		    <p><input type="radio" value="yes" name="overwrite" />Yes<input type="radio" value="no" name="overwrite" checked="checked" />No</p></td>
		    <td>&nbsp;</td>
		  </tr>
		  <tr>
		    <td><input type="hidden" name="overwrite_check" value="yes" /><input type="hidden" name="overwrite_file_name" value="<?php echo $_FILES['file1']['name']; ?>"</td>
		    <td><input type="submit"></td>
		    <td>&nbsp;</td>
		  </tr>
		</table>
		</form>
<?php
	}else{
		if ((($_FILES["file1"]["type"] == "image/gif") || ($_FILES["file1"]["type"] == "image/jpeg") ||($_FILES["file1"]["type"] == "image/pjpeg")) && ($_FILES["file1"]["size"] < 200000000)){
			move_uploaded_file($_FILES["file1"]["tmp_name"], "upload/" . $_FILES["file1"]["name"]);
			echo "<br>File \"".$_FILES["file1"]["name"]."\" uploaded to server.<br>";
		}
	}
}
?>