Press "Enter" to skip to content

深入理解PHP原理之文件上传

今天研究PHP注册POST/GET大变量的时候,看到了关于这块的一些东西,跟踪了半天,,先记录下来,免得以后再如此麻烦的跟踪

处理器注册:

  1.    [mod_php5.c, mod_php5模块初始化] php_init_handler(server_rec *s, pool *p)
  2.      ->[main/SAPI.c]sapi_startup(&apache_sapi_module)
  3.           ->[main/SAPI.c] sapi_globals_ctor(&sapi_globals)
  4.                ->[main/php_content_types.c]php_setup_sapi_content_types(TSRMLS_C)
  5.                     ->[main/php_content_types.c php_post_entries如下]sapi_register_post_entries(php_post_entries TSRMLS_CC)
  6.                          ->[main/SAPI.c]sapi_register_post_entry(p TSRMLS_CC)

如下面的代码,共注册了俩个处理器,分别处理post数据和文件上传。
注1:G(http_globals)[TRACK_VARS_COOKIE]这部分,可以参看我较早前的 在PHP Module中获取$_GET/$_POST/$_COOKIE的方法研究

  1.   [main/rfc1867.h]
  2. #define MULTIPART_CONTENT_TYPE "multipart/form-data"
  3.   [main/php_content_types.h]
  4. #define DEFAULT_POST_CONTENT_TYPE "application/x-www-form-urlencoded"
  5.   [main/SAPI.c]
  6.      struct _sapi_post_entry {
  7.          char *content_type;
  8.          uint content_type_len;
  9.          void (*post_reader)(TSRMLS_D);
  10.          void (*post_handler)(char *content_type_dup, void *arg TSRMLS_DC);
  11.      };
  12.   [main/php_content_types.c]
  13.      static sapi_post_entry php_post_entries[] = {
  14.          { DEFAULT_POST_CONTENT_TYPE, sizeof(DEFAULT_POST_CONTENT_TYPE)-1, sapi_read_standard_form_data, php_std_post_handler },
  15.          { MULTIPART_CONTENT_TYPE, sizeof(MULTIPART_CONTENT_TYPE)-1, NULL, rfc1867_post_handler },
  16.          { NULL, 0, NULL, NULL }
  17. };
  18.  

那么对于rfc1867_post_handler函数来说,我罗列出源码如下, 加了我的注释.

  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.         if (SG(request_info).content_length > SG(post_max_size)) {
  21.                 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));
  22.                 return;
  23.         }
  24.         //取得上传文件的分隔符
  25.         boundary = strstr(content_type_dup, "boundary");
  26.         if (!boundary || !(boundary=strchr(boundary, '='))) {
  27.                 sapi_module.sapi_error(E_WARNING, "Missing boundary in multipart/form-data POST data");
  28.                 return;
  29.         }
  30.         boundary++;
  31.         boundary_len = strlen(boundary);
  32.         if (boundary[0] == '"') {
  33.                 boundary++;
  34.                 boundary_end = strchr(boundary, '"');
  35.                 if (!boundary_end) {
  36.                         sapi_module.sapi_error(E_WARNING, "Invalid boundary in multipart/form-data POST data");
  37.                         return;
  38.                 }
  39.         } else {
  40.                 /* search for the end of the boundary */
  41.                 boundary_end = strchr(boundary, ',');
  42.         }
  43.         if (boundary_end) {
  44.                 boundary_end[0] = '';
  45.                 boundary_len = boundary_end-boundary;
  46.         }
  47.         /* Initialize the buffer */
  48.         if (!(mbuff = multipart_buffer_new(boundary, boundary_len))) {
  49.                 sapi_module.sapi_error(E_WARNING, "Unable to initialize the input buffer");
  50.                 return;
  51.         }
  52.         //初始化$_FILE变量
  53.         zend_hash_init(&PG(rfc1867_protected_variables), 5, NULL, NULL, 0);
  54.         ALLOC_HASHTABLE(uploaded_files);
  55.         zend_hash_init(uploaded_files, 5, NULL, (dtor_func_t) free_estring, 0);
  56.         SG(rfc1867_uploaded_files) = uploaded_files;
  57.         ALLOC_ZVAL(http_post_files);
  58.         array_init(http_post_files);
  59.         INIT_PZVAL(http_post_files);
  60.         PG(http_globals)[TRACK_VARS_FILES] = http_post_files; //TRACK_VARS_FILE正是_FILE在php_core_globals.http_globals中的index (注1)
  61. #if HAVE_MBSTRING && !defined(COMPILE_DL_MBSTRING)
  62.         if (php_mb_encoding_translation(TSRMLS_C)) {
  63.                 val_list = (char **)ecalloc(num_vars_max+2, sizeof(char *));
  64.                 len_list = (int *)ecalloc(num_vars_max+2, sizeof(int));
  65.         }
  66. #endif
  67.         zend_llist_init(&header, sizeof(mime_header_entry), (llist_dtor_func_t) php_free_hdr_entry, 0);
  68.         if (php_rfc1867_callback != NULL) {
  69.                 multipart_event_start event_start;
  70.                 event_start.content_length = SG(request_info).content_length;
  71.                 if (php_rfc1867_callback(MULTIPART_EVENT_START, &event_start, &event_extra_data TSRMLS_CC) == FAILURE) {
  72.                         goto fileupload_done;
  73.                 }
  74.         }
  75.         while (!multipart_buffer_eof(mbuff TSRMLS_CC))
  76.         {
  77.                 char buff[FILLUNIT];
  78.                 char *cd=NULL,*param=NULL,*filename=NULL, *tmp=NULL;
  79.                 size_t blen=0, wlen=0;
  80.                 off_t offset;
  81.                 zend_llist_clean(&header);
  82.                 if (!multipart_buffer_headers(mbuff, &header TSRMLS_CC)) {
  83.                         goto fileupload_done;
  84.                 }
  85.                 if ((cd = php_mime_get_hdr_value(header, "Content-Disposition"))) {
  86.                         char *pair=NULL;
  87.                         int end=0;
  88.                         while (isspace(*cd)) {
  89.                                 ++cd;
  90.                         }
  91.                         while (*cd && (pair = php_ap_getword(&cd, ';')))
  92.                         {
  93.                                 char *key=NULL, *word = pair;
  94.                                 while (isspace(*cd)) {
  95.                                         ++cd;
  96.                                 }
  97.                                 if (strchr(pair, '=')) {
  98.                                         key = php_ap_getword(&pair, '=');
  99.                                         if (!strcasecmp(key, "name")) {
  100.                                                 if (param) {
  101.                                                         efree(param);
  102.                                                 }
  103.                                                 param = php_ap_getword_conf(&pair TSRMLS_CC);
  104.                                         } else if (!strcasecmp(key, "filename")) {
  105.                                                 if (filename) {
  106.                                                         efree(filename);
  107.                                                 }
  108.                                                 filename = php_ap_getword_conf(&pair TSRMLS_CC);
  109.                                         }
  110.                                 }
  111.                                 if (key) {
  112.                                         efree(key);
  113.                                 }
  114.                                 efree(word);
  115.                         }
  116.                         /* Normal form variable, safe to read all data into memory */
  117.                         if (!filename && param) {
  118.                                 unsigned int value_len;
  119.                                 char *value = multipart_buffer_read_body(mbuff, &value_len TSRMLS_CC);
  120.                                 unsigned int new_val_len; /* Dummy variable */
  121.                                 if (!value) {
  122.                                         value = estrdup("");
  123.                                 }
  124.                                 if (sapi_module.input_filter(PARSE_POST, param, &value, value_len, &new_val_len TSRMLS_CC)) {
  125.                                         if (php_rfc1867_callback != NULL) {
  126.                                                 multipart_event_formdata event_formdata;
  127.                                                 size_t newlength = 0;
  128.                                                 event_formdata.post_bytes_processed = SG(read_post_bytes);
  129.                                                 event_formdata.name = param;
  130.                                                 event_formdata.value = &value;
  131.                                                 event_formdata.length = new_val_len;
  132.                                                 event_formdata.newlength = &newlength;
  133.                                                 if (php_rfc1867_callback(MULTIPART_EVENT_FORMDATA, &event_formdata, &event_extra_data TSRMLS_CC) == FAILURE) {
  134.                                                         efree(param);
  135.                                                         efree(value);
  136.                                                         continue;
  137.                                                 }
  138.                                                 new_val_len = newlength;
  139.                                         }
  140. #if HAVE_MBSTRING && !defined(COMPILE_DL_MBSTRING)
  141.                                         if (php_mb_encoding_translation(TSRMLS_C)) {
  142.                                                 php_mb_gpc_stack_variable(param, value, &val_list, &len_list,
  143.                                                                                                   &num_vars, &num_vars_max TSRMLS_CC);
  144.                                         } else {
  145.                                                 safe_php_register_variable(param, value, new_val_len, array_ptr, 0 TSRMLS_CC);
  146.                                         }
  147. #else
  148.                                         safe_php_register_variable(param, value, new_val_len, array_ptr, 0 TSRMLS_CC);
  149. #endif
  150.                                 } else if (php_rfc1867_callback != NULL) {
  151.                                         multipart_event_formdata event_formdata;
  152.                                         event_formdata.post_bytes_processed = SG(read_post_bytes);
  153.                                         event_formdata.name = param;
  154.                                         event_formdata.value = &value;
  155.                                         event_formdata.length = value_len;
  156.                                         event_formdata.newlength = NULL;
  157.                                         php_rfc1867_callback(MULTIPART_EVENT_FORMDATA, &event_formdata, &event_extra_data TSRMLS_CC);
  158.                                 }
  159.                                 if (!strcasecmp(param, "MAX_FILE_SIZE")) {
  160.                                         max_file_size = atol(value);
  161.                                 }
  162.                                 efree(param);
  163.                                 efree(value);
  164.                                 continue;
  165.                         }
  166.                         /* If file_uploads=off, skip the file part */
  167.                         if (!PG(file_uploads)) {
  168.                                 skip_upload = 1;
  169.                         }
  170.                         /* Return with an error if the posted data is garbled */
  171.                         if (!param && !filename) {
  172.                                 sapi_module.sapi_error(E_WARNING, "File Upload Mime headers garbled");
  173.                                 goto fileupload_done;
  174.                         }
  175.                         if (!param) {
  176.                                 is_anonymous = 1;
  177.                                 param = emalloc(MAX_SIZE_ANONNAME);
  178.                                 snprintf(param, MAX_SIZE_ANONNAME, "%u", anonindex++);
  179.                         } else {
  180.                                 is_anonymous = 0;
  181.                         }
  182.                         /* New Rule: never repair potential malicious user input */
  183.                         if (!skip_upload) {
  184.                                 char *tmp = param;
  185.                                 long c = 0;
  186.                                 while (*tmp) {
  187.                                         if (*tmp == '[') {
  188.                                                 c++;
  189.                                         } else if (*tmp == ']') {
  190.                                                 c--;
  191.                                                 if (tmp[1] && tmp[1] != '[') {
  192.                                                         skip_upload = 1;
  193.                                                         break;
  194.                                                 }
  195.                                         }
  196.                                         if (c < 0) {
  197.                                                 skip_upload = 1;
  198.                                                 break;
  199.                                         }
  200.                                         tmp++;
  201.                                 }
  202.                         }
  203.                         total_bytes = cancel_upload = 0;
  204.                         if (!skip_upload) {
  205.                                 /* Handle file */
  206.                                 fd = php_open_temporary_fd_ex(PG(upload_tmp_dir), "php", &temp_filename, 1 TSRMLS_CC);
  207.                                 if (fd==-1) {
  208.                                         sapi_module.sapi_error(E_WARNING, "File upload error - unable to create a temporary file");
  209.                                         cancel_upload = UPLOAD_ERROR_E;
  210.                                 }
  211.                         }
  212.                         if (!skip_upload && php_rfc1867_callback != NULL) {
  213.                                 multipart_event_file_start event_file_start;
  214.                                 event_file_start.post_bytes_processed = SG(read_post_bytes);
  215.                                 event_file_start.name = param;
  216.                                 event_file_start.filename = &filename;
  217.                                 if (php_rfc1867_callback(MULTIPART_EVENT_FILE_START, &event_file_start, &event_extra_data TSRMLS_CC) == FAILURE) {
  218.                                         if (temp_filename) {
  219.                                                 if (cancel_upload != UPLOAD_ERROR_E) { /* file creation failed */
  220.                                                         close(fd);
  221.                                                         unlink(temp_filename);
  222.                                                 }
  223.                                                 efree(temp_filename);
  224.                                         }
  225.                                         temp_filename="";
  226.                                         efree(param);
  227.                                         efree(filename);
  228.                                         continue;
  229.                                 }
  230.                         }
  231.                         if (skip_upload) {
  232.                                 efree(param);
  233.                                 efree(filename);
  234.                                 continue;
  235.                         }
  236.                         if(strlen(filename) == 0) {
  237. #if DEBUG_FILE_UPLOAD
  238.                                 sapi_module.sapi_error(E_NOTICE, "No file uploaded");
  239. #endif
  240.                                 cancel_upload = UPLOAD_ERROR_D;
  241.                         }
  242.                         offset = 0;
  243.                         end = 0;
  244.                         while (!cancel_upload && (blen = multipart_buffer_read(mbuff, buff, sizeof(buff), &end TSRMLS_CC)))
  245.                         {
  246.                                 if (php_rfc1867_callback != NULL) {
  247.                                         multipart_event_file_data event_file_data;
  248.                                         event_file_data.post_bytes_processed = SG(read_post_bytes);
  249.                                         event_file_data.offset = offset;
  250.                                         event_file_data.data = buff;
  251.                                         event_file_data.length = blen;
  252.                                         event_file_data.newlength = &blen;
  253.                                         if (php_rfc1867_callback(MULTIPART_EVENT_FILE_DATA, &event_file_data, &event_extra_data TSRMLS_CC) == FAILURE) {
  254.                                                 cancel_upload = UPLOAD_ERROR_X;
  255.                                                 continue;
  256.                                         }
  257.                                 }
  258.                                 if (PG(upload_max_filesize) > 0 && total_bytes > PG(upload_max_filesize)) {
  259. #if DEBUG_FILE_UPLOAD
  260.                                         sapi_module.sapi_error(E_NOTICE, "upload_max_filesize of %ld bytes exceeded - file [%s=%s] not saved", PG(upload_max_filesize), param, filename);
  261. #endif
  262.                                         cancel_upload = UPLOAD_ERROR_A;
  263.                                 } else if (max_file_size && (total_bytes > max_file_size)) {
  264. #if DEBUG_FILE_UPLOAD
  265.                                         sapi_module.sapi_error(E_NOTICE, "MAX_FILE_SIZE of %ld bytes exceeded - file [%s=%s] not saved", max_file_size, param, filename);
  266. #endif
  267.                                         cancel_upload = UPLOAD_ERROR_B;
  268.                                 } else if (blen > 0) {
  269.                                         wlen = write(fd, buff, blen);
  270.                                         if (wlen == -1) {
  271.                                                 /* write failed */
  272. #if DEBUG_FILE_UPLOAD
  273.                                                 sapi_module.sapi_error(E_NOTICE, "write() failed - %s", strerror(errno));
  274. #endif
  275.                                                 cancel_upload = UPLOAD_ERROR_F;
  276.                                         } else if (wlen < blen) {
  277. #if DEBUG_FILE_UPLOAD
  278.                                                 sapi_module.sapi_error(E_NOTICE, "Only %d bytes were written, expected to write %d", wlen, blen);
  279. #endif
  280.                                                 cancel_upload = UPLOAD_ERROR_F;
  281.                                         } else {
  282.                                                 total_bytes += wlen;
  283.                                         }
  284.                                         offset += wlen;
  285.                                 }
  286.                         }
  287.                         if (fd!=-1) { /* may not be initialized if file could not be created */
  288.                                 close(fd);
  289.                         }
  290.                         if (!cancel_upload && !end) {
  291. #if DEBUG_FILE_UPLOAD
  292.                                 sapi_module.sapi_error(E_NOTICE, "Missing mime boundary at the end of the data for file %s", strlen(filename) > 0 ? filename : "");
  293. #endif
  294.                                 cancel_upload = UPLOAD_ERROR_C;
  295.                         }
  296. #if DEBUG_FILE_UPLOAD
  297.                         if(strlen(filename) > 0 && total_bytes == 0 && !cancel_upload) {
  298.                                 sapi_module.sapi_error(E_WARNING, "Uploaded file size 0 - file [%s=%s] not saved", param, filename);
  299.                                 cancel_upload = 5;
  300.                         }
  301. #endif
  302.                         if (php_rfc1867_callback != NULL) {
  303.                                 multipart_event_file_end event_file_end;
  304.                                 event_file_end.post_bytes_processed = SG(read_post_bytes);
  305.                                 event_file_end.temp_filename = temp_filename;
  306.                                 event_file_end.cancel_upload = cancel_upload;
  307.                                 if (php_rfc1867_callback(MULTIPART_EVENT_FILE_END, &event_file_end, &event_extra_data TSRMLS_CC) == FAILURE) {
  308.                                         cancel_upload = UPLOAD_ERROR_X;
  309.                                 }
  310.                         }
  311.                         if (cancel_upload) {
  312.                                 if (temp_filename) {
  313.                                         if (cancel_upload != UPLOAD_ERROR_E) { /* file creation failed */
  314.                                                 unlink(temp_filename);
  315.                                         }
  316.                                         efree(temp_filename);
  317.                                 }
  318.                                 temp_filename="";
  319.                         } else {
  320.                                 zend_hash_add(SG(rfc1867_uploaded_files), temp_filename, strlen(temp_filename) + 1, &temp_filename, sizeof(char *), NULL);
  321.                         }
  322.                         /* is_arr_upload is true when name of file upload field
  323. * ends in [.*]
  324. * start_arr is set to point to 1st [
  325. */
  326.                         is_arr_upload = (start_arr = strchr(param,'[')) && (param[strlen(param)-1] == ']');
  327.                         if (is_arr_upload) {
  328.                                 array_len = strlen(start_arr);
  329.                                 if (array_index) {
  330.                                         efree(array_index);
  331.                                 }
  332.                                 array_index = estrndup(start_arr+1, array_len-2);
  333.                         }
  334.                         /* Add $foo_name */
  335.                         if (llen < strlen(param) + MAX_SIZE_OF_INDEX + 1) {
  336.                                 llen = strlen(param);
  337.                                 lbuf = (char *) safe_erealloc(lbuf, llen, 1, MAX_SIZE_OF_INDEX + 1);
  338.                                 llen += MAX_SIZE_OF_INDEX + 1;
  339.                         }
  340.                         if (is_arr_upload) {
  341.                                 if (abuf) efree(abuf);
  342.                                 abuf = estrndup(param, strlen(param)-array_len);
  343.                                 snprintf(lbuf, llen, "%s_name[%s]", abuf, array_index);
  344.                         } else {
  345.                                 snprintf(lbuf, llen, "%s_name", param);
  346.                         }
  347. #if HAVE_MBSTRING && !defined(COMPILE_DL_MBSTRING)
  348.                         if (php_mb_encoding_translation(TSRMLS_C)) {
  349.                                 if (num_vars>=num_vars_max){
  350.                                         php_mb_gpc_realloc_buffer(&val_list, &len_list, &num_vars_max,
  351.                                                                                           1 TSRMLS_CC);
  352.                                 }
  353.                                 val_list[num_vars] = filename;
  354.                                 len_list[num_vars] = strlen(filename);
  355.                                 num_vars++;
  356.                                 if(php_mb_gpc_encoding_detector(val_list, len_list, num_vars, NULL TSRMLS_CC) == SUCCESS) {
  357.                                         str_len = strlen(filename);
  358.                                         php_mb_gpc_encoding_converter(&filename, &str_len, 1, NULL, NULL TSRMLS_CC);
  359.                                 }
  360.                                 s = php_mb_strrchr(filename, '\' TSRMLS_CC);
  361. if ((tmp = php_mb_strrchr(filename, '/' TSRMLS_CC)) > s) {
  362. s = tmp;
  363. }
  364. num_vars--;
  365. goto filedone;
  366. }
  367. #endif
  368. /* The check should technically be needed for win32 systems only where
  369. * it is a valid path separator. However, IE in all it's wisdom always sends
  370.                          * the full path of the file on the user's filesystem, which means that unless
  371. * the user does basename() they get a bogus file name. Until IE's user base drops
  372.                          * to nill or problem is fixed this code must remain enabled for all systems.
  373.                          */
  374.                         s = strrchr(filename, '\');
  375. if ((tmp = strrchr(filename, '/')) > s) {
  376. s = tmp;
  377. }
  378. #ifdef PHP_WIN32
  379. if (PG(magic_quotes_gpc)) {
  380. s = s ? s : filename;
  381. tmp = strrchr(s, ''');
  382.                                 s = tmp > s ? tmp : s;
  383.                                 tmp = strrchr(s, '"');
  384.                                 s = tmp > s ? tmp : s;
  385.                         }
  386. #endif
  387. #if HAVE_MBSTRING && !defined(COMPILE_DL_MBSTRING)
  388. filedone:
  389. #endif
  390.                         if (!is_anonymous) {
  391.                                 if (s && s > filename) {
  392.                                         safe_php_register_variable(lbuf, s+1, strlen(s+1), NULL, 0 TSRMLS_CC);
  393.                                 } else {
  394.                                         safe_php_register_variable(lbuf, filename, strlen(filename), NULL, 0 TSRMLS_CC);
  395.                                 }
  396.                         }
  397.                         /* Add $foo[name] */
  398.                         if (is_arr_upload) {
  399.                                 snprintf(lbuf, llen, "%s[name][%s]", abuf, array_index);
  400.                         } else {
  401.                                 snprintf(lbuf, llen, "%s[name]", param);
  402.                         }
  403.                         if (s && s > filename) {
  404.                                 register_http_post_files_variable(lbuf, s+1, http_post_files, 0 TSRMLS_CC);
  405.                         } else {
  406.                                 register_http_post_files_variable(lbuf, filename, http_post_files, 0 TSRMLS_CC);
  407.                         }
  408.                         efree(filename);
  409.                         s = NULL;
  410.                         /* Possible Content-Type: */
  411.                         if (cancel_upload || !(cd = php_mime_get_hdr_value(header, "Content-Type"))) {
  412.                                 cd = "";
  413.                         } else {
  414.                                 /* fix for Opera 6.01 */
  415.                                 s = strchr(cd, ';');
  416.                                 if (s != NULL) {
  417.                                         *s = '';
  418.                                 }
  419.                         }
  420.                         /* Add $foo_type */
  421.                         if (is_arr_upload) {
  422.                                 snprintf(lbuf, llen, "%s_type[%s]", abuf, array_index);
  423.                         } else {
  424.                                 snprintf(lbuf, llen, "%s_type", param);
  425.                         }
  426.                         if (!is_anonymous) {
  427.                                 safe_php_register_variable(lbuf, cd, strlen(cd), NULL, 0 TSRMLS_CC);
  428.                         }
  429.                         /* Add $foo[type] */
  430.                         if (is_arr_upload) {
  431.                                 snprintf(lbuf, llen, "%s[type][%s]", abuf, array_index);
  432.                         } else {
  433.                                 snprintf(lbuf, llen, "%s[type]", param);
  434.                         }
  435.                         register_http_post_files_variable(lbuf, cd, http_post_files, 0 TSRMLS_CC);
  436.                         /* Restore Content-Type Header */
  437.                         if (s != NULL) {
  438.                                 *s = ';';
  439.                         }
  440.                         s = "";
  441.                         /* Initialize variables */
  442.                         add_protected_variable(param TSRMLS_CC);
  443.                         magic_quotes_gpc = PG(magic_quotes_gpc);
  444.                         PG(magic_quotes_gpc) = 0;
  445.                         /* if param is of form xxx[.*] this will cut it to xxx */
  446.                         if (!is_anonymous) {
  447.                                 safe_php_register_variable(param, temp_filename, strlen(temp_filename), NULL, 1 TSRMLS_CC);
  448.                         }
  449.                         /* Add $foo[tmp_name] */
  450.                         if (is_arr_upload) {
  451.                                 snprintf(lbuf, llen, "%s[tmp_name][%s]", abuf, array_index);
  452.                         } else {
  453.                                 snprintf(lbuf, llen, "%s[tmp_name]", param);
  454.                         }
  455.                         add_protected_variable(lbuf TSRMLS_CC);
  456.                         register_http_post_files_variable(lbuf, temp_filename, http_post_files, 1 TSRMLS_CC);
  457.                         PG(magic_quotes_gpc) = magic_quotes_gpc;
  458.                         {
  459.                                 zval file_size, error_type;
  460.                                 error_type.value.lval = cancel_upload;
  461.                                 error_type.type = IS_LONG;
  462.                                 /* Add $foo[error] */
  463.                                 if (cancel_upload) {
  464.                                         file_size.value.lval = 0;
  465.                                         file_size.type = IS_LONG;
  466.                                 } else {
  467.                                         file_size.value.lval = total_bytes;
  468.                                         file_size.type = IS_LONG;
  469.                                 }
  470.                                 if (is_arr_upload) {
  471.                                         snprintf(lbuf, llen, "%s[error][%s]", abuf, array_index);
  472.                                 } else {
  473.                                         snprintf(lbuf, llen, "%s[error]", param);
  474.                                 }
  475.                                 register_http_post_files_variable_ex(lbuf, &error_type, http_post_files, 0 TSRMLS_CC);
  476.                                 /* Add $foo_size */
  477.                                 if (is_arr_upload) {
  478.                                         snprintf(lbuf, llen, "%s_size[%s]", abuf, array_index);
  479.                                 } else {
  480.                                         snprintf(lbuf, llen, "%s_size", param);
  481.                                 }
  482.                                 if (!is_anonymous) {
  483.                                         safe_php_register_variable_ex(lbuf, &file_size, NULL, 0 TSRMLS_CC);
  484.                                 }
  485.                                 /* Add $foo[size] */
  486.                                 if (is_arr_upload) {
  487.                                         snprintf(lbuf, llen, "%s[size][%s]", abuf, array_index);
  488.                                 } else {
  489.                                         snprintf(lbuf, llen, "%s[size]", param);
  490.                                 }
  491.                                 register_http_post_files_variable_ex(lbuf, &file_size, http_post_files, 0 TSRMLS_CC);
  492.                         }
  493.                         efree(param);
  494.                 }
  495.         }
  496. fileupload_done:
  497.         if (php_rfc1867_callback != NULL) {
  498.                 multipart_event_end event_end;
  499.                 event_end.post_bytes_processed = SG(read_post_bytes);
  500.                 php_rfc1867_callback(MULTIPART_EVENT_END, &event_end, &event_extra_data TSRMLS_CC);
  501.         }
  502.         SAFE_RETURN;
  503. }

19 Comments

  1. […] 关于php_rfc1867_callback是什么,可以看哥学社另一哥鸟哥的分析http://www.laruence.com/2008/11/07/586.html三.总结 在探测临时文件大小的时候,APC和 uploadprogress其实是大同小异的方法,先记录,再取大小百分比。 关于哥学社:哥学社是一个自由博客组织,以提高原创技术博客质量,为成员博客增加更有效评论,由来自腾讯、百度、盛大、人人网、新浪、奇虎、TOM等各大企业的哥们共同起草发起。 关于五四陈: 54chen(陈臻),人人网分布式存储研究人员,业余时间混迹于各技术组织且乐此不疲。目前关注实施PHP培训。对flex等前端技术有一点研究。 个人技术站点:http://www.54chen.com/ 。可以通过电子邮件czhttp@gmail.com 联系到他。 本文提及的文章:http://www.php.net/releases/5_2_0.phphttp://www.ibm.com/developerworks/cn/opensource/os-php-v525/http://www.laruence.com/2008/11/07/586.html   […]

  2. […] 关于php_rfc1867_callback是什么,可以看哥学社另一哥鸟哥的分析 http://www.laruence.com/2008/11/07/586.html […]

  3. zvaly
    zvaly November 9, 2008

    新来这2篇文章很喜欢,尤其是文件上传,曾经研究过文件上传流程,没有你这么细,要检讨啦~~

    • 雪候鸟
      雪候鸟 November 10, 2008

      呵呵,那就好, 只是现在还只是整理出来个大体框架,最好能有问题驱动, 才能找到一些可以被应用的features,

  4. 3gwind
    3gwind November 8, 2008

    就是你用什么工具跟踪的,我对这个比较感兴趣

  5. blankyao
    blankyao November 7, 2008

    鸟哥同学,能不能介绍下你是用的啥方法跟踪的?或者说在分析调试的时候用的啥方法?

    • 雪候鸟
      雪候鸟 November 7, 2008

      大胆猜测,小心求证
      呵呵.

      • blankyao
        blankyao November 8, 2008

        在这个求证的过程中是不是一遍遍的修改编译调试,最后得到结果?

        • 雪候鸟
          雪候鸟 November 9, 2008

          一般不需要,除非是实在通过猜测流程走不下去了才需要这样.

Comments are closed.