PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 307,109 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,021 people online right now. Registration is fast and FREE... Join Now!




get_new_file dreamincode snippet doesn't work

 

get_new_file dreamincode snippet doesn't work, can't get snippet to prevent file overwrite

fortesque

5 Nov, 2009 - 09:56 AM
Post #1

New D.I.C Head
*

Joined: 2 Jun, 2009
Posts: 10

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:
CODE

<?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.

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");

?>




User is offlineProfile CardPM
+Quote Post


jwwicks

RE: Get_new_file Dreamincode Snippet Doesn't Work

5 Nov, 2009 - 06:20 PM
Post #2

D.I.C Head
Group Icon

Joined: 31 Jul, 2008
Posts: 151



Thanked: 22 times
Dream Kudos: 250
My Contributions
Hello,

QUOTE(fortesque @ 5 Nov, 2009 - 09: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:
CODE

<?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
User is offlineProfile CardPM
+Quote Post

fortesque

RE: Get_new_file Dreamincode Snippet Doesn't Work

6 Nov, 2009 - 04:15 AM
Post #3

New D.I.C Head
*

Joined: 2 Jun, 2009
Posts: 10

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
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 12:50PM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month