Hướng dẫn php file upload doesn t work - tải lên tệp php không hoạt động

Tôi đã sao chép mã từ trang này: //www.w3schools.com/php/php_file_upload.asp

Vấn đề là tôi không gặp lỗi khi tải lên tệp, thay vào đó, điều này hiển thị điều nào là chính xác:

Tải lên: hình ảnh.jpeg Loại: Hình ảnh/kích thước JPEG: 5.8603515625 KB Tệp tạm thời:/TMP/PHPZ67YXK được lưu trữ trong: tải lên/hình ảnh.jpeg

Nhưng không có tệp nào được lưu trên máy chủ.

Tôi không biết điều gì là sai nhưng tôi đang nghĩ về sự cho phép, vẫn có một thư mục có tên tải lên với 777 quyền.

Các PHP-Files này được lưu trữ trên một máy chủ web trực tuyến vì vậy tôi không chạy địa phương này.

HTML-Form


Filename:

upload_file.php


Hướng dẫn khắc phục sự cố này & NBSP; sẽ giúp bạn tìm ra lý do tại sao mẫu tải lên PHP của bạn không hoạt động. Trong một số trường hợp, bạn có thể thấy rằng mảng $ _Files hoàn toàn trống, mặc dù thực tế là & nbsp; một tệp đã được chọn và tải lên.$_FILES array is completely empty, despite the fact that a file has been selected and uploaded.

Tải lên tập lệnh gỡ lỗi.

Nếu bạn đang tìm kiếm một câu trả lời nhanh chóng và dễ dàng, hãy thử đặt mã sau ở đầu tập lệnh tải lên của bạn:

/**
 * Check to see if POST was used and that its content length is less than the value in post_max_size
 */
$requestType = $_SERVER['REQUEST_METHOD'];
if[$requestType != 'POST']{
    echo 'Request type was ' . $requestType . ' - File uploads will only work with POST. Are you using method="post" in your form element?
'; exit; } else{ if[isset[$_SERVER['CONTENT_TYPE']]]{ $contentType = strtolower[$_SERVER['CONTENT_TYPE']]; if[!stristr[$contentType, 'multipart/form-data']]{ echo 'Could not find multipart/form-data in Content-Type. Did you use enctype="multipart/form-data" in your form element?
'; } } if[isset[$_SERVER['CONTENT_LENGTH']]]{ $postSize = $_SERVER['CONTENT_LENGTH']; $maxPostSize = ini_get['post_max_size']; if[$maxPostSize == 0]{ echo 'post_max_size is set to 0 - unlimited.
'; } else{ if[strlen[$maxPostSize] > 1]{ $lastChar = substr[$maxPostSize, -1]; $maxPostSize = substr[$maxPostSize, 0, -1]; if[$lastChar == 'G']{ $maxPostSize = $maxPostSize * 1024 * 1024 * 1024; } if[$lastChar == 'M']{ $maxPostSize = $maxPostSize * 1024 * 1024; } if[$lastChar == 'K']{ $maxPostSize = $maxPostSize * 1024; } if[$postSize > $maxPostSize]{ echo 'The size of the POST request [' . $postSize . ' bytes] exceeded the limit of post_max_size [' . $maxPostSize . ' bytes] in your php.ini file
'; exit; } } else{ if[$postSize > $maxPostSize]{ echo 'The size of the POST request [' . $postSize . ' bytes] exceeded the limit of post_max_size [' . $maxPostSize . ' bytes] in your php.ini file
'; exit; } } } } else{ echo 'CONTENT_LENGTH not found. Make sure that your POST request is smaller than the ' . ini_get['post_max_size'] . ' post_max_size in php.ini
'; } } $tempFolder = ini_get['upload_tmp_dir']; if[strlen[trim[$tempFolder]] == 0]{ echo 'upload_tmp_dir was blank. No temporary upload directory has been set in php.ini
'; exit; } else{ echo 'upload_tmp_dir is set to: ' . $tempFolder . '
'; } if[!is_dir[$tempFolder]]{ echo 'The temp upload directory specified in upload_tmp_dir does not exist: ' . $tempFolder . '
'; exit; } else{ echo $tempFolder . ' is a valid directory
'; } if[!is_writable[$tempFolder]]{ echo 'The temp upload directory specified in upload_tmp_dir is not writeable: ' . $tempFolder . '
'; echo 'Does PHP have permission to write to this directory?
'; exit; } else{ echo $tempFolder . ' is writeable
'; } $write = file_put_contents[$tempFolder . '/' . uniqid[]. '.tmp', 'test']; if[$write === false]{ echo 'PHP could not create a file in ' . $tempFolder . '
'; exit; } else{ echo 'PHP successfully created a test file in: ' . $tempFolder . '
'; } if[ini_get['file_uploads'] == 1]{ echo 'The file_uploads directive in php.ini is set to 1, which means that your PHP configuration allows file uploads
'; } else{ echo 'The file_uploads directive in php.ini has been set to 0 - Uploads are disabled on this PHP configuration.
'; exit; } if[empty[$_FILES]]{ echo 'The $_FILES array is empty. Is your form using method="post" and enctype="multipart/form-data"? Did the size of the file exceed the post_max_size in PHP.ini?'; exit; } else{ foreach[$_FILES as $file]{ if[$file['error'] !== 0]{ echo 'There was an error uploading ' . $file['name'] . '
'; switch[$file['error']]{ case 1: echo 'Size exceeds the upload_max_filesize directive in php.ini
'; break; case 2: echo 'Size exceeds the MAX_FILE_SIZE field in your HTML form
'; break; case 3: echo 'File was only partially uploaded
'; break; case 4: echo 'No file was selected by the user
'; break; case 6: echo 'PHP could not find the temporary upload folder
'; break; case 7: echo 'PHP failed to write to disk. Possible permissions issue?
'; break; case 8: echo 'A PHP extension prevented the file from being uploaded.
'; break; default: echo 'An unknown error occured: ' . $file['error'] . '
'; break; } } else{ echo $file['name'] . ' was successfully uploaded to ' . $file['tmp_name'] . '
'; } } }

PHP ở trên sẽ cố gắng phát hiện một số vấn đề phổ biến nhất mà mọi người gặp phải với các hình thức tải lên. Hy vọng, nó sẽ chỉ cho bạn đi đúng hướng!

Enctype

Điều đầu tiên bạn nên làm là đảm bảo rằng bạn đã đặt thuộc tính Enctype chính xác trên phần tử biểu mẫu của bạn. Thuộc tính này về cơ bản cho máy chủ biết cách dữ liệu biểu mẫu đến.enctype attribute on your form element. This attribute basically tells the server how the incoming form data should be encoded. If you forgot to add enctype to your form, no file will be uploaded and the $_FILES array will remain empty:

    Select File: 

Trong trường hợp cụ thể này, chúng tôi đã đặt & nbsp; enctype thành Multipart/form-data, thay vì ứng dụng mặc định/X-www-form-urlencoded. Cài đặt Enctype thành & NBSP;“multipart/form-data” instead of the default “application/x-www-form-urlencoded”. Setting enctype to “multipart/form-data” prevents data from being encoded.

Sử dụng bài đăng, như GET không tải lên hỗ trợ.

Bạn sẽ cần đảm bảo rằng biểu mẫu HTML của bạn đã đặt thuộc tính phương thức thành Post Post, vì các tải lên tệp được hỗ trợ trong các yêu cầu GET [xem mã trên để biết ví dụ về bài đăng trên mạng được đặt làm phương thức biểu mẫu]. Nếu thuộc tính phương thức trong biểu mẫu của bạn không được đặt, thì biểu mẫu sẽ gửi yêu cầu nhận theo mặc định.method attribute to “POST”, as file uploads are supported in GET requests [see the above code for an example of “POST” being set as the form method]. If the method attribute in your form is not set, then the form will send a GET request by default.

Kiểm tra cấu hình PHP File_uploads của bạn.

Trong tệp cấu hình php.ini của bạn, bạn sẽ tìm thấy một chỉ thị có tên File_upLoads. Chỉ thị này chỉ định liệu PHP có cho phép tải lên HTTP hay không. Trong & nbsp; Cài đặt PHP mặc định, điều này được đưa ra giá trị 1 1, được boolean cho true - có nghĩa là tải lên tệp được cho phép theo mặc định. Tuy nhiên, nếu máy chủ web của bạn đã vô hiệu hóa tải lên tệp, thì bạn có thể thấy rằng chỉ thị này đã được đặt thành từ 0 0. Để kiểm tra giá trị của chỉ thị này, bạn chỉ cần kiểm tra tệp php.ini của mình. Nếu bạn không có quyền truy cập vào tệp php.ini, thì bạn có thể sử dụng mã sau:file_uploads. This directive specifies whether PHP should allow HTTP uploads or not. In default PHP installations, this is given the value “1”, which is BOOLEAN for TRUE – meaning file uploads are allowed by default. However, if your web host has disabled file uploads, then you may find that this directive has been set to “0”. To check the value of this directive, you can simply check your PHP.ini file. If you do not have access to the PHP.ini file, then you can use the following code:

Bài Viết Liên Quan

Chủ Đề