php prevent error

I have this small snip of code which checks for the existence of a file. If the file is missing, it should not throw an error in the logs but gracefully exit. Wondering if someone could help me with this, by adding code which will prevent the apache error.

        elseif ( $_POST['function'] === 'fwfinal' ) {
                $file = $_POST['filename'];
                $fn = "logfiles/$file";
                if (@file_exists($fn)) {
                if ( $fn ) {
                    unlink ($fn);
                    return "Success";
                }
       }
}
projectsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
Remove this condition

if ( $fn ) {

And the @ symbol

What is the error you are getting?
0
projectsAuthor Commented:
Well, the client is looking for a file in a subdirectory.
When the file is not there, is it creating an error in the Apache logs which is uselessly filling the logs.

The error is;

"HEAD /subdir1/logfiles/fxrr44.log HTTP/1.1" 404 -

The client knows and is looking for a specific log file name.

I will wait to hear back from you before changing anything. Also, can you just edit the code and paste the full output just to make sure I don't mess something up.
0
GaryCommented:
I think you are going have to do this from with the Apache conf files

SetEnvIf Request_URI "^/subdir1/logfiles/*.log$" dontlog

Open in new window


http://www.serverwatch.com/tutorials/article.php/10825_3376671_2/Advanced-Logging-Techniques-With-Apache.htm

        elseif ( $_POST['function'] === 'fwfinal' ) {
                 $file = $_POST['filename'];
                 $fn = "logfiles/$file";
                 if (file_exists($fn)) {
                     unlink ($fn);
                     return "Success";
        }
 }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Ray PaseurCommented:
In PHP you can use the @ notation to suppress PHP messages about errors.  This is a bit risky -- it can cause PHP scripts to fail silently without any PHP-evident error information at all.  I don't know how to suppress the information in the Apache logs, so I am going to edit your question topic areas in the hope that someone with Apache knowledge can help.  But in PHP I think you can find out if a file exists and skip over the process of deleting it when it does not exist with something like this:

$file = $_POST['filename'];
$fn   = "logfiles/$file";
if (file_exists($fn)) 
{
    unlink ($fn);
}
return 'Success!';

Open in new window

0
projectsAuthor Commented:
Hi Ray,

I've posted about this before but the details are that the connections allowed to this directory are all authenticated. When the file doesn't exit, it is generating an error 404 which is filling my logs making it hard to see actual errors.

This is not really an error in my case, it simply means that there was no file waiting this time so I would like to prevent the error.

Once the client has downloaded the file, it should be deleted.

I have tried both suggestions and neither one works, the error still shows up in the error log.
Note also that this is an ssl connection so I've added the apache directive in the appropriate section of this particular virtual host.
0
GaryCommented:
Did you try the SetEnvIf, that should disable any logging for that directory

SetEnvIf Request_URI "^/subdir1/logfiles/$" dontlog

Open in new window

0
projectsAuthor Commented:
Yes, I added this to httpd.conf in the appropriate section.
This is the URL and not the server path right? Just double checking.

Also, I had an original question which was the same problem when the code was different.

http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28477747.html
0
GaryCommented:
And restarted Apache?
0
projectsAuthor Commented:
Yes.

I found that if I took the path out, then it works. I would prefer having the full path but this works.

            LogLevel warn
            SetEnvIf Request_URI \.log dontlog
            ErrorLog /var/www/hosts/somehost/logs/ssl_error_log
#           TransferLog /var/www/hosts/somehost/logs/ssl_access_log
            CustomLog /var/www/hosts/somehost/logs/ssl_access_log combined env=!dontlog

I would certainly prefer eliminating the error through php however.
0
GaryCommented:
You can't do it in PHP because this is a server level message and not a PHP message
0
projectsAuthor Commented:
In a previous question, it was done on the php side.
When the client would look for its file, if it was there, it downloaded it then deleted it.
If the file was not there, it would not generate an error in the logs.
0
GaryCommented:
There is a difference between PHP error logs and Apache logs
If a file is not found (404) then there is no PHP executed to prevent the logging - this is done by Apache before PHP even comes into the equation.
0
projectsAuthor Commented:
Yes, I understand that. What I am saying that is in a previous question about the same problem, someone offered a solution which updated my php code so that there was no error being generated when the file was not there.

At least, I believe that was the case but I don't know now, it's been too long.
0
GaryCommented:
I'm not sure what http:#a40519151 has to do with the answer since it just copied my amended version of your code from an hour earlier

I will look into it a bit more, but I don't see how.
0
projectsAuthor Commented:
Maybe I posted the wrong question. I'm in the middle of a number of things so would have to look closer.
I'm not worried about it, the Apache directive will work fine from now on for this purpose and I've updated the code based on Ray's input also.
0
Kerem ERSOYPresidentCommented:
Can you check error_reporting function?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Web Server

From novice to tech pro — start learning today.