上传⽂件,提⽰⽂件不存在的解决⽅法
在做公司的⼀个项⽬中,需要上传⽂件,使⽤的是AjaxUpload JS组件,选择完⽂件后,发送请求到指定接⼝,随即返回服务器上⽂件的相对路径,然后PHP再做其他⼯作(这不是重点)
处理上传的PHP程序,限制其⽂件⼤⼩为20M
php.ini中
post_max_size 20M
upload_max_filesize 20M
memory_limit 256M
client_max_body_size 518M
上传20M以下的⽂件,没有问题,但是,例如上传80M的⽂件,会报错,⽂件不存在,
if ( ! isset($_FILES[$field]))
{
$this->set_error('upload_no_file_selected');
return FALSE;
}
⽹上的解决⽅法是设置 php.ini和f中相应的属性,仍然报错,后来参考了,才到原因
解决⽅法:
php.ini中的post_max_size和upload_max_filesize 的值要⼤些,不能仅限于规定的值,⽐如这个20M,根据情况,这⾥设置200M,再次上传80M的⽂件,根据PHP程序本⾝的 $_FILES['file']['error'] 可得出⽂件⼤⼩超过限制这样的提⽰
1. SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler)
2. {
3. char *boundary, *s=NULL, *boundary_end = NULL, *start_arr=NULL, *array_index=NULL;
4. char *temp_filename=NULL, *lbuf=NULL, *abuf=NULL;
5. int boundary_len=0, total_bytes=0, cancel_upload=0, is_arr_upload=0, array_len=0;
6. int max_file_size=0, skip_upload=0, anonindex=0, is_anonymous;
7. zval *http_post_files=NULL; HashTable *uploaded_files=NULL;
8. #if HAVE_MBSTRING && !defined(COMPILE_DL_MBSTRING)
9. int str_len = 0, num_vars = 0, num_vars_max = 2*10, *len_list = NULL;
10. char **val_list = NULL;
11. #endif
12. zend_bool magic_quotes_gpc;
13. multipart_buffer *mbuff;
14. zval *array_ptr = (zval *) arg;
15. int fd=-1;
16. zend_llist header;
17. void *event_extra_data = NULL;
18. int llen = 0;
19.
20. /**
21. *检查是否超出最⼤上传⽂件⼤⼩,这是重点
22. *从request_global中取出request_info结构体中的content_length元素,这个content_length应该是http 头信息中发给服务器的
23. *如果content_length ⼤于php.ini中设置的post_max_size,就会由sapi_errror触发⼀个warning,这个warning不会被php接受(还有待研究)
24. *通过nginx的log⽇志中也能发现此warning
25. *
26. */
27. if (SG(request_info).content_length > SG(post_max_size)) {
28. sapi_module.sapi_error(E_WARNING, "POST Content-Length of %ld bytes exceeds the limit of %ld bytes", SG(request_info).content_length, SG(post_max_size));
29. return;
30. }
31.
32. //取得上传⽂件的分隔符
33. boundary = strstr(content_type_dup, "boundary");
34. if (!boundary || !(boundary=strchr(boundary, '='))) {
35. sapi_module.sapi_error(E_WARNING, "Missing boundary in multipart/form-data POST data");
36. return;
37. }
38.
39. boundary++;
40. boundary_len = strlen(boundary);
41.
42. if (boundary[0] == '"') {
43. boundary++;
44. boundary_end = strchr(boundary, '"');
45. if (!boundary_end) {
46. sapi_module.sapi_error(E_WARNING, "Invalid boundary in multipart/form-data POST data");
47. return;
48. }
49. } else {
50. /* search for the end of the boundary */
51. boundary_end = strchr(boundary, ',');
52. }
53. if (boundary_end) {
54. boundary_end[0] = '';
55. boundary_len = boundary_end-boundary;
56. }
57.
58. /* Initialize the buffer */
59. if (!(mbuff = multipart_buffer_new(boundary, boundary_len))) {
60. sapi_module.sapi_error(E_WARNING, "Unable to initialize the input buffer");
61. return;
62. }
63.
64. //初始化$_FILE变量
67. ALLOC_HASHTABLE(uploaded_files);
68. zend_hash_init(uploaded_files, 5, NULL, (dtor_func_t) free_estring, 0);
69. SG(rfc1867_uploaded_files) = uploaded_files;
70.
71. ALLOC_ZVAL(http_post_files);
72. array_init(http_post_files);
73. INIT_PZVAL(http_post_files);
74. PG(http_globals)[TRACK_VARS_FILES] = http_post_files; //TRACK_VARS_FILE正是_FILE在php_core_globals.http_globals中的index (注1)
75.
76. #if HAVE_MBSTRING && !defined(COMPILE_DL_MBSTRING)
77. if (php_mb_encoding_translation(TSRMLS_C)) {
78. val_list = (char **)ecalloc(num_vars_max+2, sizeof(char *));
79. len_list = (int *)ecalloc(num_vars_max+2, sizeof(int));
80. }
81. #endif
82. zend_llist_init(&header, sizeof(mime_header_entry), (llist_dtor_func_t) php_free_hdr_entry, 0);
83.
84. if (php_rfc1867_callback != NULL) {
85. multipart_event_start event_start;
86.
87. t_length = SG(request_info).content_length;
88. if (php_rfc1867_callback(MULTIPART_EVENT_START, &event_start, &event_extra_data TSRMLS_CC) == FAILURE) {
89. goto fileupload_done;
90. }
91. }
92.
93. while (!multipart_buffer_eof(mbuff TSRMLS_CC))
94. {
95. char buff[FILLUNIT];
96. char *cd=NULL,*param=NULL,*filename=NULL, *tmp=NULL;
97. size_t blen=0, wlen=0;
98. off_t offset;
99.
100. zend_llist_clean(&header);
101.
102. if (!multipart_buffer_headers(mbuff, &header TSRMLS_CC)) {
103. goto fileupload_done;
104. }
105.
106. if ((cd = php_mime_get_hdr_value(header, "Content-Disposition"))) {
107. char *pair=NULL;
108. int end=0;
109.
110. while (isspace(*cd)) {
111. ++cd;
112. }
113.
114. while (*cd && (pair = php_ap_getword(&cd, ';')))
115. {
116. char *key=NULL, *word = pair;
117.
118. while (isspace(*cd)) {
119. ++cd;
120. }
121.
122. if (strchr(pair, '=')) {
123. key = php_ap_getword(&pair, '=');
124.
125. if (!strcasecmp(key, "name")) {
126. if (param) {
127. efree(param);
128. }
129. param = php_ap_getword_conf(&pair TSRMLS_CC);
130. } else if (!strcasecmp(key, "filename")) {
131. if (filename) {
132. efree(filename);
133. }
134. filename = php_ap_getword_conf(&pair TSRMLS_CC);
135. }
136. }
137. if (key) {
138. efree(key);
139. }
140. efree(word);
141. }
142.
143. /* Normal form variable, safe to read all data into memory */
144. if (!filename && param) {
145. unsigned int value_len;
146. char *value = multipart_buffer_read_body(mbuff, &value_len TSRMLS_CC);
147. unsigned int new_val_len; /* Dummy variable */
148.
149. if (!value) {
150. value = estrdup("");
151. }
152.
153. if (sapi_module.input_filter(PARSE_POST, param, &value, value_len, &new_val_len TSRMLS_CC)) {
154. if (php_rfc1867_callback != NULL) {
155. multipart_event_formdata event_formdata;
156. size_t newlength = 0;
157.
158. event_formdata.post_bytes_processed = SG(read_post_bytes);
159. event_formdata.name = param;
160. event_formdata.value = &value;
161. event_formdata.length = new_val_len;
162. wlength = &newlength;
163. if (php_rfc1867_callback(MULTIPART_EVENT_FORMDATA, &event_formdata, &event_extra_data TSRMLS_CC) == FAILURE) {
166. continue;
167. }
168. new_val_len = newlength;
169. }
170.
171. #if HAVE_MBSTRING && !defined(COMPILE_DL_MBSTRING)
172. if (php_mb_encoding_translation(TSRMLS_C)) {
173. php_mb_gpc_stack_variable(param, value, &val_list, &len_list,
174. &num_vars, &num_vars_max TSRMLS_CC);
175. } else {
176. safe_php_register_variable(param, value, new_val_len, array_ptr, 0 TSRMLS_CC);inputtypefile不上传文件
177. }
178. #else
179. safe_php_register_variable(param, value, new_val_len, array_ptr, 0 TSRMLS_CC);
180. #endif
181. } else if (php_rfc1867_callback != NULL) {
182. multipart_event_formdata event_formdata;
183.
184. event_formdata.post_bytes_processed = SG(read_post_bytes);
185. event_formdata.name = param;
186. event_formdata.value = &value;
187. event_formdata.length = value_len;
188. wlength = NULL;
189. php_rfc1867_callback(MULTIPART_EVENT_FORMDATA, &event_formdata, &event_extra_data TSRMLS_CC);
190. }
191.
192. if (!strcasecmp(param, "MAX_FILE_SIZE")) {
193. max_file_size = atol(value);
194. }
195.
196. efree(param);
197. efree(value);
198. continue;
199. }
200.
201. /* If file_uploads=off, skip the file part */
202. if (!PG(file_uploads)) {
203. skip_upload = 1;
204. }
205.
206. /* Return with an error if the posted data is garbled */
207. if (!param && !filename) {
208. sapi_module.sapi_error(E_WARNING, "File Upload Mime headers garbled");
209. goto fileupload_done;
210. }
211.
212. if (!param) {
213. is_anonymous = 1;
214. param = emalloc(MAX_SIZE_ANONNAME);
215. snprintf(param, MAX_SIZE_ANONNAME, "%u", anonindex++);
216. } else {
217. is_anonymous = 0;
218. }
219.
220. /* New Rule: never repair potential malicious user input */
221. if (!skip_upload) {
222. char *tmp = param;
223. long c = 0;
224.
225. while (*tmp) {
226. if (*tmp == '[') {
227. c++;
228. } else if (*tmp == ']') {
229. c--;
230. if (tmp[1] && tmp[1] != '[') {
231. skip_upload = 1;
232. break;
233. }
234. }
235. if (c < 0) {
236. skip_upload = 1;
237. break;
238. }
239. tmp++;
240. }
241. }
242.
243. total_bytes = cancel_upload = 0;
244.
245. if (!skip_upload) {
246. /* Handle file */
247. fd = php_open_temporary_fd_ex(PG(upload_tmp_dir), "php", &temp_filename, 1 TSRMLS_CC);
248. if (fd==-1) {
249. sapi_module.sapi_error(E_WARNING, "File upload error - unable to create a temporary file");
250. cancel_upload = UPLOAD_ERROR_E;
251. }
252. }
253.
254. if (!skip_upload && php_rfc1867_callback != NULL) {
255. multipart_event_file_start event_file_start;
256.
257. event_file_start.post_bytes_processed = SG(read_post_bytes);
258. event_file_start.name = param;
259. event_file_start.filename = &filename;
260. if (php_rfc1867_callback(MULTIPART_EVENT_FILE_START, &event_file_start, &event_extra_data TSRMLS_CC) == FAILURE) { 261. if (temp_filename) {
262. if (cancel_upload != UPLOAD_ERROR_E) { /* file creation failed */
265. }
266. efree(temp_filename);
267. }
268. temp_filename="";
269. efree(param);
270. efree(filename);
271. continue;
272. }
273. }
274.
275. if (skip_upload) {
276. efree(param);
277. efree(filename);
278. continue;
279. }
280.
281. if(strlen(filename) == 0) {
282. #if DEBUG_FILE_UPLOAD
283. sapi_module.sapi_error(E_NOTICE, "No file uploaded");
284. #endif
285. cancel_upload = UPLOAD_ERROR_D;
286. }
287.
288. offset = 0;
289. end = 0;
290. while (!cancel_upload && (blen = multipart_buffer_read(mbuff, buff, sizeof(buff), &end TSRMLS_CC)))
291. {
292. if (php_rfc1867_callback != NULL) {
293. multipart_event_file_data event_file_data;
294.
295. event_file_data.post_bytes_processed = SG(read_post_bytes);
296. event_file_data.offset = offset;
297. event_file_data.data = buff;
298. event_file_data.length = blen;
299. event_wlength = &blen;
300. if (php_rfc1867_callback(MULTIPART_EVENT_FILE_DATA, &event_file_data, &event_extra_data TSRMLS_CC) == FAILURE) {
301. cancel_upload = UPLOAD_ERROR_X;
302. continue;
303. }
304. }
305.
306. if (PG(upload_max_filesize) > 0 && total_bytes > PG(upload_max_filesize)) {
307. #if DEBUG_FILE_UPLOAD
308. sapi_module.sapi_error(E_NOTICE, "upload_max_filesize of %ld bytes exceeded - file [%s=%s] not saved", PG(upload_max_filesize), param, filename); 309. #endif
310. cancel_upload = UPLOAD_ERROR_A;
311. } else if (max_file_size && (total_bytes > max_file_size)) {
312. #if DEBUG_FILE_UPLOAD
313. sapi_module.sapi_error(E_NOTICE, "MAX_FILE_SIZE of %ld bytes exceeded - file [%s=%s] not saved", max_file_size, param, filename);
314. #endif
315. cancel_upload = UPLOAD_ERROR_B;
316. } else if (blen > 0) {
317.
318. wlen = write(fd, buff, blen);
319.
320. if (wlen == -1) {
321. /* write failed */
322. #if DEBUG_FILE_UPLOAD
323. sapi_module.sapi_error(E_NOTICE, "write() failed - %s", strerror(errno));
324. #endif
325. cancel_upload = UPLOAD_ERROR_F;
326. } else if (wlen < blen) {
327. #if DEBUG_FILE_UPLOAD
328. sapi_module.sapi_error(E_NOTICE, "Only %d bytes were written, expected to write %d", wlen, blen);
329. #endif
330. cancel_upload = UPLOAD_ERROR_F;
331. } else {
332. total_bytes += wlen;
333. }
334.
335. offset += wlen;
336. }
337. }
338. if (fd!=-1) { /* may not be initialized if file could not be created */
339. close(fd);
340. }
341. if (!cancel_upload && !end) {
342. #if DEBUG_FILE_UPLOAD
343. sapi_module.sapi_error(E_NOTICE, "Missing mime boundary at the end of the data for file %s", strlen(filename) > 0 ? filename : "");
344. #endif
345. cancel_upload = UPLOAD_ERROR_C;
346. }
347. #if DEBUG_FILE_UPLOAD
348. if(strlen(filename) > 0 && total_bytes == 0 && !cancel_upload) {
349. sapi_module.sapi_error(E_WARNING, "Uploaded file size 0 - file [%s=%s] not saved", param, filename);
350. cancel_upload = 5;
351. }
352. #endif
353.
354. if (php_rfc1867_callback != NULL) {
355. multipart_event_file_end event_file_end;
356.
357. event_file_end.post_bytes_processed = SG(read_post_bytes);
358. event_p_filename = temp_filename;
359. event_file_end.cancel_upload = cancel_upload;
360. if (php_rfc1867_callback(MULTIPART_EVENT_FILE_END, &event_file_end, &event_extra_data TSRMLS_CC) == FAILURE) {
361. cancel_upload = UPLOAD_ERROR_X;
364.
365. if (cancel_upload) {
366. if (temp_filename) {
367. if (cancel_upload != UPLOAD_ERROR_E) { /* file creation failed */
368. unlink(temp_filename);
369. }
370. efree(temp_filename);
371. }
372. temp_filename="";
373. } else {
374. zend_hash_add(SG(rfc1867_uploaded_files), temp_filename, strlen(temp_filename) + 1, &temp_filename, sizeof(char *), NULL); 375. }
376.
377. /* is_arr_upload is true when name of file upload field
378. * ends in [.*]
379. * start_arr is set to point to 1st [
380. */
381. is_arr_upload = (start_arr = strchr(param,'[')) && (param[strlen(param)-1] == ']');
382.
383. if (is_arr_upload) {
384. array_len = strlen(start_arr);
385. if (array_index) {
386. efree(array_index);
387. }
388. array_index = estrndup(start_arr+1, array_len-2);
389. }
390.
391. /* Add $foo_name */
392. if (llen < strlen(param) + MAX_SIZE_OF_INDEX + 1) {
393. llen = strlen(param);
394. lbuf = (char *) safe_erealloc(lbuf, llen, 1, MAX_SIZE_OF_INDEX + 1);
395. llen += MAX_SIZE_OF_INDEX + 1;
396. }
397.
398. if (is_arr_upload) {
399. if (abuf) efree(abuf);
400. abuf = estrndup(param, strlen(param)-array_len);
401. snprintf(lbuf, llen, "%s_name[%s]", abuf, array_index);
402. } else {
403. snprintf(lbuf, llen, "%s_name", param);
404. }
405.
406. #if HAVE_MBSTRING && !defined(COMPILE_DL_MBSTRING)
407. if (php_mb_encoding_translation(TSRMLS_C)) {
408. if (num_vars>=num_vars_max){
409. php_mb_gpc_realloc_buffer(&val_list, &len_list, &num_vars_max,
410. 1 TSRMLS_CC);
411. }
412. val_list[num_vars] = filename;
413. len_list[num_vars] = strlen(filename);
414. num_vars++;
415. if(php_mb_gpc_encoding_detector(val_list, len_list, num_vars, NULL TSRMLS_CC) == SUCCESS) {
416. str_len = strlen(filename);
417. php_mb_gpc_encoding_converter(&filename, &str_len, 1, NULL, NULL TSRMLS_CC);
418. }
419. s = php_mb_strrchr(filename, '\' TSRMLS_CC);
420. if ((tmp = php_mb_strrchr(filename, '/' TSRMLS_CC)) > s) {
421. s = tmp;
422. }
423. num_vars--;
424. goto filedone;
425. }
426. #endif
427. /* The check should technically be needed for win32 systems only where
428. * it is a valid path separator. However, IE in all it's wisdom always sends
429. * the full path of the file on the user's filesystem, which means that unless
430. * the user does basename() they get a bogus file name. Until IE's user base drops
431. * to nill or problem is fixed this code must remain enabled for all systems.
432. */
433. s = strrchr(filename, '\');
434. if ((tmp = strrchr(filename, '/')) > s) {
435. s = tmp;
436. }
437. #ifdef PHP_WIN32
438. if (PG(magic_quotes_gpc)) {
439. s = s ? s : filename;
440. tmp = strrchr(s, ''');
441. s = tmp > s ? tmp : s;
442. tmp = strrchr(s, '"');
443. s = tmp > s ? tmp : s;
444. }
445. #endif
446.
447. #if HAVE_MBSTRING && !defined(COMPILE_DL_MBSTRING)
448. filedone:
449. #endif
450.
451. if (!is_anonymous) {
452. if (s && s > filename) {
453. safe_php_register_variable(lbuf, s+1, strlen(s+1), NULL, 0 TSRMLS_CC);
454. } else {
455. safe_php_register_variable(lbuf, filename, strlen(filename), NULL, 0 TSRMLS_CC);
456. }
457. }
458.
459. /* Add $foo[name] */
460. if (is_arr_upload) {
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论