fopen(/home/loyusgyp/public_html/logs/log_jurosefine/log_2020-02-02_18-00-01.txt): failed to open stream: No such file or directory in (Cpanel)

Asked

Viewed 105 times

-3

I’m having trouble recording a log on my hosting server (hosting uses Cpanel). I created a job of correction of interest and fine, but when the job runs it accuses the following error:

Warning: fopen(/home/loyusgyp/public_html/logs/log_jurosemulta/log_2020-02-02_18-00-01.txt): failed to open stream: No such file or directory in /home/loyusgyp/public_html/job/job-correcao-valor.php on line 77

The funny thing is that if I access the path by the url is to run manually, it performs everything you need and finally saves the log in the hosting. Example:

http://localhost/job/job-correcao-valor.php

Just follow my code:

Function correcaoJursoEMulta();

include_once(realpath(dirname(__FILE__) . "/../db/db_connect.php"));

date_default_timezone_set("America/Sao_Paulo");
$date_sem_horas = date("Y-m-d");
$date_com_horas = date("Y-m-d_H-i-s");

$titulos_em_vencimento = "SELECT pag.COD_PAGAMENTO,pag.DATA_VENCIMENTO,pag.VALOR,pla.VALOR_PLANO,alu.NOM_ALUNO,pag.DAT_OPERACAO 
                            FROM contrato con
                            inner join aluno alu        on alu.COD_ALUNO = con.COD_ALUNO
                            INNER JOIN pagamento  pag   on pag.COD_ALUNO = alu.COD_ALUNO        
                            inner join planos pla       on pla.COD_PLANO = con.COD_PLANO
                            WHERE pag.DATA_VENCIMENTO < now() 
                            AND pag.TIPO_PAGAMENTO = 'Mensalidade' 
                            AND pag.STATUS = 'Aberto'";
$resultado_titulos_em_vencimento = mysqli_query($connect, $titulos_em_vencimento) or die (mysqli_error($connect));


while($titulos = mysqli_fetch_assoc($resultado_titulos_em_vencimento))
{
    $cod_titulo = $titulos['COD_PAGAMENTO'];
    $data_titulos = $titulos['DATA_VENCIMENTO'];
    $nom_aluno = $titulos['NOM_ALUNO'];
    $valor_plano = $titulos['VALOR_PLANO'];
    $data_operacao = $titulos['DAT_OPERACAO'];

    if($data_operacao == $date_sem_horas){
        $msg = "Titulo já corrigido | Aluno: $nom_aluno | Data da operação: $date_sem_horas\n";
        logMe($msg,$date_com_horas);
    }else{
        $valor_corrigido = calcular($date_sem_horas,$data_titulos,$valor_plano);
        atualizarValor($valor_corrigido,$cod_titulo,$date_sem_horas,$nom_aluno,$date_com_horas);
    }
}

Function calculate($date_sem_hours,$data_titles,$value_plano)

$diferenca = strtotime($date_sem_horas) - strtotime($data_titulos);
    $dias = floor($diferenca / (60 * 60 * 24));

    $data1 = new DateTime( $date_sem_horas );
    $data2 = new DateTime( $data_titulos );

    $intervalo = $data1->diff($data2);
    $converterIntervaloNumero = intval($intervalo->m + 1);

    $multa = number_format(4.50, 2, '.', '');
    $multaFinal = $multa * $converterIntervaloNumero;
    $juros = $valor_plano * 0.003;
    $totalJuros = $juros * $dias;
    $calculoFinal = number_format($totalJuros + $multaFinal + $valor_plano, 2, '.', '');

    return $calculoFinal;

function atualizarValor($valor_corrigido,$cod_titulo,$date_sem_horas,$nom_aluno,$date_com_horas)

require(realpath(dirname(__FILE__) . "/../db/db_connect.php"));

$atualizar_titulo = "UPDATE pagamento set VALOR = '$valor_corrigido', DAT_OPERACAO = '$date_sem_horas' WHERE COD_PAGAMENTO = '$cod_titulo'";
$resultado_atualizar_titulo = mysqli_query($connect, $atualizar_titulo) or die (mysqli_error($connect));

if(mysqli_affected_rows($connect)){
    $msg = "Juros e multa corrigido | Aluno: $nom_aluno | Valor: $valor_corrigido | Data da operação: $date_sem_horas\n";
    logMe($msg,$date_com_horas);
}

Function logMe($msg,$date_com_hours)

$fp = fopen("../logs/log_jurosemulta/log_".$date_com_horas.".txt", "a");
$escreve = fwrite($fp, $msg);
fclose($fp);
}

2 answers

2

The mistake failed to open stream: No such file or directory is about the location below not existing:

../logs/log_jurosefine

The problem Has nothing to do with the opening mode of fopen ("a means "append"").

Just for the record, it is "unlikely" that this error is about folder permission, because when you have permission failure PHP sends the following error:

failed to open stream: Permission denied

The problems can be:

  • The briefcase logs does not exist or has another name
  • The briefcase log_jurosemulta does not exist or has another name
  • Your server is Linux and your folders have names with uppercase letters, but you wrote everything in lowercase, remember Linux is case-sensitive for files and folders
  • The .. is in a relative path and from the script I am invoked is falling in an absolute path (internally in PHP) different

In the case of the last item, which I believe is the most likely in your case, to know where the fopen is pointing do this (just to debug, remove from script after):

function logMe($msg,$date_com_horas) {

var_dump(realpath("../logs/log_jurosemulta"));
var_dump(getcwd());

$fp = fopen("../logs/log_jurosemulta/log_".$date_com_horas.".txt", "a");
$escreve = fwrite($fp, $msg);
fclose($fp);
}

The realpath will display the possible real path of the folder, if return false is because it is indeed pointing to the wrong path, in this case to facilitate it would be better to use the absolute path, because relative paths depend on the current execution path of the current script (which can be obtained with getcwd()).

If this "debugging" with var_dump return a possible path then check if it is indeed the correct one.

Note that you can use __DIR__, but before using have to be careful and understand it, the __DIR__ indicates which script folder is included, so suppose (is hypothetical) that its structure is so:

./public_html
  ├───index.php
  ├───libs
  │   ├───conexao.php
  │   └───funcoes.php
  │
  └───logs
      └───log_jurosemulta

And supposing its function function logMe is in the file funcoes.php the script should look like this:

function logMe($msg,$date_com_horas) {
    $fp = fopen(__DIR__ . "/../logs/log_jurosemulta/log_".$date_com_horas.".txt", "a");
    $escreve = fwrite($fp, $msg);
    fclose($fp);
}

In this case the __DIR__ would have the following value:

./public_html/libs

Then concatenating with /../logs/log_jurosemulta/*, he climbs a level, going to public_html and from this accessing the folder where the logs should be, but note, this is all to explain, I imagine that you should have a different structure, so just understand the levels of the folders, not just copy and paste the code, have to adjust to what you have.

0

That answer is correct in stating that the problem is in the path to your file. However, I would like to point out an alternative, so that it is not necessary to check all the relative path uses.

Considering the points

I created a job of correction of interest and fine, but when the job runs it accuses the following error

and

if I access the path by the url is run manually

it is possible to infer that the problem is not in the script, but in the cron job setup. (It was even better to have made that more explicit and include cron setup to the question.)

To fix the problem running the script via cron, you need to change your cron.

I imagine something like * * * * * /usr/bin/php /home/loyusgyp/public_html/job/job-correcao-valor.php has been done, the path to the PHP executable, how different the scheduling can be.

In this case, you need to change the command that runs in CRON to cd /home/loyusgyp/public_html && /usr/bin/php ./job/job-correcao-valor.php and the script will be executed from /home/loyusgyp/public_html and consequently will attempt to write the logs into /home/loyusgyp/logs/log_jurosemulta/.

  • "Does not point to proper correction", How so? He didn’t even present how he ran the script, regardless of how it was I answered that yes, I said very clearly in "because relative paths depend on the current execution path of the current script (which can be obtained with getcwd()).", or is he used ../ which is relative, if the script is accessible via HTTP and at the same time by CRON then __DIR__ perfectly resolves always need to be explicit in anything, because the __DIR__ is already the "explicit" (or almost, depending on what comes after).

  • @Guilhermenascimento, is better now?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.