Monday, May 25, 2015

Contoh penggunaan facade file::

1. Semak fail wujud :
 
if (File::exists($myfile))
{
    echo "Yup. It exists.";
}
 
2. Dapatkan kandungan Fail :

$contents = File::get($filename);
 
atau 
 
try
{
    $contents = File::get($filename);
}
catch (Illuminate\Filesystem\FileNotFoundException $exception)
{
    die("The file doesn't exist");
} 

3. Dapatkan kandungan Fail Remote :

$contents = File::getRemote($url);
 
atau
 
$contents = File::getRemote($url);
if ($contents === false)
{
    die("Couldn't fetch the file.");
} 

4. Dapatakn nilai yang dikembalikan oleh fail :

<?php
// file1.php - returns an array
return array(
    'key1' => 'value1',
    'key2' => 'value2',
);
?>

// Fetching the array of the file above
$value = File::getRequire('file1.php');
 

5. require_once :

File::requireOnce($some_php_file)
 
6. Tulis kandungan fail, php file_put_contents():

$bytes_written = File::put($file, $contents);
if ($bytes_written === false)
{
    die("Error writing to file");
}


7. Tulis ke permulaan Fail :
 
$bytesWritten = File::prepend($filename, $content);
if ($bytesWritten === false)
{
    die("Couldn't write to the file.");
}

8. Tambah di akhir fail :

$bytesWritten = File::append($filename, $content);
if ($bytesWritten === false)
{
    die("Couldn't write to the file.");
}

9. Deleting fail :

// Delete a single file
File::delete($filename);

// Delete multiple files
File::delete($file1, $file2, $file3);

// Delete an array of files
$files = array($file1, $file2);
File::delete($files);
 

10. Pindah fail , php rename() :

if ( ! File::move($oldfile, $newfile))
{
    die("Couldn't rename file");
}

11. Salin Fail, php copy():

if ( ! File::copy($file, $dest))
{
    die("Couldn't copy file");
}
 
 
12. Dapatkan Extension fail :

$extension = File::extension($filename);

13. Dapatkan jenis fail :

echo File::type($filename);


14. Dapatkan saiz fail :

$bytes = File::size($filename);


15. Dapatkan tarikh ubah :

$timestamp = File::lastModified($filename);
if ($timestamp === false)
{
    die("Failure getting the time");
}

16. Semak samada  Path adalah Directory  php is_dir():

if (File::isDirectory($filename))
{
    echo "Yes. It's a directory.";
}

17. Semak fail atau Directory boleh ditulis, php is_writable() :

if (File::isWritable($filename))
{
    echo "Yes. $filename is writable.";
}
if (File::isWritable($dirname))
{
    echo "Yes. $dirname is writable.";
}

18. Semak  Path adalah fail, php is_file() :

if (File::isFile($filename))
{
    echo "Yep. It's a file.";
}


18. Cari fail yang sama paten, php glob() :

$log_files = File::glob('/test/*.log');
if ($log_files === false)
{
    die("An error occurred.");
}
 
atau 
 
$dir_list = File::glob('/test/*', GLOB_ONLYDIR);
if ($dir_files === false)
{
    die("An error occurred.");
}
 
Flag yang boleh guna :
 
  • GLOB_MARK - Adds a slash to each directory returned
  • GLOB_NOSORT - Return files as they appear in the directory (no sorting)
  • GLOB_NOCHECK - Return the search pattern if no files matching it were found
  • GLOB_NOESCAPE - Backslashes do not quote meta-characters
  • GLOB_BRACE - Expands {a,b,c} to match 'a', 'b', or 'c'
  • GLOB_ONLYDIR - Return only directory entries which match the pattern
  • GLOB_ERR - Stop on read errors (like unreadable directories), by default errors are ignored.

  

19. Dapatkan senarai fail dalam directory :

$files = File::files();


20. Dapatakn senarai fail rekusiv dalam directori :

$files = File::allFiles($directory);
foreach ($files as $file)
{
    echo (string)$file, "\n";
}


21. Dapatkan semua direktori dalam direktori :

$list = File::directories('/test');
echo "Test contains ", count($list), "directories.";


22. Cipta direktori :

$result = File::makeDirectory('/path/to/directory');
 
atau
 
$result = File::makeDirectory('/path/to/directory', 0775);
 
atau
 
$result = File::makeDirectory('/path/to/directory', 0775, true);
 
atau force
 
$result = File::makeDirectory('/path/to/directory', 0775, true, true);   


23. Salin Directory :

$success = File::copyDirectory($sourceDir, $destinationDir);


23. Buang Recursively Directory :

$success = File::deleteDirectory($directory);
 
atau tinggal direktori teratas :
 
$success = File::deleteDirectory($directory, true); 

24. Kosongkan Directory :

$success = File::cleanDirectory($directory);
 
guna ini juga : File::deleteDirectory() 

No comments:

Post a Comment