Solved

php file upload  overwrite

Posted on 2009-06-27
8
502 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

708 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

16 Experts available now in Live!

Get 1:1 Help Now