-2

I'm trying to create a script that will send across files from one server to another. My script successfully does that as well as checks if the file has something in it or not. My next step is to check whether the file already exists on the server; if the file already exists it does not send and if it does not exist, it does send.
I've tried a few different things and can't seem to get my head around it. How can I get it to check whether the file already exists or not? Any help would be appreciated!
(I had a look at some similar questions but couldn't find anything specific to my issue.)

   require('constants.php');    
   $files = $sftp->nlist('out/');    
   foreach($files as $file) {    
     if(basename((string) $file)) {    
       if(strpos($file,".") > 1) { //Checks if file    
         $filesize = $sftp->size('out/'.$file); //gets filesize    
         if($filesize > 1){    
           if (file_exists('import/'.$file)){    
              echo $file.' already exists';
            }    
            else {
              $sftp->get('out/'.$file, 'import/'.$file); //Sends file over
             //$sftp->delete('out/'.$file); //Deletes file from out folder
            }    
            else {
              echo $file. ' is empty.</br>';
            }
          }
        }
      }
    }

EDIT: To try and get this to work, I wrote the following if statement to see if it was finding the file test.php;

if (file_exists('test.txt')){
  echo 'True';
} else {
  echo 'False';
}

This returned true (a good start) but as soon as I put this into my code, I just get a 500 Internal Server Error (extremely unhelpful). I cannot turn on errors as it is on a server that multiple people use.
I also tried changing the file_exists line to;

if (file_exists('test.txt'))

in the hopes that would work but still didn't work.
Just to clarify, I'm sending the files from the remote server to my local server.

Ember
  • 284
  • 2
  • 12
  • Please can you give examples of what you have tried so far, including the code you used, this may help in finding an answer to your issue. – 5202456 Aug 29 '18 at 09:58
  • 1
    I've added some edits in to try and help. – Ember Aug 29 '18 at 10:11
  • Where does the `phpseclib` come into play? There is no reference to it in the question or in the code. – feeela Aug 29 '18 at 10:14
  • That is the package being used so that I can use SFTP – Ember Aug 29 '18 at 10:17
  • “I cannot turn on errors as it is on a server that multiple people use.” If you want to debug PHP, this is essential. You can turn on error-reporting during runtime too: `ini_set('error_reporting', 1); error_reporting(E_ALL);`. This way you should see an error message instead of just the HTTP 500 notice of your browser. – feeela Aug 29 '18 at 10:17
  • feeela - I have tried doing this but it's still returning a 500 Internal Server error. Just my luck... – Ember Aug 29 '18 at 10:20

2 Answers2

1

Your code checks the file exist in your local server not in remote server.

if (file_exists('import/'.$file)){    
echo $file.' already exists';
}    

You need to check in remote server using sftp object like

if($sftp->file_exists('import/'.$file)){
echo $file.' already exists';
}

Edit:

Add clearstatcache() before checking file_exists() function as the results of the function get cached. Refer: file_exists

balakrishnan
  • 383
  • 4
  • 12
  • The file is being sent from the remote server to my local server. Sorry if this wasn't clear. – Ember Aug 29 '18 at 10:10
1

There is a closing curly brace missing right before the second else keyword.

Please try to use a code editor with proper syntax highlighting and code formatting to spot such mistakes on the fly while you are still editing the PHP file.

The corrected and formatted code:

require('constants.php');
$files = $sftp->nlist('out/');
foreach ($files as $file) {
    if (basename((string)$file)) {
        if (strpos($file, ".") > 1) { //Checks if file
            $filesize = $sftp->size('out/' . $file); //gets filesize
            if ($filesize > 1) {
                if (file_exists('import/' . $file)) {
                    echo $file . ' already exists';
                } else {
                    $sftp->get('out/' . $file, 'import/' . $file); //Sends file over
                }
            } else {
                echo $file . ' is empty.</br>';
            }
        }
    }
}
feeela
  • 29,399
  • 7
  • 59
  • 71
  • 1
    Well now I feel silly, it's amazing what such a simple mistake can do. I use Atom which has Linter installed but it's very temperamental and doesn't always work. Nonetheless, your edit seems to work so thank you! – Ember Aug 29 '18 at 10:28