File overwrite?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
angelicodin
Forum Commoner
Posts: 81
Joined: Fri Nov 13, 2009 3:17 am
Location: Oregon, USA

File overwrite?

Post 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?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: File overwrite?

Post by social_experiment »

No idea on overwriting it but you could unlink() the existing file before moving the new one
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
angelicodin
Forum Commoner
Posts: 81
Joined: Fri Nov 13, 2009 3:17 am
Location: Oregon, USA

Re: File overwrite?

Post by angelicodin »

This is an idea, and I suppose I could that in, but I'm sure there is an easier way, maybe? ~shrugs~
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: File overwrite?

Post 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.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: File overwrite?

Post 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
User avatar
angelicodin
Forum Commoner
Posts: 81
Joined: Fri Nov 13, 2009 3:17 am
Location: Oregon, USA

Re: File overwrite?

Post 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?
User avatar
angelicodin
Forum Commoner
Posts: 81
Joined: Fri Nov 13, 2009 3:17 am
Location: Oregon, USA

Re: File overwrite?

Post 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>";
		}
	}
}
?>
Post Reply