📄 file.c
字号:
memset(buf, 0, len + 1); if (php_stream_get_line(stream, buf, len, &actual_len) == NULL) { efree(buf); RETURN_FALSE; } /* strlen() can be used here since we are doing it on the return of an fgets() anyway */ retval_len = php_strip_tags(buf, actual_len, &stream->fgetss_state, allowed_tags, allowed_tags_len); RETURN_STRINGL(buf, retval_len, 0);}/* }}} *//* {{{ proto mixed fscanf(resource stream, string format [, string ...]) Implements a mostly ANSI compatible fscanf() */PHP_FUNCTION(fscanf){ int result; zval **file_handle, **format_string; size_t len; int type; char *buf; void *what; zval ***args; int argCount; argCount = ZEND_NUM_ARGS(); if (argCount < 2) { WRONG_PARAM_COUNT; } args = (zval ***)safe_emalloc(argCount, sizeof(zval **), 0); if (!args || (zend_get_parameters_array_ex(argCount, args) == FAILURE)) { efree( args ); WRONG_PARAM_COUNT; } file_handle = args[0]; format_string = args[1]; what = zend_fetch_resource(file_handle TSRMLS_CC, -1, "File-Handle", &type, 2, php_file_le_stream(), php_file_le_pstream()); /* * we can't do a ZEND_VERIFY_RESOURCE(what), otherwise we end up * with a leak if we have an invalid filehandle. This needs changing * if the code behind ZEND_VERIFY_RESOURCE changed. - cc */ if (!what) { efree(args); RETURN_FALSE; } buf = php_stream_get_line((php_stream *) what, NULL, 0, &len); if (buf == NULL) { efree(args); RETURN_FALSE; } convert_to_string_ex(format_string); result = php_sscanf_internal(buf, Z_STRVAL_PP(format_string), argCount, args, 2, &return_value TSRMLS_CC); efree(args); efree(buf); if (SCAN_ERROR_WRONG_PARAM_COUNT == result) { WRONG_PARAM_COUNT; }}/* }}} *//* {{{ proto int fwrite(resource fp, string str [, int length]) Binary-safe file write */PHPAPI PHP_FUNCTION(fwrite){ zval **arg1, **arg2, **arg3=NULL; int ret; int num_bytes; char *buffer = NULL; php_stream *stream; switch (ZEND_NUM_ARGS()) { case 2: if (zend_get_parameters_ex(2, &arg1, &arg2)==FAILURE) { RETURN_FALSE; } convert_to_string_ex(arg2); num_bytes = Z_STRLEN_PP(arg2); break; case 3: if (zend_get_parameters_ex(3, &arg1, &arg2, &arg3)==FAILURE) { RETURN_FALSE; } convert_to_string_ex(arg2); convert_to_long_ex(arg3); num_bytes = MIN(Z_LVAL_PP(arg3), Z_STRLEN_PP(arg2)); break; default: WRONG_PARAM_COUNT; /* NOTREACHED */ break; } PHP_STREAM_TO_ZVAL(stream, arg1); if (!arg3 && PG(magic_quotes_runtime)) { buffer = estrndup(Z_STRVAL_PP(arg2), Z_STRLEN_PP(arg2)); php_stripslashes(buffer, &num_bytes TSRMLS_CC); } ret = php_stream_write(stream, buffer ? buffer : Z_STRVAL_PP(arg2), num_bytes); if (buffer) { efree(buffer); } RETURN_LONG(ret);}/* }}} *//* {{{ proto bool fflush(resource fp) Flushes output */PHPAPI PHP_FUNCTION(fflush){ zval **arg1; int ret; php_stream *stream; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg1) == FAILURE) { WRONG_PARAM_COUNT; } PHP_STREAM_TO_ZVAL(stream, arg1); ret = php_stream_flush(stream); if (ret) { RETURN_FALSE; } RETURN_TRUE;}/* }}} *//* {{{ proto int stream_set_write_buffer(resource fp, int buffer) Set file write buffer */PHP_FUNCTION(stream_set_write_buffer){ zval **arg1, **arg2; int ret; size_t buff; php_stream *stream; switch (ZEND_NUM_ARGS()) { case 2: if (zend_get_parameters_ex(2, &arg1, &arg2)==FAILURE) { RETURN_FALSE; } break; default: WRONG_PARAM_COUNT; /* NOTREACHED */ break; } PHP_STREAM_TO_ZVAL(stream, arg1); convert_to_long_ex(arg2); buff = Z_LVAL_PP(arg2); /* if buff is 0 then set to non-buffered */ if (buff == 0) { ret = php_stream_set_option(stream, PHP_STREAM_OPTION_WRITE_BUFFER, PHP_STREAM_BUFFER_NONE, NULL); } else { ret = php_stream_set_option(stream, PHP_STREAM_OPTION_WRITE_BUFFER, PHP_STREAM_BUFFER_FULL, &buff); } RETURN_LONG(ret == 0 ? 0 : EOF);}/* }}} *//* {{{ proto bool rewind(resource fp) Rewind the position of a file pointer */PHPAPI PHP_FUNCTION(rewind){ zval **arg1; php_stream *stream; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg1) == FAILURE) { WRONG_PARAM_COUNT; } PHP_STREAM_TO_ZVAL(stream, arg1); if (-1 == php_stream_rewind(stream)) { RETURN_FALSE; } RETURN_TRUE;}/* }}} *//* {{{ proto int ftell(resource fp) Get file pointer's read/write position */PHPAPI PHP_FUNCTION(ftell){ zval **arg1; long ret; php_stream *stream; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg1) == FAILURE) { WRONG_PARAM_COUNT; } PHP_STREAM_TO_ZVAL(stream, arg1); ret = php_stream_tell(stream); if (ret == -1) { RETURN_FALSE; } RETURN_LONG(ret);}/* }}} *//* {{{ proto int fseek(resource fp, int offset [, int whence]) Seek on a file pointer */PHPAPI PHP_FUNCTION(fseek){ zval **arg1, **arg2, **arg3; int argcount = ZEND_NUM_ARGS(), whence = SEEK_SET; php_stream *stream; if (argcount < 2 || argcount > 3 || zend_get_parameters_ex(argcount, &arg1, &arg2, &arg3) == FAILURE) { WRONG_PARAM_COUNT; } PHP_STREAM_TO_ZVAL(stream, arg1); convert_to_long_ex(arg2); if (argcount > 2) { convert_to_long_ex(arg3); whence = Z_LVAL_PP(arg3); } RETURN_LONG(php_stream_seek(stream, Z_LVAL_PP(arg2), whence));}/* }}} *//* {{{ proto bool mkdir(string pathname[, int mode]) Create a directory */PHP_FUNCTION(mkdir){ int dir_len, ret; long mode = 0777; char *dir; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &dir, &dir_len, &mode) == FAILURE) { return; } if (PG(safe_mode) && (!php_checkuid(dir, NULL, CHECKUID_CHECK_FILE_AND_DIR))) { RETURN_FALSE; } if (php_check_open_basedir(dir TSRMLS_CC)) { RETURN_FALSE; } ret = VCWD_MKDIR(dir, (mode_t)mode); if (ret < 0) { php_error_docref1(NULL TSRMLS_CC, dir, E_WARNING, "%s", strerror(errno)); RETURN_FALSE; } RETURN_TRUE;}/* }}} *//* {{{ proto bool rmdir(string dirname) Remove a directory */PHP_FUNCTION(rmdir){ zval **arg1; int ret; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg1) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(arg1); if (PG(safe_mode) &&(!php_checkuid(Z_STRVAL_PP(arg1), NULL, CHECKUID_CHECK_FILE_AND_DIR))) { RETURN_FALSE; } if (php_check_open_basedir(Z_STRVAL_PP(arg1) TSRMLS_CC)) { RETURN_FALSE; } ret = VCWD_RMDIR(Z_STRVAL_PP(arg1)); if (ret < 0) { php_error_docref1(NULL TSRMLS_CC, Z_STRVAL_PP(arg1), E_WARNING, "%s", strerror(errno)); RETURN_FALSE; } RETURN_TRUE;}/* }}} *//* {{{ proto int readfile(string filename [, int use_include_path]) Output a file or a URL */PHP_FUNCTION(readfile){ zval **arg1, **arg2; int size=0; int use_include_path=0; php_stream *stream; /* check args */ switch (ZEND_NUM_ARGS()) { case 1: if (zend_get_parameters_ex(1, &arg1) == FAILURE) { WRONG_PARAM_COUNT; } break; case 2: if (zend_get_parameters_ex(2, &arg1, &arg2) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_long_ex(arg2); use_include_path = Z_LVAL_PP(arg2); break; default: WRONG_PARAM_COUNT; } convert_to_string_ex(arg1); stream = php_stream_open_wrapper(Z_STRVAL_PP(arg1), "rb", (use_include_path ? USE_PATH : 0) | ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL); if (stream) { size = php_stream_passthru(stream); php_stream_close(stream); RETURN_LONG(size); } RETURN_FALSE;}/* }}} *//* {{{ proto int umask([int mask]) Return or change the umask */PHP_FUNCTION(umask){ pval **arg1; int oldumask; int arg_count = ZEND_NUM_ARGS(); oldumask = umask(077); if (arg_count == 0) { umask(oldumask); } else { if (arg_count > 1 || zend_get_parameters_ex(1, &arg1) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_long_ex(arg1); umask(Z_LVAL_PP(arg1)); } /* XXX we should maybe reset the umask after each request! */ RETURN_LONG(oldumask);}/* }}} *//* {{{ proto int fpassthru(resource fp) Output all remaining data from a file pointer */PHPAPI PHP_FUNCTION(fpassthru){ zval **arg1; int size; php_stream *stream; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg1) == FAILURE) { WRONG_PARAM_COUNT; } PHP_STREAM_TO_ZVAL(stream, arg1); size = php_stream_passthru(stream); RETURN_LONG(size);}/* }}} *//* {{{ proto bool rename(string old_name, string new_name) Rename a file */PHP_FUNCTION(rename){ zval **old_arg, **new_arg; char *old_name, *new_name; int ret; if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &old_arg, &new_arg) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(old_arg); convert_to_string_ex(new_arg); old_name = Z_STRVAL_PP(old_arg); new_name = Z_STRVAL_PP(new_arg); if (PG(safe_mode) && (!php_checkuid(old_name, NULL, CHECKUID_CHECK_FILE_AND_DIR) || !php_checkuid(new_name, NULL, CHECKUID_CHECK_FILE_AND_DIR))) { RETURN_FALSE; } if (php_check_open_basedir(old_name TSRMLS_CC) || php_check_open_basedir(new_name TSRMLS_CC)) { RETURN_FALSE; } ret = VCWD_RENAME(old_name, new_name); if (ret == -1) {#ifdef EXDEV if (errno == EXDEV) { struct stat sb; if (php_copy_file(old_name, new_name TSRMLS_CC) == SUCCESS) { if (VCWD_STAT(old_name, &sb) == 0) {#if !defined(TSRM_WIN32) && !defined(NETWARE) if (VCWD_CHMOD(new_name, sb.st_mode)) { if (errno == EPERM) { php_error_docref2(NULL TSRMLS_CC, old_name, new_name, E_WARNING, "%s", strerror(errno)); VCWD_UNLINK(old_name); RETURN_TRUE; } php_error_docref2(NULL TSRMLS_CC, old_name, new_name, E_WARNING, "%s", strerror(errno)); RETURN_FALSE; } if (VCWD_CHOWN(new_name, sb.st_uid, sb.st_gid)) { if (errno == EPERM) { php_error_docref2(NULL TSRMLS_CC, old_name, new_name, E_WARNING, "%s", strerror(errno)); VCWD_UNLINK(old_name); RETURN_TRUE; } php_error_docref2(NULL TSRMLS_CC, old_name, new_name, E_WARNING, "%s", strerror(errno)); RETURN_FALSE; }#endif VCWD_UNLINK(old_name); RETURN_TRUE; } } }#endif php_error_docref2(NULL TSRMLS_CC, old_name, new_name, E_WARNING, "%s", strerror(errno)); RETURN_FALSE; } RETURN_TRUE;}/* }}} *//* {{{ proto bool unlink(string filename) Delete a file */PHP_FUNCTION(unlink){ zval **filename; int ret; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &filename) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(filename); if (PG(safe_mode) && !php_checkuid(Z_STRVAL_PP(filename), NULL, CHECKUID_CHECK_FILE_AND_DIR)) { RETURN_FALSE; } if (php_check_open_basedir(Z_STRVAL_PP(filename) TSRMLS_CC)) { RETURN_FALSE; } ret = VCWD_UNLINK(Z_STRVAL_PP(filename)); if (ret == -1) { php_error_docref1(NULL TSRMLS_CC, Z_STRVAL_PP(filename), E_WARNING, "%s", strerror(errno)); RETURN_FALSE; } /* Clear stat cache */ PHP_FN(clearstatcache)(INTERNAL_FUNCTION_PARAM_PASSTHRU); RETURN_TRUE;}/* }}} *//* {{{ proto int ftruncate(resource fp, int size) Truncate file to 'size' length */PHP_NAMED_FUNCTION(php_if_ftruncate){ zval **fp , **size;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -