0

I am able to save the results as JSON. The problem I am having is saving it to a network drive.

I confirmed the path is good by using the following:

if ($handle = opendir('//network/IS/folder1/folder2/targetdirectory')) 
{
  echo "Directory handle: $handle\n";
  echo "Entries:\n";

  // This is the correct way to loop over the directory. 
  while (false !== ($entry = readdir($handle))) {
    echo "$entry\n";
  }

  // This is the WRONG way to loop over the directory. 
  while ($entry = readdir($handle)) {
    echo "$entry\n";
  }

  closedir($handle);
}

Using the above, I am able to see all of the files in the targetdirectory. Therefore, I know the path is good. It's writing/creating the text file that is to be saved in the targetdirectory.

The whole process looks like this:

<?php
  include("../include/sessions.php");

  if(isset($_POST['selectedOption']))
  {
    $svc1 = mysqli_real_escape_string($dbc, $_POST['selectedOption']);

    $sql = "SELECT column1, column2, column3 FROM table WHERE column1 = '$svc1'";

    $query = mysqli_query($dbc, $sql);

    if($query)
    {
      $out = array(); 
      while($row = $query->fetch_assoc())
      {
        $out[] = $row;
      }
      $json = json_encode($out);  // I can change $json to echo and see the $out in the console as json

      $file = fopen(__DIR__ . "//network/IS/folder1/folder2/targetdirectory", 'w');
      fwrite($file, $json);
      fclose($file);
    }
    else
    {
      echo "Error: " . mysqli_error($dbc);
    }
?>

Using the above process, nothing is being saved into the targetdirectory.

What I can do to fix this problem?

halfer
  • 19,824
  • 17
  • 99
  • 186
John Beasley
  • 2,577
  • 9
  • 43
  • 89
  • Would file_put_contents work for you? –  Mar 05 '20 at 14:24
  • And aren't you appending the desired directory to the current directory? \__DIR\__ . "//network/IS/folder1/folder2/targetdirectory" –  Mar 05 '20 at 14:26
  • @SarPutnik - No, I am not appending. I am trying to create a new file. I am not familiar with file_put_contents. How does that work? – John Beasley Mar 05 '20 at 14:33
  • "I am not familiar with file_put_contents. How does that work?"... did you look it up? See https://www.php.net/manual/en/function.file-put-contents.php. It writes data to a file. – ADyson Mar 05 '20 at 14:39
  • `//network/IS/folder1/folder2/targetdirectory`...is this supposed to be a windows UNC fileshare path? If so it should be written using backslashes not forward slashes e.g. `\\server\sharedfolder`. – ADyson Mar 05 '20 at 14:40
  • **Warning:** Your code is vulnerable to SQL Injection attacks. You should use parameterised queries and prepared statements to help prevent attackers from compromising your database by using malicious input values. http://bobby-tables.com gives an explanation of the risks, as well as some examples of how to write your queries safely using PHP / mysqli. **Never** insert unsanitised data directly into your SQL. The way your code is written now, someone could easily steal, incorrectly change, or even delete your data. – ADyson Mar 05 '20 at 14:40
  • And your mysqli_error() call would fail because (again as per the manual! https://www.php.net/manual/en/mysqli.error.php) it requires you to supply the connection object as a parameter. `mysqli_error($dbc)`. But anyway it's easier and less verbose to just tell mysqli to throw exceptions when errors occur. See https://stackoverflow.com/a/14578644/5947043 for instructions on setting that up. – ADyson Mar 05 '20 at 14:42
  • 1
    @ADyson - I made some updates to account for SQL injections, and I updated the mysqli_error reporting. The code that I used here is a few years old. I have since switched over to PDO, prepared statements, and stored procedures. – John Beasley Mar 05 '20 at 14:49
  • @ADyson - and yes, I did look up file_put_contents, which doesn't seem will work for my problem. This is why I asked "how does that work" in hopes that an answer would be generated below. – John Beasley Mar 05 '20 at 14:51
  • "doesn't seem will work"...why not, exactly? You want to write some data to a file. You can do it in one command using file_put_contents, instead of the 3 you've already got. Have you tried it? Shouldn't be hard to use. The manual page specifically says "This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file." – ADyson Mar 05 '20 at 15:06

1 Answers1

0

I solved my problem by removing DIR and adding the filename at the end of the path:

$file = fopen("//network/IS/folder1/folder2/targetdirectory/newFile.txt", 'w');
fwrite($file, $json);
fclose($file);

The PHP command "file_put_contents", although succeeding in creating a file, only continued to append to the same file. Using the above will overwrite the target file in the targetdirectory.

John Beasley
  • 2,577
  • 9
  • 43
  • 89
  • 1
    file_put_contents should not append by default unless you used the FILE_APPEND flag. In which case the file gets overwritten. if you call **file_put_contents($filename, $json)** the functions should work as desired –  Mar 05 '20 at 16:29
  • @SarPutnik - I do thank you for your input and information. – John Beasley Mar 05 '20 at 18:40