473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

problem with file_exists caching even when clearstatcache() is called

Hi guys,

I've created this little function to check whether a user has uploaded a
file with the same name as an existing file, and if so rename it to
file-1.jpg, file-2.jpg etc.

clearstatcache();
if(file_exists("$BasePath/$FileName.jpg")){
$extra=1;
while(file_exists("$BasePath/$FileName-$extra.jpg")){
$extra++;
}
$FileName="$FileName-$extra";
}

However if files are deleted outside PHP file_exists still returns true so
it keeps adding higher numbers to the files.
Is there any way I can get round this? The strange thing is if I call
file_exists() on a folder it works fine.

--
Regards,
Andrew Crowe
Jul 17 '05 #1
2 3736
Sorry forgot to mention I'm using PHP5 RC2 on Windows 2000 / IIS (isapi)

--
Regards,
Andrew Crowe
Jul 17 '05 #2
"Andrew Crowe" <an************@yahoo.co.uk> wrote in message
news:40**********************@news.easynet.co.uk.. .
Hi guys,

I've created this little function to check whether a user has uploaded a
file with the same name as an existing file, and if so rename it to
file-1.jpg, file-2.jpg etc.

clearstatcache();
if(file_exists("$BasePath/$FileName.jpg")){
$extra=1;
while(file_exists("$BasePath/$FileName-$extra.jpg")){
$extra++;
}
$FileName="$FileName-$extra";
}

However if files are deleted outside PHP file_exists still returns true so
it keeps adding higher numbers to the files.
Is there any way I can get round this? The strange thing is if I call
file_exists() on a folder it works fine.

--
Regards,
Andrew Crowe


There's a race condition in your code. Between the time when file_exists()
was called and the subsequent move_uploaded_file() a file with that name
could conceivably have been created. A better approach is to keep calling
move_uploaded_file() until it works:

$extra = 1;
while(!@move_upload_file($tmpname, $filename)) {
$filename = "$BasePath/$FileName-$extra.jpg";
$extra++;
}

This bypasses the caching issue as well.
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Chann Becker | last post by:
My current setup: WWW Server: Windows 2000 Server, SP4 IIS5 PHP 4.3.4 File Server Windows 2000 Server, SP4
2
by: Ian Hardcastle | last post by:
I am creating a php script that I can call from a delphi program, this itself is easy as I have already done this. The problem lies within the php script, here is the code that DOESN'T work: <?...
10
by: Google Mike | last post by:
{NOTE: I have PHP 4.2.2 for RH9 Linux.} Anyone have a better file_exists() out there? Even if you use shell out tricks with Linux using the `command` trick, I'd be interested to see what you...
1
by: Hinrich Specht | last post by:
Hello, I have a problem using file_exists. I want to use file_exists to dertermine if a product-image is available or not to show either the product-image or a standard-image. This is the code:...
3
by: Kenny Halley | last post by:
I am a newb I have to admit at this programming lark (i have tried various languages in the past but can't seem to find my way round them, but for once I feel I am getting somewhere with php and...
2
by: Disco Octopus | last post by:
Hi, I am trying to upload my site to a new server, and am finding a hard time with the include() function. I am getting this... Warning: main(includes/site_auth.inc) : failed to open...
3
by: Pitcairnia | last post by:
The basic purpose of the site is for authenticated users to post event listings, which often include photographs. The user is faced with a page where they can insert all of the information about...
6
by: +86 | last post by:
i encountered this problem: "include('inc.php')" will work problely but "include('./inc.php') doesn't work .. both file_exists('inc.php') or file_exists('./inc.php') didn't return the right...
15
by: cooldht | last post by:
Hi all, I ran into a cool function in a google search, called file_exists that would do exactly what I need it to do. Unfortunately, I think something is wrong with the logic I'm using, because...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.