Jump to content

Best way to replace a file?


suttercain

Recommended Posts

Hi guys,

 

I have a file upload form and the file is moved and renamed based on the users id number. Example userId #150 = 150.jpg.

 

How can I allow the user to upload, using the same form, but replace the existing file so it will still read 150.jpg but with a new image (replacing the old one).

 

FORM

<form enctype="multipart/form-data" action="uploadedProfilePic" method="post">
<input name="logoImage" type="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

 

PHP

<?php session_start(); //DO NOT REMOVE!!!! ?>
<?php if ($_SESSION['merchant'] == 1) { ?>
<?php
require('../database.php');
if ($_FILES['logoImage']['size'] < 250000) {
	if ($_FILES['logoImage']['type'] === "image/gif" || $_FILES['logoImage']['type'] === "image/jpg" || $_FILES['logoImage']['type'] === "image/jpeg") {
	$sqlImage = mysql_query("SELECT merchantId FROM merchants WHERE username = '".$_SESSION['user']."'") or die(mysql_error($connect));
	$image = mysql_fetch_row($sqlImage);
	$imageName = $image['0'];

	// PROCESS IMAGE
	$target_path = "../profileimages/";
	$target_path = $target_path . basename( $_FILES['logoImage']['tmp_name']); 
	$_FILES['logoImage']['tmp_name'];

	//MOVE THE IMAGE TO THUMBNAILS
	move_uploaded_file($_FILES['logoImage']['tmp_name'], $target_path);

	//EXPLODE TO RENAME IMAGE
	$fileName = $_FILES['logoImage']['name'];
	$broken = explode(".", $fileName);
	rename("../profileimages/".basename($_FILES['logoImage']['tmp_name'])."", "../profileimages/".$imageName."." .$broken[1]."");

	//Upload Image Name Into Table
	$update = mysql_query("UPDATE merchants SET profilePic='" .$imageName."." .$broken[1]. "' WHERE merchantId='".$imageName."'"		) or die(mysql_error());
	$_SESSION['message'] = "Logo Uploaded!";
	echo "<script type=\"text/javascript\">document.location.href='logged.php'</script>";
	} else {
	echo "The image file must be in .gif, .jpg or .jpeg format.";
	$passed = FALSE;
	}
} else {
echo "The file was too large!"; 
}

} else {
echo "You are not authorized to view this page."; }
?>

 

Thanks

Link to comment
Share on other sites

I believe so. This is what the current code does.

 

User Uploads > File is Moved to Directory > File is renamed based on the userId and the extension type (exp. jpg, gif).

 

When a user "re" uploads a photo nothing happens. The form seems to process but no "new" file is placed in the directory. Instead the old file stays the same.

 

 

SC

Link to comment
Share on other sites

Do you think it would be "safe" to delete the existing file, with unlink, before uploading the new file. In essence replacing the old file?

 

//find the file based on userId

$myFile = $row['fileName'];
$fh = fopen($myFile, 'w') or die("can't open file");
fclose($fh);

//Uplaod file code here

Link to comment
Share on other sites

Thats what I do for one of my pages

 

<?php

$old_file = 'avatars/'.$page_id.'.gif';
if(file_exists($old_file))
{
   unlink($old_file);
}
copy ($_FILES['avatar']['tmp_name'], "avatars/".$page_id.".".$file_extension) or die("Your avatar could not be copied correctly!");

?>

 

~ Chocopi

Link to comment
Share on other sites

Before fixing the problem, it would be good to verify that it really is a problem :)  Check that rename() returns false (indicating failure to rename).  If that's the case, then chocopi's approach should work.  If you're ultra paranoid, you may want to make a backup of the file you're deleting, just in case.

Link to comment
Share on other sites

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.