acctman
03-07-2006, 08:13 PM
does anyone know how i can stop the following php error from echo'ing. The the image file is not there so it can't delete it. No big deal but, I would like for it not to print it to the page after processing the code.
Warning: unlink(/home/rmy/public_html/images/pictures/-102569.jpg): No such file or directory in /home/rmy/public_html/admin/includes/delnewguy.inc.php on line 23
php line code 23
unlink($_SERVER["DOCUMENT_ROOT"] . '/images/pictures/'.$line['i_user'].'-'.$key.'.jpg');
CodeMaker
03-07-2006, 08:55 PM
Well you could just comment that line, or put error_reporting(E_NONE); somewhere before that line to prevent the error from showing up.
Dan L
03-07-2006, 09:00 PM
$file = '/images/pictures/'.$line['i_user'].'-'.$key.'.jpg';
if(file_exists($file)) {
unlink($_SERVER["DOCUMENT_ROOT"] . $file);
}
Alternatively, put an @ sign before the unlink() to suppress errors.
acctman
03-07-2006, 09:25 PM
thanks DanX... I have another question for you, would this work?
$file = '/images/pictures/'.$line['i_user'].'-'.$key.'.jpg';
$file1 = '/images/backup/'.$line['i_user'].'-'.$key.'.jpg';
$file2 = '/images/thumbnails/'.$line['i_user'].'-'.$key.'.jpg';
$file3 = '/images/small/'.$line['i_user'].'-'.$key.'.jpg';
if(file_exists($file, $file1, $file2, $file3)) {
unlink($_SERVER["DOCUMENT_ROOT"] . $file);
unlink($_SERVER["DOCUMENT_ROOT"] . $file1);
unlink($_SERVER["DOCUMENT_ROOT"] . $file2);
unlink($_SERVER["DOCUMENT_ROOT"] . $file3);
}
Dan L
03-07-2006, 09:46 PM
Nope, you can only have one argument for file_exists.
Try something like..
<?php
$fileName = $line['i_user'].'-'.$key.'.jpg';
$dirArray = array('pictures','backup','thumbnails','small');
foreach($dirArray as $key => $value) {
$value = "images/$value/";
if(file_exists($value.$fileName)) {
unlink($_SERVER["DOCUMENT_ROOT"].$value.$fileName);
}
}
?>
acctman
03-07-2006, 10:05 PM
thanks, everything worked.
SolidSolutions
09-09-2010, 02:18 PM
After a few searches, came across Dan's solution.
THANK YOU!
My site looks real ugly in google search results because the error is output as the page content and thus shows in the listings.
I tried to use PHP exit or die function with no success.
I had no idea you could check a file for existence before trying to open it. Great concept.
Preventing the error message with PHP on fopen as Dan suggests works great!
adhfu
09-10-2010, 12:32 AM
i came across before, but now it worked. thanks!
adhfu
09-10-2010, 12:36 AM
your sharing is appreaiated, i will record it.