Jump to content

file_exists problem


PerHaPs

Recommended Posts

Hi,

This bit of code is supposed to detect which filenames have been used already
out of a possible 12
1.jpg,2.jpg ...... 12.jpg.

If 1.jpg does not exist,
$filenum_next should be '1'.

THE CODE
for($i=1;$i<=12;$i++)
{
if($breaker)break;
$filename_to_check=$i.'.jpg';
$opendir=opendir($path_thumbs);
while (false !== ($file=readdir($opendir)))
{
if($breaker)break;
if($file != '..' && $file !='.' && $file !='')
{
if(!file_exists($filename_to_check)) 
{
$filenum_next=$i;
$breaker=1;
}
}
}
}

Instead it acts as if the directory is empty. It suggests 2.jpg should be the next filename. Next time it assigns 2.jpg again and overwrites the file.

Any thoughts? Thanks.
Link to comment
https://forums.phpfreaks.com/topic/33384-file_exists-problem/
Share on other sites

Why are you looping through the directory. Just check to see if the file exists, if it doesn't return that number and use it.
[code]<?php
$path_thumbs = 'thmbs';
$nextnum = check_for($path_thumbs);
if ($nextnum === false) echo "no more files available";
else {
  echo $nextnum; // for testing
//
//
//  do work
//
//
}

function check_for($path) {
  $ret = false;
  for ($i=1;$i<13;$i++)
    if (!file_exists($path . '/' . $i . '.jpg')) return($i);
  return($ret);
}
?>[/code]

Ken
Link to comment
https://forums.phpfreaks.com/topic/33384-file_exists-problem/#findComment-156042
Share on other sites

That is neat. Thanks. I tried looping because not looping didn't work. Seems main problem was path to the directory was not correct.

Maybe this should be a new post?
During test, if I have say 3 images in the dir, and delete the second one with the following:



[code]if($delimg==1)
{
$todelete=$path_thumbs.'/'.$delname;
$opendir=opendir($path_thumbs);
if(file_exists($todelete))unlink($todelete);
$delimg=0;
}[/code]


where $delimg=1 and $delname=2.jpg are in a query string attached to a delete link,

the file is successfully deleted.
However, when I upload a new one, which gets the name 2.jpg, the ORIGINAL 2.jpg image appears.
How is that possible?

Link to comment
https://forums.phpfreaks.com/topic/33384-file_exists-problem/#findComment-156225
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.