1

I have created one process to read information from files and save into database, everything works fine in my desenv environment, but when I have put files in my php host (production environment) the process fail when read files.

to execute my process, I have created one cron job on cpanel, whith the command bellow:

php -q /home/<hostfolder>/batch/index.php

When my process is executed by cron, the output say that don't have files. Bellow part of my code:

private $sourceFilesFolder = "/home/<host folder>/public_html/batch/arquivos";  
private $destFilesFolder = "/home/<host folder>/public_html/batch/processados";
private $log;
private $trataException;

function __construct($log, $trataException) {
    $this->log = $log;
    $this->trataException = $trataException;
}

/**
 * Read the source folder and select only files
 * @return array - Array of valid files
 */
function selectFiles() {

    // Save the first read of ftp folder
    $listSourceFolder = scandir ( $this->sourceFilesFolder );

    // Array tho save only valid files
    $listFiles = array ();

    // read the array with ftp content and save in listFiles only files
    foreach ( $listSourceFolder as $file ) {
        $verifica = $this->sourceFilesFolder . "\\" . $file;

        // if is a file type, try save in listFiles array
        if (($file != ".") && ($file != "..") && (!is_dir ( $verifica ))) {

            // verifiy if the file exists
            if (file_exists ( $verifica )) {

                $this->log->gravaLog ( $file . " -> ADDED TO PROCESS" );
                //verificaArquivoEmUso ( $verifica );
                array_push ( $listFiles, $verifica );
            } else
                $this->log->gravaLog ( $file . "-> do not exist." );
        } else
            $this->log->gravaLog ( $file . "-> not is a file." );
    }
    return $listFiles;
}

In my folder I have two txt files and this appear in the $listSourceFolder variable, but when I check this files with file_exists, always return false.

First, I have put my code files in a bacth folder in /home/

In the second test, I move the files in ftp folder and put inside the bacth folder (same of my code).

In the third test, I moved all batch folder (with codes and txt files) to public_html folder.

Nothing work, always the same error, file not exists.

I tryed remove ths file_exists if, but occur erros on the next step of algoritm.

I have checked the file permissions, and all permissions are ok.

What is I can do???

user1352652
  • 425
  • 1
  • 8
  • 21
  • 1
    From your mention of /home/ and cron I would guess you are running *nix. Why are you using the `\` path delimited usually used on windows instead of the `/` usually used on *nix? Also, what is the value of `$verifica` right before you check it with `file_exists`? Have you verified that the filepath and filename pointed to by `$verifica` actually exist? – ThatOneDude Aug 12 '15 at 01:07
  • Man, you are my hero. My desenv environment is windows, so i'm using '\'. if you had not seen it, I would not notice. Thanks a lot – user1352652 Aug 12 '15 at 02:01

2 Answers2

1

You can try three things.

1 - chmod 777 (Give permission so php can read and write files)

2 - I know its practically impossible that your server has a lower version of php. Scandir only works php 5 above. So you might wanna check that.

3 - There's a module called "mod_speling", try put that on.

;)

Raf Zeres
  • 71
  • 3
0

It appears that you are using the incorrect path delimiter for *nix.

You might change your code to be the following instead:

$verifica = $this->sourceFilesFolder . "/" . $file;

ThatOneDude
  • 1,516
  • 17
  • 20