Hàm 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. Show Nội dung chính
Nội dung chính
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ápCú pháp: 4Trong đó::
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 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:
- Trong đó:
- Đường dẫn đến tập tin được sao chép thành.
picturesmyCode.php
mobilemyCode.php được thực thi, cây thư mục sẽ trở thành:
picturescopy() được thực thi, nó sẽ trả về giá trị:
- 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"
Hàm 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ápCú pháp: 4Trong đó::
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 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
Tổng quan về kỹ thuật Upload file trong PHPYê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ử 9 với thuộc tính 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.
- Đ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 fileflower.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à 9 với kiểu 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à 9 với kiểu 0. Tên phần tử là $file = 'test.txt'; $newfile = 'example.txt'; copy($file, $newfile);8 Code PHP xử lý Upload FileCode 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 0Từ 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 FileTrong 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: 1Lú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
Đ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 2Source 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 |