Jump to content

[SOLVED] If file_exists... loop question


A JM

Recommended Posts

I want to test for the existence of a file in the directory before saving. If the file already exists then I want to  increment the file name by appending a number to end of the file name, then save the file. The caveat for this file save routine is that if this process has happend before, filename1.pdf exists then the new file should increment one more number to filename2.pdf, etc...

 

I'm not familiar with loops and wanted to make sure that there isn't an easier/better way to do this, any ideas or thoughts? I hacked this up and I think will work????

 

$fnamecnt= 1;

$newfname = false;

$fname = preg_replace('/\..*$/','',$fname); //filename no extension

$ext = substr($fname, strpos($fname,'.'), strlen($fname)-1); //file extension

while ( $newfname = false ) {

//Test if file exists and rename if needed

if (!file_exists($uploaddir . $fname . $ext)) {

//Move file to new destination

if (move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir . $fname . $ext)) {

// uploaded file was moved and renamed succesfuly. Display a message.

echo "Upload successful!";

} else {

echo "Error while uploading the file, Please contact the webmaster.";

unset($fname . $ext);

}

$newfname = true;

} else {

$fname = $fname . $fnamcnt;

}

$fnamecnt = $fnamcnt +1;

}

 

Thanks.

 

A JM,

Link to comment
Share on other sites

while ( $newfname = false ) {

 

Will always be true. You need to use == for a comparison operator.

 

Alternatively, you can just do a break; when the file_exists and it will exit out of the loop.

Link to comment
Share on other sites

$file = 'kenny';

$ext = 'pdf';

$count = 0;

if (file_exists($file.'.'.$ext)) {

  while (file_exists("{$file}{$count}.{$ext}")) {

    $count++;

  }

  // save file with name "{$file}{$count}.{$ext}"

} else {

  // save file with correct name

}

Link to comment
Share on other sites

while ( $newfname = false ) {

 

Will always be true. You need to use == for a comparison operator.

 

Alternatively, you can just do a break; when the file_exists and it will exit out of the loop.

 

With regard to the Break; comment do you mean to not check for $newfilename = true; instead just use break;?

 

I'll still need the variable though correct (while ( $newfname == false )) otherwise there's nothing for it to loop against?

 

Thanks for the post premiso but I'm working in PHP and not that familiar with javascript.

 

A JM,

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.