chmod example


  protected static function writeFile($directory$filename$contents$force) {
    $file_path = $directory . DIRECTORY_SEPARATOR . $filename;
    // Don't overwrite if the file exists unless forced.     if (file_exists($file_path) && !$force) {
      return TRUE;
    }
    // Try to write the file. This can fail if concurrent requests are both     // trying to write a the same time.     if (@file_put_contents($file_path$contents)) {
      return @chmod($file_path, 0444);
    }
    return FALSE;
  }

}
        if ( ! $files ) {
            return true;
        }

        // Check all files are writable before attempting to clear the destination.         $unwritable_files = array();

        // Check writability.         foreach ( $files as $file ) {
            if ( ! $wp_filesystem->is_writable( $file ) ) {
                // Attempt to alter permissions to allow writes and try again.                 $wp_filesystem->chmod( $file, FS_CHMOD_FILE );
                if ( ! $wp_filesystem->is_writable( $file ) ) {
                    $unwritable_files[] = $file;
                }
            }
        }

        if ( ! empty( $unwritable_files ) ) {
            return new WP_Error( 'files_not_writable', $this->strings['files_not_writable']implode( ', ', $unwritable_files ) );
        }

        foreach ( $files as $file ) {
            
unlink( $tempfile );
            return false;
        }

        fseek( $temphandle, 0 ); // Skip back to the start of the file being written to.
        $ret = ftp_fput( $this->link, $file$temphandle, FTP_BINARY );

        fclose( $temphandle );
        unlink( $tempfile );

        $this->chmod( $file$mode );

        return $ret;
    }

    /** * Gets the current working directory. * * @since 2.5.0 * * @return string|false The current working directory on success, false on failure. */
    

    protected function writeProxyClass($fileName$content)
    {
        $tmpFile = tempnam(\dirname($fileName)basename($fileName));
        if (\is_string($tmpFile) && @file_put_contents($tmpFile$content) !== false && @rename($tmpFile$fileName)) {
            @chmod($fileName, 0666 & ~umask());

            return;
        }

        throw new Enlight_Exception(sprintf('Unable to write file "%s"', $fileName));
    }
}
/* For Windows servers and safe_mode "on" installations we'll actually * write a file then read it. Bah... */
        if (is_dir($file)) {
            $file = rtrim($file, '/') . '/' . bin2hex(random_bytes(16));
            if (($fp = @fopen($file, 'ab')) === false) {
                return false;
            }

            fclose($fp);
            @chmod($file, 0777);
            @unlink($file);

            return true;
        }

        if (is_file($file) || ($fp = @fopen($file, 'ab')) === false) {
            return false;
        }

        fclose($fp);

        
    // executable script named 'git' that always exits with 127, as if git were     // not found. Note that we run our tests using process isolation, so we do     // not need to restore the PATH when we are done.     $unavailableGitPath = $this->getWorkspaceDirectory() . '/bin';
    mkdir($unavailableGitPath);
    $bash = <<<SH #!/bin/bash exit 127 SH;
    file_put_contents($unavailableGitPath . '/git', $bash);
    chmod($unavailableGitPath . '/git', 0755);
    // Confirm that 'git' is no longer available.     $env = [
      'PATH' => $unavailableGitPath . ':' . getenv('PATH'),
      'COLUMNS' => 80,
    ];
    $process = new Process([
      'git',
      '--help',
    ], NULL, $env);
    $process->run();
    $this->assertEquals(127, $process->getExitCode(), 'Fake git used by process.');

    
$this->fileSystem = new FileSystem($this->streamWrapperManager, $settings$this->logger);
  }

  /** * @covers ::chmod */
  public function testChmodFile() {
    vfsStream::setup('dir');
    vfsStream::create(['test.txt' => 'asdf']);
    $uri = 'vfs://dir/test.txt';

    $this->assertTrue($this->fileSystem->chmod($uri));
    $this->assertFilePermissions(FileSystem::CHMOD_FILE, $uri);
    $this->assertTrue($this->fileSystem->chmod($uri, 0444));
    $this->assertFilePermissions(0444, $uri);
  }

  /** * @covers ::chmod */
  public function testChmodDir() {
    vfsStream::setup('dir');
    vfsStream::create(['nested_dir' => []]);
    

  public function prepareInstallDirectory(&$filetransfer$directory) {
    // Make the parent dir writable if need be and create the dir.     if (!is_dir($directory)) {
      $parent_dir = dirname($directory);
      if (!is_writable($parent_dir)) {
        @chmod($parent_dir, 0755);
        // It is expected that this will fail if the directory is owned by the         // FTP user. If the FTP user == web server, it will succeed.         try {
          $filetransfer->createDirectory($directory);
          $this->makeWorldReadable($filetransfer$directory);
        }
        catch (FileTransferException $e) {
          // Probably still not writable. Try to chmod and do it again.           // @todo Make a new exception class so we can catch it differently.           try {
            $old_perms = fileperms($parent_dir) & 0777;
            
    $this->assertStringContainsString('Congratulations, you installed Drupal!', $process->getErrorOutput());
    // Ensure the command does not trigger any PHP deprecations.     $this->assertStringNotContainsString('Deprecated', $process->getErrorOutput());
    $this->assertNotFalse($port, "Web server running on port $port");

    // Give the server a couple of seconds to be ready.     sleep(2);
    $this->assertStringContainsString("127.0.0.1:$port/user/reset/1/", $process->getOutput());

    // Generate a cookie so we can make a request against the installed site.     define('DRUPAL_TEST_IN_CHILD_SITE', FALSE);
    chmod($this->testDb->getTestSitePath(), 0755);
    $cookieJar = CookieJar::fromArray([
      'SIMPLETEST_USER_AGENT' => drupal_generate_test_ua($this->testDb->getDatabasePrefix()),
    ], '127.0.0.1');

    $response = $guzzle->get('http://127.0.0.1:' . $port['cookies' => $cookieJar]);
    $content = (string) $response->getBody();
    $this->assertStringContainsString('Test site ' . $this->testDb->getDatabasePrefix()$content);

    // Stop the web server.     $process->stop();
  }

  
__( 'https://wordpress.org/documentation/article/changing-file-permissions/' )
                    );
                } else {
                    $error_message = sprintf(
                        /* translators: %s: wp-config.php */
                        __( 'Unable to write to %s file.' ),
                        '<code>wp-config.php</code>'
                    );
                }
            }

            chmod( $path_to_wp_config, 0666 );
            setup_config_display_header();

            if ( false !== $handle ) :
                ?> <h1 class="screen-reader-text"> <?php                 /* translators: Hidden accessibility text. */
                _e( 'Successful database connection' );
                ?> </h1> <p><?php _e( 'All right, sparky! You&#8217;ve made it through this part of the installation. WordPress can now communicate with your database. If you are ready, time now to&hellip;' ); ?></p> <p class="step"><a href="
'inline_class_loader' => $inlineClassLoader,
            'build_time' => $container->hasParameter('kernel.container_build_time') ? $container->getParameter('kernel.container_build_time') : time(),
            'preload_classes' => array_map('get_class', $this->bundles),
        ]);

        $rootCode = array_pop($content);
        $dir = \dirname($cache->getPath()).'/';
        $fs = new Filesystem();

        foreach ($content as $file => $code) {
            $fs->dumpFile($dir.$file$code);
            @chmod($dir.$file, 0666 & ~umask());
        }
        $legacyFile = \dirname($dir.key($content)).'.legacy';
        if (is_file($legacyFile)) {
            @unlink($legacyFile);
        }

        $cache->write($rootCode$container->getResources());
    }

    /** * Returns a loader for the container. */
    if (!$this->isValid()) {
      return FALSE;
    }

    $destination = $destination ?: $this->getSource();
    if ($return = $this->getToolkit()->save($destination)) {
      // Clear the cached file size and refresh the image information.       clearstatcache(TRUE, $destination);
      $this->fileSize = filesize($destination);
      $this->source = $destination;

      if (\Drupal::service('file_system')->chmod($destination)) {
        return $return;
      }
    }
    return FALSE;
  }

  /** * {@inheritdoc} */
  public function apply($operation, array $arguments = []) {
    return $this->getToolkit()->apply($operation$arguments);
  }

    public function changePermissions()
    {
        foreach ($this->filePermissions as $filePermission) {
            if (\array_key_exists('filePath', $filePermission)
                && \array_key_exists('chmod', $filePermission)
                && is_writable($filePermission['filePath'])) {
                // If the owner of a file is not the user of the currently running process, "is_writable" might return true                 // while "chmod" below fails. So we suppress any errors in that case.
                try {
                    @chmod($filePermission['filePath']$filePermission['chmod']);
                } catch (Exception $e) {
                    // Don't block the update process                 } catch (Throwable $e) {
                    // Don't block the update process                 }
            }
        }
    }
}

        $mime = $this->getMimeTypeByFile($filepath);

        $optimizer = $this->getOptimizerByMimeType($mime);

        // Reading and resetting the permissions on the file since some optimizer are unable to do so themselves.         $perms = fileperms($filepath);
        if (!\is_int($perms)) {
            $perms = 0644;
        }
        $optimizer->run($filepath);
        chmod($filepath$perms);
    }

    /** * {@inheritdoc} */
    public function getOptimizers()
    {
        return $this->optimizers;
    }

    /** * {@inheritdoc} */
        $privateKeyDirectory = \dirname($privateKeyPath);
        if (!\is_dir($privateKeyDirectory)) {
            \mkdir($privateKeyDirectory, 0755, true);
        }

        $publicKeyDirectory = \dirname($publicKeyPath);
        if (!\is_dir($publicKeyDirectory)) {
            \mkdir($publicKeyDirectory, 0755, true);
        }

        \file_put_contents($privateKeyPath$private);
        \chmod($privateKeyPath, 0660);

        \file_put_contents($publicKeyPath$public);
        \chmod($publicKeyPath, 0660);
    }
}
Home | Imprint | This part of the site doesn't use cookies.