如何写入文本文件并将其保存在文件夹中以备日后下载?

时间:2020-02-26 作者:Sid

“templates”文件夹中有一个模板文件,它根据从外部源接收的JSON输入执行一个函数。

我想将JSON输入写入一个文本文件,并将其保存在某个地方。然后以后可以通过浏览器URL hit通过另一个页面访问它。

我的问题是,我不知道这个文本文件应该存储在哪里,我对这种方法的安全性表示怀疑。注意:这不是一个插件,它只是一个模板php文件,当加载到浏览器URL中时可以执行某些操作。

1 个回复
SO网友:Zeth

汤姆的观点很有道理。我不建议任何人这样做。

然而以下是我之前的做法;-)

不过,它的范围相当广泛。

步骤1-在盒子上切一个洞,添加一个名为“自定义上载文件”的文件。php”,并将其添加到WordPress主题文件夹中:

<?php
// Getting the header to get WordPress-functions available
require_once( $_SERVER[\'DOCUMENT_ROOT\'] . \'/wp-load.php\' );

$errors       = [];
$confirmation = [];
$post_id      = \'\';

if( is_user_logged_in() ):

  // Check that post_id is set
  if( ! empty( $_POST[\'post_id\'] ) ):
    $post_id = $_POST[\'post_id\'];

    $docs_folder = $_SERVER[\'DOCUMENT_ROOT\'] . \'/wp-content/uploads/custom-uploaded-files\';
    // Check that the doc-folder exists
    if( file_exists( $docs_folder ) ):

      // Create the folder, if it doesn\'t exist
      if( ! file_exists( $docs_folder . \'/post\' . $post_id ) ):
        $folder_created = mkdir( $docs_folder . \'/post\' . $post_id );
        if( ! $folder_created ):
          $errors[] = \'Destination folder didnt exists and couldnt be created.\';
        endif; // if( $folder_created ):
      endif; // if( file_exists( $docs_folder . \'/post\' . $post_id ) ):

      // Check that the folder exists (which it always will, now)
      if( file_exists( $docs_folder . \'/post\' . $post_id ) ):

        // Check that the file aren\'t empty
        if( ! empty( $_FILES[\'files\'][\'name\'][0] ) ):
          $sanitized_filename = filter_var( $_FILES[\'files\'][\'name\'][0], FILTER_SANITIZE_URL );
          $approved_formats   = array(
            \'jpg\',
            \'jpeg\',
            \'png\',
            \'gif\',
            //          \'mov\',
            //          \'avi\',
            \'mpg\',
            //          \'3gp\',
            //          \'3g2\',
            //          \'midi\',
            //          \'mid\',
            \'pdf\',
            \'doc\',
            \'ppt\',
            \'odt\',
            \'pptx\',
            \'docx\',
            //          \'pps\',
            //          \'ppsx\',
            \'xls\',
            \'xlsx\',
            //          \'key\',
            //          \'mp3\',
            //          \'ogg\',
            //          \'flac\',
            //          \'m4a\',
            //          \'wav\',
            //          \'mp4\',
            //          \'m4v\',
            //          \'webm\',
            //          \'ogv\',
            //          \'flv\'
          );

          $fileinfo  = pathinfo( $sanitized_filename );
          $extension = $fileinfo[\'extension\'];

          if( in_array( $extension, $approved_formats ) ):

            // Determining the filename
            $counter                      = 1;
            $file_name_path_not_available = true;
            $filename                     = $fileinfo[\'filename\'];
            $filename_modification        = \'\';
            while( $file_name_path_not_available ):
              if( file_exists( $docs_folder . \'/post\' . $post_id . \'/\' . $filename . $filename_modification . \'.\' . $extension ) ):
                $filename_modification = \'-\' . $counter;
                $counter ++;
              else:
                $file_name_path_not_available = false;
              endif; // if( file_exists( $docs_folder . \'/post\' . $post_id . \'/\' . $fileinfo[\'basename\'] ) ):
            endwhile; // while( $file_name_path_not_available ):

            // Saving the file
            $file_uploaded = move_uploaded_file( $_FILES[\'files\'][\'tmp_name\'][0], $docs_folder . \'/post\' . $post_id . \'/\' . $filename . $filename_modification . \'.\' . $extension );
            if( $file_uploaded ):
              $file_info_array = array(
                \'uploaded_by\' => get_current_user_id(),
                \'file_name\' => $filename . $filename_modification . \'.\' . $extension
              );
              add_post_meta( $post_id, \'fileupload\', serialize( $file_info_array ) );
              $confirmation[] = \'Filen blev gemt.\';
            else:
              $errors[] = \'Error. File was not saved.\';
            endif; // if( $file_saved ):

          else:
            $errors[] = \'Error. The format of the uploaded file wasnt allowed. Please try another format.\';
          endif; // if( in_array( $extension, $approved_formats ) ):

        else:
          $errors[] = \'No file was selected.\';
        endif; // if( !empty( $_POST[\'file\'] ) ):
      endif; // if( file_exists( $docs_folder . \'/post\' . $post_id ) ):

    else:
      $errors[] = \'The destination-folder didnt exists and couldnt be created.\';
    endif; // if( file_exists( $folder ) ):

  else:
    $errors[] = "The Post-ID was empty, so it couldnt be determined where it should be saved.";
  endif; //  if( !empty( $_POST[\'post_id\'] ) ):

else:
  $errors[] = "You are not logged in.";
endif; // if( ! is_user_logged_in() ):

$_SESSION[\'display_messages\']          = true;
$_SESSION[\'upload_errors\']       = $errors;
$_SESSION[\'upload_confirmation\'] = $confirmation;

header( \'Location: \' . $_SERVER[\'HTTP_REFERER\'] );
exit();
?>
它可以检查很多东西。我试着添加了一些好的评论。

步骤2-将垃圾放入框中,现在在主题文件夹中创建一个自定义页面模板并调用它page-upload-file.php. 然后将其放入该文件中:

<?php
/**
 * Template Name: Upload file template
 */

get_header(); ?>

<main>

  <div class="container-fluid">
    <div class="container">
      <div class="row">
        <div class="col-8">

          <h1>
            <?php the_title(); ?>
          </h1>

          <div class="entry-content">
            <?php the_content(); ?>
          </div>

        </div>
        <!-- /.col-8 -->

        <div class="col-4">
          <form action="<?php echo get_stylesheet_directory_uri() . \'/custom-upload-file.php\'; ?>" method="post" enctype="multipart/form-data">
            <input class="upload__file-button" type="file" name="files[]">
            <!-- /.upload__files-label -->
            <input type="hidden" name="post_id" value="<?php echo get_the_id(); ?>">
            <input type="submit" value="Upload">
          </form>
        </div>
        <!-- /.col-4 -->

      </div>
      <!-- /.row -->
    </div>
    <!-- /.container -->
  </div>
  <!-- /.container-fluid -->

</main>

<?php get_footer(); ?>
第3步-让她打开盒子,然后创建新页面并设置新页面模板。并尝试一下。

虽然我还没有对其进行测试,但代码是从以前的生产解决方案中提取和修改的。所以我知道我在某个时候成功了。

相关推荐

$wp_FILESYSTEM返回空。依赖关系是什么?

我需要获取对$wp\\u filesystem对象的引用。在下面的测试中,var\\u dump($wp\\u filesystem)返回NULL。要正确设置$wp\\U文件系统,还需要哪些其他文件?我一直在期待,因为它在文件中调用。php,加载该文件就足以加载该对象。<?php require(\'../../../wp-blog-header.php\'); require(\'../../../wp-admin/includes/file.php\'); $m