get_new_file dreamincode snippet doesn't work

can't get snippet to prevent file overwrite

Page 1 of 1

2 Replies - 1100 Views - Last Post: 06 November 2009 - 05:15 AM Rate Topic: -----

#1 fortesque   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 02-June 09

get_new_file dreamincode snippet doesn't work

Posted 05 November 2009 - 10:56 AM

Hi: I'm trying to use get_next_file dreamincode snippet to prevent an overwrite of an uploaded file.
I have a script "save_html_output_2_2.php5" which combines the code to prevent overwrite with code to upload file contents. Instructions for the snippet say that I only have to pass the directory and file name to the function.
Here is the code for "save_html_output_2_2.php5" script:
<?php

function get_next_filename($directory, $filename) {
  if(!file_exists($dir . $filename)) {
	return $filename;
  }

  $x = 0;
  $fex = explode(".", $filename);
  $newfile = $fex[0] . "({$x})" . "." . $fex[1];

  while(file_exists($dir . $newfile)) {
	$x++;
	$newfile = $fex[0] . "({$x})" . "." . $fex[1];
  }

  return $newfile;
} 

get_next_filename("http://www.headacheanalysis.com/practice", "saved_test.html");



ob_start();
	$content=file_get_contents('http://www.headacheanalysis.com/patient_query_lastinsert_KPC_1.php5');
	


// Save the test result to a file
$fn = "saved_test.html";
$fh = fopen($fn, 'w') or die("can't open file");

if(file_put_contents("saved_test.html", $content)) { 
  echo "Success"; 
} 
else { 
  echo "Failed to save file"; 
} 
echo $content;
fclose($fh);
 ob_end_clean();
?>




If the file to be uploaded, "saved_test.htm" does not exist on the server the above code successfully uploads it on the first call. However, if the uploaded file exists, the portion of dreamincode snippett does not rename the uploaded file with an extension, and the new version of the file is not uploaded.

I have tried running the dreamincode snippet file by itself after the uploaded file "save_test.html" has been uploaded and it fails to add an extension to the uploaded file.
Here is the snippet code modified to check for file "save_test.html" on server.
Need help in figuring out what I'm doing wrong.
Many thanks.

Here is the snippet get_next_file code.


<?php
//get net file snippett
function get_next_filename($directory, $filename) {
  if(!file_exists($dir . $filename)) {
	return $filename;
  }

  $x = 0;
  $fex = explode(".", $filename);
  $newfile = $fex[0] . "({$x})" . "." . $fex[1];

  while(file_exists($dir . $newfile)) {
	$x++;
	$newfile = $fex[0] . "({$x})" . "." . $fex[1];
  }

  return $newfile;
} 

get_next_filename("http://www.headacheanalysis.com/practice", "saved_test.html");

?>



Is This A Good Question/Topic? 0
  • +

Replies To: get_new_file dreamincode snippet doesn't work

#2 jwwicks   User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 162
  • Joined: 31-July 08

Re: get_new_file dreamincode snippet doesn't work

Posted 05 November 2009 - 07:20 PM

Hello,

View Postfortesque, on 5 Nov, 2009 - 09:56 AM, said:

Hi: I'm trying to use get_next_file dreamincode snippet to prevent an overwrite of an uploaded file.
I have a script "save_html_output_2_2.php5" which combines the code to prevent overwrite with code to upload file contents. Instructions for the snippet say that I only have to pass the directory and file name to the function.
Here is the code for "save_html_output_2_2.php5" script:
<?php

function get_next_filename($directory, $filename) {
  if(!file_exists($dir . $filename)) {
	return $filename;
  }

  $x = 0;
  $fex = explode(".", $filename);
  $newfile = $fex[0] . "({$x})" . "." . $fex[1];

  while(file_exists($dir . $newfile)) {
	$x++;
	$newfile = $fex[0] . "({$x})" . "." . $fex[1];
  }

  return $newfile;
} 

get_next_filename("http://www.headacheanalysis.com/practice", "saved_test.html");

?>



I'd venture a guess that the $dir in that function/snippet is supposed to be $directory or visa versa

Jw
Was This Post Helpful? 0
  • +
  • -

#3 fortesque   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 02-June 09

Re: get_new_file dreamincode snippet doesn't work

Posted 06 November 2009 - 05:15 AM

Jw:
I found the inconsistency between $dir and $directory. Thanks. However, when that inconsistency in varible naming is corrected, regardless of whether I use $dir or $directory throughout the code, still the snippet doesn't add an extension on to the file. Hmmm. Have to play with this some more I quess.
Thanks for your help
JWB
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1