Hướng dẫn php upload file check if exists - kiểm tra tệp tải lên php nếu tồn tại

Tôi có biểu mẫu ở Frontend để gửi nội dung, trong đó tôi cần kiểm tra xem tệp có tồn tại khi gửi không.

Xin lưu ý rằng biểu mẫu gửi mảng các tệp. Nó đang thất bại trong tất cả các logic tôi đã thử. Vui lòng chú ý upload[] trên dòng thứ hai của HTML.array of files. It is failing the all logics I have been trying. Please notice the upload[] on second line of html.

HTML

PHP

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }

Mã trên hiển thị 'file is not set !' trong cả hai trường hợp, đó là khi tôi nhấn nút Gửi mà không tải lên tệp và khi tôi chọn tải lên tệp và nhấn nút gửi.

Điều gì đang xảy ra ở đây, tôi thậm chí còn làm điều đó đúng không?

Về cơ bản, tôi muốn trả lại tin nhắn 'Tệp trống' khi ai đó nhấn nút gửi mà không cần chọn tệp để tải lên và dừng thực thi mã tiếp theo.

(Php 4, Php 5, Php 7, Php 8)

file_exists - kiểm tra xem một tệp hoặc thư mục có tồn tại khôngChecks whether a file or directory exists

Sự mô tả

File_Exists (Chuỗi $filename): Bool(string $filename): bool

Thông số

filename

Đường dẫn đến tệp hoặc thư mục.

Trên Windows, sử dụng // Computername/Share/FileName hoặc \\ Computername \ Share \ FileName để kiểm tra các tệp trên chia sẻ mạng.

Trả về giá trị

Trả về true Nếu tệp hoặc thư mục được chỉ định bởi filename tồn tại; false nếu không.true if the file or directory specified by filename exists; false otherwise.

Ghi chú::

Hàm này sẽ trả về false cho các liên kết symlink trỏ đến các tệp không tồn tại.false for symlinks pointing to non-existing files.

Ghi chú::

Hàm này sẽ trả về false cho các liên kết symlink trỏ đến các tệp không tồn tại.

Kiểm tra được thực hiện bằng cách sử dụng UID/GID thực sự thay vì hiệu quả.: Because PHP's integer type is signed and many platforms use 32bit integers, some filesystem functions may return unexpected results for files which are larger than 2GB.

Errors/Exceptions

Lưu ý: Vì loại số nguyên của PHP được ký kết và nhiều nền tảng sử dụng số nguyên 32 bit, một số hàm hệ thống tập tin có thể trả về kết quả bất ngờ cho các tệp lớn hơn 2GB.

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
0 is emitted.

Sau khi thất bại, một if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) { $upload_error = 'file is not set !'; return; //stop further code execution }else{ //do further validation } 0 được phát ra.

Ví dụ

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
1

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
2

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
3

Ví dụ số 1 kiểm tra xem một tệp có tồn tại không

Ghi chú: The results of this function are cached. See clearstatcache() for more details.

Lưu ý: Kết quả của chức năng này được lưu trong bộ nhớ cache. Xem ClearStatCache () để biết thêm chi tiết.

  • Xem thêm
  • is_readable () - cho biết liệu một tệp có tồn tại hay không và có thể đọc được không
  • is_writable () - cho biết liệu tên tệp có thể ghi được không
  • is_file () - cho biết liệu tên tệp có phải là tệp thông thường không
  • file () - đọc toàn bộ tệp vào một mảng

Splfileinfo

Maurice tại Idify Dot NL ¶

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
4

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
5

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
6

14 năm trước

ziptwipi tại goioia dot com ¶

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
7

6 năm trước

Welkom tại Remconijhuis dot nl ¶

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
8

8 năm trước

Vernon tại KesnerDesigns Dot Net ¶

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
9

upload[]0

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
6

15 năm trước

Vernon tại KesnerDesigns Dot Net ¶

upload[]2

upload[]3

upload[]4

upload[]5

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
6

15 năm trước

Bvazquez tại Siscomx Dot Com ¶

upload[]7

upload[]8

upload[]9

'file is not set !'0

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
6

jag ¶

3 năm trước

'file is not set !'2

'file is not set !'3

'file is not set !'4

'file is not set !'5

'file is not set !'6

'file is not set !'7

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
6

Vcoletti tại Tiscali Dot it ¶

Vernon tại KesnerDesigns Dot Net ¶

'file is not set !'9

$filename0

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
6

15 năm trước

Bvazquez tại Siscomx Dot Com ¶

$filename2

jag ¶

3 năm trước

$filename3

$filename4

$filename5

$filename6

$filename7

$filename8

$filename9

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
6

Vcoletti tại Tiscali Dot it ¶

Vernon tại KesnerDesigns Dot Net ¶

filename1

filename2

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
6

15 năm trước

Bvazquez tại Siscomx Dot Com ¶

filename4

filename5

filename6

filename7

jag ¶

Vernon tại KesnerDesigns Dot Net ¶

filename8

filename9

true0

true1

15 năm trước

Maurice tại Idify Dot NL ¶

true2

true3

true4

true5

true6

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
6

14 năm trước

ziptwipi tại goioia dot com ¶

true8

true9

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
6

6 năm trước

Welkom tại Remconijhuis dot nl ¶

filename1

filename2

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
6

8 năm trước

Vernon tại KesnerDesigns Dot Net ¶

filename4

filename5

filename6

filename7

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
6

15 năm trước

Welkom tại Remconijhuis dot nl ¶

filename9

false0

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
6

8 năm trước

Maurice tại Idify Dot NL ¶

false2

false3

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
6

14 năm trước

Maurice tại Idify Dot NL ¶

false5

false6

false7

false8

false9

false0

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
6

14 năm trước

Maurice tại Idify Dot NL ¶

false2

false3

false4

false5

14 năm trước

Maurice tại Idify Dot NL ¶

false6

false7

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
6

14 năm trước

ziptwipi tại goioia dot com ¶

false9

6 năm trước

ziptwipi tại goioia dot com ¶

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
00

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
01

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
02

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
03

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
04

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
05

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
06

6 năm trước

ziptwipi tại goioia dot com ¶

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
07

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
08

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
09

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
10

6 năm trước

ziptwipi tại goioia dot com ¶

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
11

6 năm trước

Bvazquez tại Siscomx Dot Com ¶

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
12

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
13

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
14

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
15

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
16

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
17

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
18

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
19

jag ¶

ziptwipi tại goioia dot com ¶

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
20

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
21

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
6

6 năm trước

ziptwipi tại goioia dot com ¶

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
23

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
24

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
25

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }
6

Làm thế nào để bạn kiểm tra xem một tệp đã tồn tại trong PHP?

File_exists kiểm tra xem một tệp có tồn tại trong đường dẫn được chỉ định hay không. Trả về true nếu tệp hoặc thư mục được chỉ định bởi tên tệp tồn tại; Sai nếu không. Một phương thức thay thế khác mà bạn có thể sử dụng getImagesize (), nó sẽ trả về 0 (không) nếu tệp/thư mục không có sẵn trong đường dẫn được chỉ định.. Returns TRUE if the file or directory specified by filename exists; FALSE otherwise. Another alternative method you can use getimagesize(), it will return 0(zero) if file/directory is not available in the specified path.

Làm thế nào tôi có thể kiểm tra xem một tệp được tải lên trong PHP?

Hàm is_uploaded_file () trong PHP là hàm sẵn được sử dụng để kiểm tra xem tệp được chỉ định được tải lên qua bài HTTP hay không.Tên của tệp được gửi dưới dạng tham số cho hàm is_uploaded_file () và nó trả về true nếu tệp được tải lên qua bài đăng http.is_uploaded_file() function in PHP is an inbuilt function which is used to check whether the specified file uploaded via HTTP POST or not. The name of the file is sent as a parameter to the is_uploaded_file() function and it returns True if the file is uploaded via HTTP POST.

Có tồn tại trong PHP không?

Định nghĩa và cách sử dụng.Hàm isset () kiểm tra xem một biến được đặt, điều đó có nghĩa là nó phải được khai báo và không phải là null.Hàm này trả về đúng nếu biến tồn tại và không phải là null, nếu không nó sẽ trả về sai.The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false.

Move_uploaded_file trong PHP là gì?

Định nghĩa và sử dụng hàm Move_uploaded_file () di chuyển một tệp đã tải lên một đích mới.Lưu ý: Chức năng này chỉ hoạt động trên các tệp được tải lên thông qua cơ chế tải lên bài HTTP của PHP.Lưu ý: Nếu tệp đích đã tồn tại, nó sẽ bị ghi đè.moves an uploaded file to a new destination. Note: This function only works on files uploaded via PHP's HTTP POST upload mechanism. Note: If the destination file already exists, it will be overwritten.