2015년 1월 30일 금요일

PHP 파일 시스템 함수 정리


// http://www.gnu.org/licenses/gpl-3.0.txt
$filename = "test.txt";

/*
fopen
resource_handle = fopen(string_filename, string_mode)
http://php.net/manual/en/function.fopen.php
- mode
r  : read, pointer Begin
r+ : read, write, pointer Begin
w  : write, erase, create new file, pointer End 
w+ : write, read, pointer End
a  : add, write, create new file, pointer End
a+ : add, write, read, pointer End
x  : creat new file, write, pointer Begin
x+ : read, write, pointer Begin
c  : write, pointer Begin
c+ : read, write, pointer Begin
*/
$handle = fopen($filename,"r"); // chmod 777 test, "D:\\test\\test.txt"
if ($handle)
{
 echo "file Opened.";
}
else
{
 die("open Failed.");
}

/*
fread
string = fread(resource_handle, int_length)
http://php.net/manual/en/function.fread.php
*/
$handle = fopen($filename,"r");
$temp = fread($handle, 100);
echo $temp;

/*
fwrite, fputs
int = fwrite(resource_handle, string, int)
int = fputs(resource_handle, string, int)
http://php.net/manual/en/function.fwrite.php
http://php.net/manual/en/function.fputs.php
*/
$handle = fopen($filename,"w");
$temp = fwrite($handle, "ABCDEFGabcあいうえお가나다라마바사");
echo $temp;
$temp = fputs($handle, "abcdefg", 3);
echo $temp;
/*
fwrite, fputs
int = fwrite(resource_handle, string, int)
int = fputs(resource_handle, string, int)
http://php.net/manual/en/function.fwrite.php
http://php.net/manual/en/function.fputs.php
*/
$handle = fopen($filename,"w");
$temp = fwrite($handle, "ABCDEFGabcあいうえお가나다라마바사");
echo $temp;
$temp = fputs($handle, "abcdefg", 3);
echo $temp;

/*
feof
bool = feof(resource_handle, int_length)
http://php.net/manual/en/function.feof.php
*/
$temp = "";
$handle = fopen($filename,"r");
while (!feof($handle))
{
 $temp .= fread($handle, 128);
}

/*
fgets
string = fgets(resource_handle, int_length)
http://php.net/manual/en/function.fgets.php
*/
while (($buffer = fgets($handle, 4096)) !== false) {
 echo $buffer;
}
if (!feof($handle)) {
 echo "Error: unexpected fgets() fail\n";
}

/*
fpassthru
int = fpassthru(resource_handle)
http://php.net/manual/en/function.fpassthru.php
*/
if (!feof($handle)) {
 header("Content-Type: text/html");
 header("Content-Length: " . filesize($file));
 fpassthru($handle);
 exit;
}

/*
readfile
int = readfile(string_filename)
http://php.net/manual/en/function.readfile.php
*/
if (file_exists($filename)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($filename));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($filename));
    readfile($filename);
    exit;
}

/*
fgetc
string = fgetc(resource_handle)
http://php.net/manual/en/function.fgetc.php
*/
while (!feof($handle)) {
 $char = fgetc($handle);
 echo $char . ", ";
}

/*
fgetss
string = fgetss(resource_handle)
http://php.net/manual/en/function.fgetss.php
*/
while (!feof($handle)) {
 $strip_tags = fgetss($handle);
 echo $strip_tags;
}

/*
fgetcsv
array = fgetcsv(resource_handle)
http://php.net/manual/en/function.fgetcsv.php
*/
$handle = fopen("test.csv","r");
if ($handle)
{
 while($col = fgetcsv($handle,1024,","))
 {
  $row = count($col);
  //print_r($col);
  foreach ($col as $key=>$val) {
   echo $col[$key] . ",";
  }
  echo "
"; } echo "Total = " . $row; } else { die("open Failed."); } /* file array = file(string_filename) http://php.net/manual/kr/function.file.php */ $array = file("test.txt"); for ($i = 0; $i < count($array); $i++) { echo $array[$i]; } $lines = file("http://php.net/"); while (list($line_num,$line)=each($lines)) { echo "Line" . $line_num . " : " . htmlspecialchars($line) . "
"; } /* filesize int = filesize(string_filename) http://php.net/manual/kr/function.filesize.php */ $size = filesize("test.txt"); echo $size . " Bytes"; /* file_exists bool = file_exists(string_filename) http://php.net/manual/kr/function.file-exists.php */ if (file_exists($filename)) { echo "The file $filename exists"; } else { echo "The file $filename does not exist"; } /* is_executable bool = is_executable(string_filename) http://php.net/manual/kr/function.is-executable.php */ if (is_executable($filename)) { echo $filename.' is executable'; } else { echo $filename.' is not executable'; } /* is_writable bool = is_writable(string_filename) http://php.net/manual/kr/function.is-writable.php */ if (is_writable($filename)) { echo $filename.' is writable'; } else { echo $filename.' is not writable'; } /* copy bool = copy(string_source, string_dest) http://php.net/manual/kr/function.copy.php */ $file = "test.txt"; $newfile = "test.bak"; if (!copy($file, $newfile)){ echo "failed to copy $file...\n"; } /* rename bool = rename(string_oldname, string_newname) http://php.net/manual/kr/function.rename.php */ rename("test.txt", "test.new.txt"); /* unlink bool = unlink(string_filename) http://php.net/manual/kr/function.unlink.php */ unlink("test.csv"); /* mkdir bool = mkdir(string_pathname, int_mode) http://php.net/manual/kr/function.mkdir.php */ $structure = "depth1/depth2/depth3/"; if (!mkdir($structure, 0777, true)) { die("Failed to create folders..."); } /* rmdir bool = rmdir(string_dirname) http://php.net/manual/kr/function.rmdir.php */ if (!is_dir("FolderName")) { mkdir("FolderName"); } rmdir("FolderName"); /* basename string = basename(string_path, string_suffix) http://php.net/manual/kr/function.basename.php */ echo "1) ".basename("/etc/sudoers.txt", ".txt").PHP_EOL; echo "2) ".basename("/etc/passwd").PHP_EOL; echo "3) ".basename("/etc/").PHP_EOL; echo "4) ".basename(".").PHP_EOL; echo "5) ".basename("/"); /* dirname string = dirname(string_path) http://php.net/manual/kr/function.dirname.php */ echo "1) " . dirname("/etc/passwd/test.txt") . PHP_EOL; // 1) /etc/passwd echo "2) " . dirname("/etc/passwd") . PHP_EOL; // 1) /etc echo "3) " . dirname("/etc/") . PHP_EOL; // 2) / (or \ on Windows) echo "4) " . dirname("."); // 3) . /* pathinfo array = pathinfo(string_path) http://php.net/manual/kr/function.pathinfo.php */ $path_parts = pathinfo('/www/htdocs/inc/lib.inc.php'); echo $path_parts['dirname'], "
"; // folder name echo $path_parts['basename'], "
"; // file + extension name echo $path_parts['extension'], "
"; // extension name echo $path_parts['filename'], "
"; // file name /* rewind bool = rewind(resource_handle) http://php.net/manual/kr/function.rewind.php */ $handle = fopen('test.txt', 'r+'); fwrite($handle, 'Really long sentence.'); rewind($handle); fwrite($handle, 'Foo'); rewind($handle); echo fread($handle, filesize('test.txt')); rewind($handle); echo fgets($handle); /* fseek int = fseek(resource_handle, int_offset) http://php.net/manual/kr/function.fseek.php */ $handle = fopen('test.txt', 'r'); while (!feof($handle)) { echo fgets($handle, 4096); } rewind($handle); echo fgets($handle); // move next 10 byte fseek($handle, 10, SEEK_SET); echo fgets($handle); // from now point. move next 5 byte fseek($handle, 5, SEEK_CUR); echo fgets($handle); // from end point. move prev 10 byte fseek($handle, -10, SEEK_END); echo fgets($handle); /* ftell int = ftell(resource_handle) http://php.net/manual/kr/function.ftell.php */ $handle = fopen($filename, "r"); echo ftell($handle); // 0 echo fgets($handle,7); echo ftell($handle); // 6 /* parse_ini_file array = parse_ini_file(string_filename) http://php.net/manual/kr/function.parse-ini-file.php */ define('BIRD', 'Dodo bird'); // Parse without sections $ini_array = parse_ini_file("test.ini"); print_r($ini_array); // Parse with sections $ini_array = parse_ini_file("test.ini", true); print_r($ini_array); /* is_uploaded_file bool = is_uploaded_file(string_filename) http://php.net/manual/kr/function.is-uploaded-file.php */
if (is_uploaded_file($_FILES['uploadedfile']['tmp_name'])) { echo "File ". $_FILES['uploadedfile']['name'] ." uploaded successfully.\n"; echo "Displaying contents\n"; readfile($_FILES['uploadedfile']['tmp_name']); } else { echo "Possible file upload attack: "; echo "filename '". $_FILES['uploadedfile']['tmp_name'] . "'."; } /* move_uploaded_file bool = move_uploaded_file(string_filename, string_destination) http://php.net/manual/kr/function.move-uploaded-file.php */ $uploads_dir = 'iglu'; if (is_uploaded_file($_FILES["uploadedfile"]["tmp_name"])) { //print_r($_FILES); echo "file name : " . $_FILES["uploadedfile"]["name"]; echo "file size : " . $_FILES["uploadedfile"]["size"]; echo "file type : " . $_FILES["uploadedfile"]["type"]; echo "temp name : " . $_FILES["uploadedfile"]["tmp_name"]; //save path $dest = $uploads_dir . "/" . $_FILES["uploadedfile"]["name"]; //save file if (move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], $dest)) { echo "MOVED SUCCESS!"; } else { die("MOVED Failed!"); } } else { die("NO FILE"); } /* fclose bool = fclose(resource_handle) http://php.net/manual/en/function.fopen.php */ $tf = fclose($handle); if($tf) { echo "
file Closed."; } else { echo "
close Failed."; }

댓글 없음:

댓글 쓰기

플러터 단축키

1. 위젯 감싸기/벗기기 비주얼 스튜디오 :   Cmd + . 안드로이드 스튜디오 : Alt + Enter 2. 코드 정렬 비주얼 스튜디오 : Ctrl + S 안드로이드 스튜디오 : Ctlr + Alt + L 3. StatelessWidget ->...