Stop rename script from writing to the error log each time it can't find file

Hey guys,

Have this script which moves a file from one directory to another if it exists. Problem is if it doesn’t exist yet it writes an error to the error log. How do I stop this from happening?

<?php

$filename = '/home/plan/public_html/export/nz_orders.xlsx';
$filename2 = '/home/plan/public_html/export/nz_order_history.sql';
$filename3 = '/home/plan/public_html/export/nz_order_product.sql';
$filename4 = '/home/plan/public_html/export/nz_order_total.sql';

if (file_exists($filename)) {
    rename("/home/plan/public_html/export/nz_orders.xlsx", "/home/plan/orders.plan.co.nz/imports/nz_orders.xlsx");
} else {
    // do nothing
}

if (file_exists($filename2)) {
    rename("/home/plan/public_html/export/nz_order_history.sql", "/home/plan/orders.plan.co.nz/imports/nz_order_history.sql");
} else {
    // do nothing
}

if (file_exists($filename3)) {
    rename("/home/plan/public_html/export/nz_order_product.sql", "/home/plan/orders.plan.co.nz/imports/nz_order_product.sql");
} else {
    // do nothing
}
    
if (file_exists($filename4)) {
    rename("/home/plan/public_html/export/nz_order_total.sql", "/home/plan/orders.plan.co.nz/imports/nz_order_total.sql");
} else {
    exit; // exit the script
}   

?>

I can’t see why this code would produce an error, since you don’t call rename() if the file does not exist. Can you show an extract from the error log? Is it a permissions thing, that allows you to check the file, open it, read it, but not delete it (as a rename effectively does)?

As you have variables defined in the top of the script, why don’t you use them in the rename() calls? Won’t make any difference to how it works, just seems a bit strange.

Actually you’re right, might be another error

[23-Jul-2018 10:00:02 UTC] PHP Warning:  rename(/home/plan/public_html/system/storage/cache/export/backup.sql,/home/plan/public_html/export/nz_order_total.sql): No such file or directory in /home/plan/public_html/admin/controller/module/universal_import.php on line 2425

[23-Jul-2018 12:27:02 UTC] PHP Warning:  rename(/home/plan/public_html/system/storage/cache/export/backup.sql,/home/plan/public_html/export/nz_order_total.sql): No such file or directory in /home/planmate/public_html/admin/controller/module/universal_import.php on line 2425

Not sure, my php isn’t great

Well, the error tells you which line of code is causing the problem. The file name suggests that it isn’t one of those in the code you posted initially.

I have contacted the developer of the extension, it may be an issue with their extension

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.