Jump to content

Recommended Posts

I have a form that uploads images and renames them to an auto increasing ID. So it starts at 1.jpg, 2.jpg etc...

 

If I wanted to edit the images using the form, I guess I would have to simply add the image again with the ID but how do I delete the image off the server before uploading the new one in it's place?

Link to comment
https://forums.phpfreaks.com/topic/44690-updating-an-uploaded-image/
Share on other sites

$uploadid = $id;
$uploaddir = 'images/products/';
$uploadfile = $uploaddir . $uploadid.".jpg";

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
   echo "File is valid, and was successfully uploaded.\n";
} else {
   echo "Possible file upload attack!\n";
}
echo $id;

echo 'Here is some more debugging info:';
print_r($_FILES);

print "</pre>";

'

	<!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
    <!-- Name of input element determines name in $_FILES array -->
    <input name="userfile" type="file" />	</td>

That's what I use but I get my "Possible upload attack" message, I guess because the file exists?

I want to over write the original image because when I load the page that is 'ID 7' for example, it will look on the server for '7.jpg' and I need to overwrite the file to that or it won't find it.

 

An alternative would be to delete the current image THEN upload the new one but I don't know how to do that.

I tried:

$uploadid = $id;
$uploaddir = 'images/products/';
$uploadfile = $uploaddir . $uploadid.".jpg";

$previous_img = "images/products/$id.jpg";
unlink($previous_img);

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
   echo "File is valid, and was successfully uploaded.\n";
} else {
   echo "Possible file upload attack!\n";
}
echo $id;

echo 'Here is some more debugging info:';
print_r($_FILES);

print "</pre>";

 

But no luck? It deletes the image but doesn't upload a new one.

I run a script on a website that allows admins to upload files. In order to avoid one person uploading a file with the same name as someone elses file i have to rename each file on upload.

 

Here is the code i used, maybe it will help.

$idnum = $cookie[0];
$target_path = "modules/$leaguedir/adminfiles/";
$thefile = $target_path . basename($_FILES['matchzip']['name']);
$thefilename = basename($_FILES['matchzip']['name']);
$thefiletype = substr("$thefile", -4);
$thefilesize = filesize($thefile);
$newname = gmdate("m-d-y-Hi");
$thenewfile = "modules/$leaguedir/adminfiles/$idnum-$newname-$thefilename";
$target_path = $target_path . basename($_FILES['matchzip']['name']);
$_FILES['matchzip']['tmp_name'];
if(move_uploaded_file($_FILES['matchzip']['tmp_name'], $target_path)){
$fileuploadok = "<FONT COLOR=green><B>Yes</B></FONT>";
//----------------------------------Heres where the file is renamed.
rename("$thefile", "$thenewfile");}
//----------------------------------Heres where the file is renamed.
else{$fileuploadok = "<FONT COLOR=red><B>No</B></FONT>";}

try

<?php
   preg_match('{(.+?)(\.[\w\d]+$)}', $filename, $match); //get name and type of file
   $name = $match[1];
   $type = $match[2];
   $i = 0;
   while (file_exists($filename)) { //add a number to name
      $i++;
      $filename = $name.'_'.$i.$type;
   }
   //result: 1_1.jpg (for example)
   move_uploaded_file(...);
?>

$filename is the name of $_FILE[uploaded file input]['file']

after that code use move_uploaded_file

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.