Hướng dẫn php website copier - máy photocopy trang web php

Hàm 

copy(file_path_from, file_path_to)
3 sẽ sao chép nội dung của file này và ghi đè lên nội dung của file khác.

Nội dung chính

  • Kết quả trả về
  • Kết quả trả về
  • Tổng quan về kỹ thuật Upload file trong PHP
  • Tạo form HTML upload file
  • Code PHP xử lý Upload File
  • Code PHP Upload nhiều File

Nội dung chính

  • Kết quả trả về
  • Kết quả trả về
  • Tổng quan về kỹ thuật Upload file trong PHP
  • Tạo form HTML upload file
  • Code PHP xử lý Upload File
  • Code PHP Upload nhiều File

Hướng dẫn php website copier - máy photocopy trang web php

Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.freetuts.net, không được copy dưới mọi hình thức.

Cú pháp

Cú pháp: 

copy(file_path_from, file_path_to)
4
copy(file_path_from, file_path_to)
4

Trong đó::

  • copy(file_path_from, file_path_to)
    5 là file cần sao chép nội dung.
  • copy(file_path_from, file_path_to)
    6 là đường dẫn( path) của file sẽ nhận nội dung của
    copy(file_path_from, file_path_to)
    5.
  • copy(file_path_from, file_path_to)
    8 là nội dung được khởi tạo từ hàm 
    copy(file_path_from, file_path_to)
    9.

Kết quả trả về

Hàm sẽ trả về True nếu sao chép thành công và trả về False nếu thất bại.

Bài viết này được đăng tại [free tuts .net]

Ví dụ

Sử dụng hàm

copy(file_path_from, file_path_to)
3 để sao chép nội dung file test.txt:

Code

$file = 'test.txt';
$newfile = 'example.txt';
copy($file, $newfile);

sau khi chạy bạn mở file example.txt sẽ thấy nội dung giống với file test.txt

Tham khảo: php.net

- Hàm copy() dùng để sao chép một tập tin.copy() dùng để sao chép một tập tin.

- Cú pháp:

copy(file_path_from, file_path_to)

- Trong đó:

Tham sốYêu cầuMô tả
file_path_from Bắt buộc

- Đường dẫn đến tập tin mà bạn muốn sao chép.

file_path_to Bắt buộc

- Đường dẫn đến tập tin mà bạn muốn sao chép.

file_path_to

- Đường dẫn đến tập tin được sao chép thành.

  • myCode.php
  • - Lưu ý: Nếu đường dẫn đến tập tin được sao chép thành đã tồn tại một tập tin có cùng tên thì tập tin đó sẽ bị ghi đè.
    • flower.png
  • - Chúng ta có một cây thư mục như sau:

picturesmyCode.php

<?php
    copy("pictures/flower.png", "banana.png");
    copy("pictures/flower.png", "pictures/apple.jpg");
    copy("pictures/flower.png", "mobile/nokia.jpg");
?>

mobilemyCode.php được thực thi, cây thư mục sẽ trở thành:

  • myCode.php
  • banana.png
  • - Lưu ý: Nếu đường dẫn đến tập tin được sao chép thành đã tồn tại một tập tin có cùng tên thì tập tin đó sẽ bị ghi đè.
    • - Chúng ta có một cây thư mục như sau:
    • apple.jpg
  • - Chúng ta có một cây thư mục như sau:
    • nokia.png

picturescopy() được thực thi, nó sẽ trả về giá trị:

  • mobile
  • - Đoạn mã phía dưới là nội dung của tập tin myCode.php
  • - Sau khi tập tin myCode.php được thực thi, cây thư mục sẽ trở thành:

flower.png flower.png thành công thì màn hình sẽ hiển thị thông báo "SAO CHÉP THÀNH CÔNG" và ngược lại sẽ hiển thị thông báo "SAO CHÉP THẤT BẠI"


<?php
    if(copy("pictures/flower.png", "pictures/nokia.png")){
        echo "SAP CHÉP THÀNH CÔNG";
    }else{
        echo "SAO CHÉP THẤT BẠI";     
    }
?>

Hàm 

copy(file_path_from, file_path_to)
3 sẽ sao chép nội dung của file này và ghi đè lên nội dung của file khác.

Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.freetuts.net, không được copy dưới mọi hình thức.

Cú pháp

Cú pháp: 

copy(file_path_from, file_path_to)
4
copy(file_path_from, file_path_to)
4

Trong đó::

  • copy(file_path_from, file_path_to)
    5 là file cần sao chép nội dung.
  • copy(file_path_from, file_path_to)
    6 là đường dẫn( path) của file sẽ nhận nội dung của
    copy(file_path_from, file_path_to)
    5.
  • copy(file_path_from, file_path_to)
    8 là nội dung được khởi tạo từ hàm 
    copy(file_path_from, file_path_to)
    9.

Kết quả trả về

Hàm sẽ trả về True nếu sao chép thành công và trả về False nếu thất bại.

Bài viết này được đăng tại [free tuts .net]

Ví dụ

Sử dụng hàm

copy(file_path_from, file_path_to)
3 để sao chép nội dung file test.txt:

Code

$file = 'test.txt';
$newfile = 'example.txt';
copy($file, $newfile);

sau khi chạy bạn mở file example.txt sẽ thấy nội dung giống với file test.txt

Tham khảo: php.net

  • - Hàm copy() dùng để sao chép một tập tin.
  • - Cú pháp:
  • - Trong đó:
  • Tham số

Tổng quan về kỹ thuật Upload file trong PHP

Yêu cầuenctype="multipart/form-data", phần tử trong form để chọn và gửi file có dạng (tức là dùng phần tử

<?php
    copy("pictures/flower.png", "banana.png");
    copy("pictures/flower.png", "pictures/apple.jpg");
    copy("pictures/flower.png", "mobile/nokia.jpg");
?>
9 với thuộc tính
<?php
    if(copy("pictures/flower.png", "pictures/nokia.png")){
        echo "SAP CHÉP THÀNH CÔNG";
    }else{
        echo "SAO CHÉP THẤT BẠI";     
    }
?>
0). Cấu trúc tóm tắt là:

<form enctype="multipart/form-data" method="POST">
    <input type="file" name="myfile" />
</form>

Mô tả

file_path_from

Bắt buộc

array (size=1)
    'myfile' =>
        array (size=5)
            'name' => string 'somefile.txt' (length=12)
            'type' => string 'text/plain' (length=10)
            'tmp_name' => string '/tmp/phpDC66.tmp' (length=16)
            'error' => int 0
            'size' => int 18

- Đường dẫn đến tập tin mà bạn muốn sao chép.

  • file_path_to : chỉ số mảng tương ứng với tên phần tử input, upload file.
  • - Đường dẫn đến tập tin được sao chép thành. : tên gốc (ban đầu) của file.
  • - Lưu ý: Nếu đường dẫn đến tập tin được sao chép thành đã tồn tại một tập tin có cùng tên thì tập tin đó sẽ bị ghi đè. : kiểu file (tùy phần mở rộng có thể là
    <?php
        if(copy("pictures/flower.png", "pictures/nokia.png")){
            echo "SAP CHÉP THÀNH CÔNG";
        }else{
            echo "SAO CHÉP THẤT BẠI";     
        }
    ?>
    4,
    <?php
        if(copy("pictures/flower.png", "pictures/nokia.png")){
            echo "SAP CHÉP THÀNH CÔNG";
        }else{
            echo "SAO CHÉP THẤT BẠI";     
        }
    ?>
    5,
    <?php
        if(copy("pictures/flower.png", "pictures/nokia.png")){
            echo "SAP CHÉP THÀNH CÔNG";
        }else{
            echo "SAO CHÉP THẤT BẠI";     
        }
    ?>
    6 ...)
  • - Chúng ta có một cây thư mục như sau: : nơi lưu tạm file upload lên, nếu muốn di chuyển nó ra khỏi thư mục tạm dùng hàm
    <?php
        if(copy("pictures/flower.png", "pictures/nokia.png")){
            echo "SAP CHÉP THÀNH CÔNG";
        }else{
            echo "SAO CHÉP THẤT BẠI";     
        }
    ?>
    7.
  • pictures : mã lỗi, nếu mã này bằng 0 là không lỗi.
  • mobile : cỡ file (byte).

- Đoạn mã phía dưới là nội dung của tập tin myCode.php

- Sau khi tập tin myCode.php được thực thi, cây thư mục sẽ trở thành:

Tạo form HTML upload file

flower.png

Có thể tạo file

$file = 'test.txt';
$newfile = 'example.txt';
copy($file, $newfile);
3

<form action="/upload.php" method="post" enctype="multipart/form-data">
    Chọn file để upload:
    <input type="file" name="fileupload" id="fileupload">
    <input type="submit" value="Đăng ảnh" name="submit">
</form>

Hiện thị

$file = 'test.txt';
$newfile = 'example.txt';
copy($file, $newfile);
0 có dạng:

Lưu ý khi tạo form để uplad ảnh (file) thì cần thiết đặt thuộc tính form có:

$file = 'test.txt';
$newfile = 'example.txt';
copy($file, $newfile);
5 Phần tử chọn file trong form là
<?php
    copy("pictures/flower.png", "banana.png");
    copy("pictures/flower.png", "pictures/apple.jpg");
    copy("pictures/flower.png", "mobile/nokia.jpg");
?>
9 với kiểu
<?php
    if(copy("pictures/flower.png", "pictures/nokia.png")){
        echo "SAP CHÉP THÀNH CÔNG";
    }else{
        echo "SAO CHÉP THẤT BẠI";     
    }
?>
0. Tên phần tử là
$file = 'test.txt';
$newfile = 'example.txt';
copy($file, $newfile);
8
$file = 'test.txt';
$newfile = 'example.txt';
copy($file, $newfile);
5
Phần tử chọn file trong form là
<?php
    copy("pictures/flower.png", "banana.png");
    copy("pictures/flower.png", "pictures/apple.jpg");
    copy("pictures/flower.png", "mobile/nokia.jpg");
?>
9 với kiểu
<?php
    if(copy("pictures/flower.png", "pictures/nokia.png")){
        echo "SAP CHÉP THÀNH CÔNG";
    }else{
        echo "SAO CHÉP THẤT BẠI";     
    }
?>
0.
Tên phần tử là
$file = 'test.txt';
$newfile = 'example.txt';
copy($file, $newfile);
8

Code PHP xử lý Upload File

Code xử lý upload sau là xử lý chỉ cho upload các file ảnh, với các loại file là jpg, png, jpeg, gif. File Upload file có cỡ < 0.8 MB. File Upload sẽ lưu vào thư mục

$file = 'test.txt';
$newfile = 'example.txt';
copy($file, $newfile);
9

Code xử lý upload file để trong file

<form enctype="multipart/form-data" method="POST">
    <input type="file" name="myfile" />
</form>
0 có nội dung như sau:

<?php
  
  // file upload.php xử lý upload file

  if ($_SERVER['REQUEST_METHOD'] !== 'POST')
  {
      // Dữ liệu gửi lên server không bằng phương thức post
      echo "Phải Post dữ liệu";
      die;
  }

  // Kiểm tra có dữ liệu fileupload trong $_FILES không
  // Nếu không có thì dừng
  if (!isset($_FILES["fileupload"]))
  {
      echo "Dữ liệu không đúng cấu trúc";
      die;
  }

  // Kiểm tra dữ liệu có bị lỗi không
  if ($_FILES["fileupload"]['error'] != 0)
  {
    echo "Dữ liệu upload bị lỗi";
    die;
  }

  // Đã có dữ liệu upload, thực hiện xử lý file upload

  //Thư mục bạn sẽ lưu file upload
  $target_dir    = "uploads/";
  //Vị trí file lưu tạm trong server (file sẽ lưu trong uploads, với tên giống tên ban đầu)
  $target_file   = $target_dir . basename($_FILES["fileupload"]["name"]);

  $allowUpload   = true;

  //Lấy phần mở rộng của file (jpg, png, ...)
  $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

  // Cỡ lớn nhất được upload (bytes)
  $maxfilesize   = 800000;

  ////Những loại file được phép upload
  $allowtypes    = array('jpg', 'png', 'jpeg', 'gif');


  if(isset($_POST["submit"])) {
      //Kiểm tra xem có phải là ảnh bằng hàm getimagesize
      $check = getimagesize($_FILES["fileupload"]["tmp_name"]);
      if($check !== false)
      {
          echo "Đây là file ảnh - " . $check["mime"] . ".";
          $allowUpload = true;
      }
      else
      {
          echo "Không phải file ảnh.";
          $allowUpload = false;
      }
  }

  // Kiểm tra nếu file đã tồn tại thì không cho phép ghi đè
  // Bạn có thể phát triển code để lưu thành một tên file khác
  if (file_exists($target_file))
  {
      echo "Tên file đã tồn tại trên server, không được ghi đè";
      $allowUpload = false;
  }
  // Kiểm tra kích thước file upload cho vượt quá giới hạn cho phép
  if ($_FILES["fileupload"]["size"] > $maxfilesize)
  {
      echo "Không được upload ảnh lớn hơn $maxfilesize (bytes).";
      $allowUpload = false;
  }


  // Kiểm tra kiểu file
  if (!in_array($imageFileType,$allowtypes ))
  {
      echo "Chỉ được upload các định dạng JPG, PNG, JPEG, GIF";
      $allowUpload = false;
  }


  if ($allowUpload)
  {
      // Xử lý di chuyển file tạm ra thư mục cần lưu trữ, dùng hàm move_uploaded_file
      if (move_uploaded_file($_FILES["fileupload"]["tmp_name"], $target_file))
      {
          echo "File ". basename( $_FILES["fileupload"]["name"]).
          " Đã upload thành công.";

          echo "File lưu tại " . $target_file;

      }
      else
      {
          echo "Có lỗi xảy ra khi upload file.";
      }
  }
  else
  {
      echo "Không upload được file, có thể do file lớn, kiểu file không đúng ...";
  }
?>

Giải thích code trên:

Các

<form enctype="multipart/form-data" method="POST">
    <input type="file" name="myfile" />
</form>
1 mà
$file = 'test.txt';
$newfile = 'example.txt';
copy($file, $newfile);
0 HTML upload nên, được PHP lưu ở một thư mục tạm hệ thống, và tất cả các thông tin về file được lưu ở biến mảng
<form enctype="multipart/form-data" method="POST">
    <input type="file" name="myfile" />
</form>
3, cấu trúc biến này như sau:

$_FILE = Array
    (
        [file1] => Array
            (
                [name] => 'test2.txt';//một tên file người dùng upload
                [type] => text/plain  (kiểu nội dung file text)
                [tmp_name] => /tmp/php/php1h4j1 //vị trí lưu file tạm thời trên server.
                [error] => UPLOAD_ERR_OK (= 0 là không lỗi)
                [size] => 123   (kích thước file - bype)
            )

        [file2] => Array
            (
                [name] => test.jpg
                [type] => image/jpeg
                [tmp_name] => /tmp/php/php6hst32
                [error] => UPLOAD_ERR_OK
                [size] => 98174
            )
    )

<form enctype="multipart/form-data" method="POST">
    <input type="file" name="myfile" />
</form>
4 tên key tương ứng với tên phần tử form upload file, ví dụ trên là
$file = 'test.txt';
$newfile = 'example.txt';
copy($file, $newfile);
8

Như vậy khi xủ lý upload file, bạn truy cập vào $_FILE và di chuyển file vừa upload từ

<form enctype="multipart/form-data" method="POST">
    <input type="file" name="myfile" />
</form>
6 sang vị trí mới bạn muốn lưu
<form enctype="multipart/form-data" method="POST">
    <input type="file" name="myfile" />
</form>
7 bằng hàm
<form enctype="multipart/form-data" method="POST">
    <input type="file" name="myfile" />
</form>
8

Kiểm tra file upload có phải là file ảnh (dùng cho trường hợp bạn chỉ cho phép up ảnh) thì đầu tiên gọi

<form enctype="multipart/form-data" method="POST">
    <input type="file" name="myfile" />
</form>
9 để xem kích thước ảnh, nếu hàm phân tích được có nghĩa là ảnh, ngược lại là một loại file khác.

Kiểm tra phần mở rộng của tên file: Lấy phần mở rộng

array (size=1)
    'myfile' =>
        array (size=5)
            'name' => string 'somefile.txt' (length=12)
            'type' => string 'text/plain' (length=10)
            'tmp_name' => string '/tmp/phpDC66.tmp' (length=16)
            'error' => int 0
            'size' => int 18
0 rồi so sánh với các mẫu cho phép.

NHANH CHÓNG CHẠY THỬ CODE TRÊN

Bạn có thể tạo ra một thư mục đặt tên

array (size=1)
    'myfile' =>
        array (size=5)
            'name' => string 'somefile.txt' (length=12)
            'type' => string 'text/plain' (length=10)
            'tmp_name' => string '/tmp/phpDC66.tmp' (length=16)
            'error' => int 0
            'size' => int 18
1, để trong đó 2 file
$file = 'test.txt';
$newfile = 'example.txt';
copy($file, $newfile);
3 và
<form enctype="multipart/form-data" method="POST">
    <input type="file" name="myfile" />
</form>
0 ở trên, trong thư mục này tạo thư mục con
$file = 'test.txt';
$newfile = 'example.txt';
copy($file, $newfile);
9 để lưu file - cấu trúc này như tại đây: uploadfile

Sau đó vào thư mục

array (size=1)
    'myfile' =>
        array (size=5)
            'name' => string 'somefile.txt' (length=12)
            'type' => string 'text/plain' (length=10)
            'tmp_name' => string '/tmp/phpDC66.tmp' (length=16)
            'error' => int 0
            'size' => int 18
1 và gõ lệnh chạy Server HTTP của PHP

copy(file_path_from, file_path_to)
0

Từ trình duyệt truy cập

array (size=1)
    'myfile' =>
        array (size=5)
            'name' => string 'somefile.txt' (length=12)
            'type' => string 'text/plain' (length=10)
            'tmp_name' => string '/tmp/phpDC66.tmp' (length=16)
            'error' => int 0
            'size' => int 18
6 để chạy thử

Code PHP Upload nhiều File

Trong thẻ

array (size=1)
    'myfile' =>
        array (size=5)
            'name' => string 'somefile.txt' (length=12)
            'type' => string 'text/plain' (length=10)
            'tmp_name' => string '/tmp/phpDC66.tmp' (length=16)
            'error' => int 0
            'size' => int 18
7 nếu có thuộc tính
array (size=1)
    'myfile' =>
        array (size=5)
            'name' => string 'somefile.txt' (length=12)
            'type' => string 'text/plain' (length=10)
            'tmp_name' => string '/tmp/phpDC66.tmp' (length=16)
            'error' => int 0
            'size' => int 18
8 và tên phần tử chỉ ra là mảng, thì cho phép chọn một lúc nhiều file để upload.

Ví dụ HTML như sau:

copy(file_path_from, file_path_to)
1

Lúc này tại PHP

array (size=1)
    'myfile' =>
        array (size=5)
            'name' => string 'somefile.txt' (length=12)
            'type' => string 'text/plain' (length=10)
            'tmp_name' => string '/tmp/phpDC66.tmp' (length=16)
            'error' => int 0
            'size' => int 18
9 mỗi thành phần sau đều là mảng chứa số lượng tương ứng của file upload

  • <form action="/upload.php" method="post" enctype="multipart/form-data">
        Chọn file để upload:
        <input type="file" name="fileupload" id="fileupload">
        <input type="submit" value="Đăng ảnh" name="submit">
    </form>
    0 chứa các tên file upload (up 3 file thì là mảng 3 phần tử các tên)
  • <form action="/upload.php" method="post" enctype="multipart/form-data">
        Chọn file để upload:
        <input type="file" name="fileupload" id="fileupload">
        <input type="submit" value="Đăng ảnh" name="submit">
    </form>
    1 các kiểu file
  • <form action="/upload.php" method="post" enctype="multipart/form-data">
        Chọn file để upload:
        <input type="file" name="fileupload" id="fileupload">
        <input type="submit" value="Đăng ảnh" name="submit">
    </form>
    2 chứa các vị trí lưu tạm file
  • <form action="/upload.php" method="post" enctype="multipart/form-data">
        Chọn file để upload:
        <input type="file" name="fileupload" id="fileupload">
        <input type="submit" value="Đăng ảnh" name="submit">
    </form>
    3 mảng báo lỗi
  • <form action="/upload.php" method="post" enctype="multipart/form-data">
        Chọn file để upload:
        <input type="file" name="fileupload" id="fileupload">
        <input type="submit" value="Đăng ảnh" name="submit">
    </form>
    4 các kích thước file

Đoạn code sau hiện thị FORM để bạn chọn một lúc nhiều file upload, sau đó code PHP sẽ hiện thị các thông tin về các file mà bạn upload lên. Hãy chạy và chọn thử nhiều file upload để kiểm tra

File

<form action="/upload.php" method="post" enctype="multipart/form-data">
    Chọn file để upload:
    <input type="file" name="fileupload" id="fileupload">
    <input type="submit" value="Đăng ảnh" name="submit">
</form>
5

copy(file_path_from, file_path_to)
2

Source code: uploadfile (Git), hoặc tải rphp-uploadfile

Chạy thử bằng cách để file này vào thư mục

array (size=1)
    'myfile' =>
        array (size=5)
            'name' => string 'somefile.txt' (length=12)
            'type' => string 'text/plain' (length=10)
            'tmp_name' => string '/tmp/phpDC66.tmp' (length=16)
            'error' => int 0
            'size' => int 18
1 như trên, và truy cập:
<form action="/upload.php" method="post" enctype="multipart/form-data">
    Chọn file để upload:
    <input type="file" name="fileupload" id="fileupload">
    <input type="submit" value="Đăng ảnh" name="submit">
</form>
7 để kiểm tra