Solved

php file upload  overwrite

Posted on 2009-06-27
8
504 Views
Last Modified: 2012-05-07
hello -

i'm using some commercial php upload script that i purchased years ago.

the problem is it won't allow me to overwrite my images which i have to do for this particular application - it just creates new ones.

don't know a lot about php upload so i tried throwing an $overwrite=true variable in the code for kicks and nothing changed. here's the way the code for the variables:

// *** BEGIN Simply Upload ***
require_once("FXInc/uploadAction.inc");
$errMsg = "";
$action = true;
$noPath = true;
$rename = false;
$delete = false;
$overwrite = true;
$FX_successRedirect = "";
$FX_DirPath = "../images/ba/".$thumbs_dir."/thumbs/";
$FX_typearray = array("application","audio","image");
$FX_extarray = array();
$FX_size = "";
$FX_fields = array();
if ((isset($HTTP_POST_VARS["FX_upload"])) && ($HTTP_POST_VARS["FX_upload"] == "form1")) {
  require_once("FXInc/upload.inc");
}
// *** END Simply Upload ***
0
Comment
Question by:phillystyle123
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 7

Expert Comment

by:szewkam
ID: 24728981
well it look like the code you have has few years ;). The $HTTP_POST_VARS variable is deprecated for long time ;). Maybe you should consider pay someone to wrote or modify your script :).
But back to the point. You have to show us a little bit more of your code, fragment you show us is too small. We can't predict problem just from definition of variables ;)
0
 

Author Comment

by:phillystyle123
ID: 24729226
thanks for taking a look at this szewkam:

this file looks relevant:

<?php

function renameIt($name,$path) {

  $re = "/\(\d+\)/";

  if (preg_match($re,$name,$matches)) {

    $num = substr($matches[0],1,strlen($matches[0])-2);

    $newName = preg_replace($re,"(".(++$num).")",$name);

  } else {

    $newName = str_replace(".","(1).",$name);

  }

  if (file_exists($path . $newName)) $newName = renameIt($newName,$path);

  return $newName;

}
 

$FX_uploadAction = $HTTP_SERVER_VARS['PHP_SELF'];

if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {

  $FX_uploadAction .= "?" . htmlentities($HTTP_SERVER_VARS['QUERY_STRING']);

}

?>

Open in new window

0
 
LVL 5

Accepted Solution

by:
solutionDriver earned 500 total points
ID: 24730486
Hi phillystyle123,

if you want to overwrite the file everytime, you can simply make a small change to the rename function,
like this:

function renameIt($name,$path) {

  return $name; // amendment: don't rename  -- phillystyle123, [date]

  $re = "/\(\d+\)/";

  [...]
}

Make sure that the file is writable. If there are problems with overwriting the file,
you can have the old one deleted beforehand:

function renameIt($name,$path) {

  unlink($path . $newName);
  return $name; // amendment: don't rename  -- phillystyle123, [date]

  $re = "/\(\d+\)/";

  [...]
}

Best,

  sd

0
 
LVL 7

Expert Comment

by:szewkam
ID: 24730882
you can also find a file where function renameIt is called, and just comment line with it or remove the call :)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:phillystyle123
ID: 24731931
thanks guys - solutionDriver - can you show me where your code would fit into my code?

this is what i'm guessing:
<?php

function renameIt($name,$path) {

unlink($path . $newName);

  return $name; // amendment: don't rename  -- phillystyle123, [date]
 

  $re = "/\(\d+\)/";

  $re = "/\(\d+\)/";

  if (preg_match($re,$name,$matches)) {

    $num = substr($matches[0],1,strlen($matches[0])-2);

    $newName = preg_replace($re,"(".(++$num).")",$name);

  } else {

    $newName = str_replace(".","(1).",$name);

  }

  if (file_exists($path . $newName)) $newName = renameIt($newName,$path);

 }

 

$FX_uploadAction = $HTTP_SERVER_VARS['PHP_SELF'];

if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {

  $FX_uploadAction .= "?" . htmlentities($HTTP_SERVER_VARS['QUERY_STRING']);

}

?>

Open in new window

0
 
LVL 14

Expert Comment

by:profya
ID: 24732006
You may need to change $delete = false; to $delete = true; and to make sure that there is enough permissions for deletion. Overwrite is simply delete then create new.
0
 
LVL 5

Expert Comment

by:solutionDriver
ID: 24732098
Hello phillystyle123.

that's exactly right, you got it.

Best,

  sd
0
 

Author Closing Comment

by:phillystyle123
ID: 31597590
thanks solutionDriver!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
FILTER_SANITIZE_NUMBER_INT takes out the 0 when staring a number with 0 19 45
.php tree directory? 5 55
only allow numbers with preg match 4 25
Session timeout 5 13
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now