Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

php file upload  overwrite

Posted on 2009-06-27
8
Medium Priority
?
515 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

721 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