Solved

php prevent error

Posted on 2014-12-26
17
203 Views
Last Modified: 2014-12-26
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";
                }
       }
}
0
Comment
Question by:projects
17 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40519003
Remove this condition

if ( $fn ) {

And the @ symbol

What is the error you are getting?
0
 

Author Comment

by:projects
ID: 40519118
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
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40519131
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40519151
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
 

Author Comment

by:projects
ID: 40519169
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
 
LVL 58

Expert Comment

by:Gary
ID: 40519174
Did you try the SetEnvIf, that should disable any logging for that directory

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

Open in new window

0
 

Author Comment

by:projects
ID: 40519187
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
 
LVL 58

Expert Comment

by:Gary
ID: 40519191
And restarted Apache?
0
 

Author Comment

by:projects
ID: 40519217
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
 
LVL 58

Expert Comment

by:Gary
ID: 40519221
You can't do it in PHP because this is a server level message and not a PHP message
0
 

Author Comment

by:projects
ID: 40519223
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
 
LVL 58

Expert Comment

by:Gary
ID: 40519225
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
 

Author Comment

by:projects
ID: 40519226
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
 
LVL 58

Expert Comment

by:Gary
ID: 40519229
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
 

Author Comment

by:projects
ID: 40519231
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
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 40519238
Can you check error_reporting function?
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question