0

I am writing an application in PHP where the user submits a form of data and a file name is chosen based off of the data, like so:

$filename = "./savelocation/".$name."_".$identification."_".$date.'.txt';

I am trying to use the file_exists() function to check to see if a file with the same name exists. If it does, the final name is changed to prevent overwriting the submitted form data. Here is my implementation:

$file = "./savelocation/".$name."_".$identification."_".$date.'.txt';
$file = preg_replace('/\s+/', '', $file); 
$filepath = "./savelocation/".$name."_".$identification."_".$date.'.txt';

if(file_exists($filepath))
{
    $file = "./savelocation/"."INVALIDFILE".'.txt';
} 

This prevents people from overwriting applications by changing the name to a single file which acts as the 'default file' in which it doesn't matter if it is overwritten. However, I know this is wrong. My logic was that the if statement would return true, which would execute the code inside of the statement changing the file name to the 'default file'. Is this even a good way to prevent duplicate submissions?

skeletonne
  • 83
  • 1
  • 9
  • You can prevent duplicates using glob() to get an array of all the files in the directory, then use a foreach loop to match file names – The One and Only ChemistryBlob Sep 10 '14 at 02:34
  • or just create a unique name for each file (a very common approach) –  Sep 10 '14 at 02:36
  • I am creating a unique name for each file, my only worry is that if a user resubmits their data by refreshing, it will overwrite their file. – skeletonne Sep 10 '14 at 02:40
  • then it can't be unique? –  Sep 10 '14 at 02:41
  • Well I guess it is technically unique, I think. It is still the form that the user filled out, the same data was just resubmitted again. It is unique because the filename is constructed using their name and a special ID. The file is rewritten with the same data, I just don't want them to be able to resubmit. – skeletonne Sep 10 '14 at 02:43
  • stopping form resubmission - its not the same issue, there are many way to do that: http://stackoverflow.com/questions/6320113/how-to-prevent-form-resubmission-when-page-is-refreshed-via-php –  Sep 10 '14 at 02:47
  • Unfortunately I can't really figure out how to implement this, which is why I haven't done that method yet. :( Perhaps I should study it some more. – skeletonne Sep 10 '14 at 03:02
  • If you don't want them to overwrite an existing file ... just, don't overwrite it :) – Ja͢ck Sep 10 '14 at 03:43
  • @Jack You are brilliant. Thank you so much! I could just redirect them to a page saying they already uploaded a form and exit the script, which prevents the file from being created. I just didn't want them overwriting the file they already submitted, haha. – skeletonne Sep 10 '14 at 04:14

2 Answers2

0

Try this...if there is a match on the file name, break from the loop and redirect

$userFile = $name."_".$identification."_".$date.'.txt;
$fileArray = glob('./savelocation/*');
$arrCount = count($fileArray);
$i = 1;
$msg = null;

foreach ($fileArray as $FA) {
    $fileSubstring = str_replace("\.\/savelocation\/", "", $FA);
    if ($i > $arrCount) {
        break;
    } else if ($userFile === $fileSubstring) {
        $msg = 'repeat';
        break;
    } else null;
    $i++;
}

if (isset($msg)) header('location: PageThatChastisesUser.php');

Alternatively, if you tweak your code a bit to change your file name, this should work:

if(file_exists($file)) {
    $file = str_replace("\.txt", "duplicate\.txt", $file); 
}

Change the file name in a way that identifies itself to you as a duplicate.

  • This seems to be causing an error when I begin to write to a file later in the code. I'm not sure why though. – skeletonne Sep 10 '14 at 03:02
  • No, I am just using Notepad++ with a combination of WAMP server. Relatively new to this, sorry! I guess if I can figure out how to change a file name if it already exists in the directory I'll be golden. But if I use file_exists() wouldn't it check under the whole directory for the specific file name? – skeletonne Sep 10 '14 at 03:13
  • Your second edit didn't work for some reason - when the application with the same information is submitted, it overwrites the file again. It still has the same info, I would just like users not to be able to change their first application. However I don't see what the problem is though.. the filepath seems to be correct. – skeletonne Sep 10 '14 at 03:30
  • Which gets saved on the server? $file or $filepath? – The One and Only ChemistryBlob Sep 10 '14 at 03:35
  • $file gets saved on the server. Well, it actually gets saved on the www directory of WAMP (which I think is saving on the server). But I think the issue may be with the if statement because $file would be modified to be the duplicate file. – skeletonne Sep 10 '14 at 03:36
  • It still isn't working for me. I think I'm doing something wrong on my end. I apologize, thank you so much for your time. I'll keep working at it. – skeletonne Sep 10 '14 at 03:50
0

Here's one way of doing it:

$file = "./savelocation/".$name."_".$identification."_".$date.'.txt';
$file = preg_replace('/\s+/', '', $filen); 
$filepath = "./savelocation/".$name."_".$identification."_".$date.'.txt';

$i = 1;
while(file_exists($filepath))
{
   $filepath = "./savelocation/".$name."_".$identification."_".$date.'_'.$i.'.txt';
   $i++;
}