wp_tempnam example

// Re-generate attachment metadata since it was previously generated for a different theme.                         $metadata = wp_generate_attachment_metadata( $attachment_post->ID, $attached_file );
                        wp_update_attachment_metadata( $attachment_id$metadata );
                        update_post_meta( $attachment_id, '_starter_content_theme', $this->get_stylesheet() );
                    }
                }

                // Insert the attachment auto-draft because it doesn't yet exist or the attached file is gone.                 if ( ! $attachment_id ) {

                    // Copy file to temp location so that original file won't get deleted from theme after sideloading.                     $temp_file_name = wp_tempnam( wp_basename( $file_path ) );
                    if ( $temp_file_name && copy( $file_path$temp_file_name ) ) {
                        $file_array['tmp_name'] = $temp_file_name;
                    }
                    if ( empty( $file_array['tmp_name'] ) ) {
                        continue;
                    }

                    $attachment_post_data = array_merge(
                        wp_array_slice_assoc( $attachment, array( 'post_title', 'post_content', 'post_excerpt' ) ),
                        array(
                            'post_status' => 'auto-draft', // So attachment will be garbage collected in a week if changeset is never published.

            }
        }

        // Get the content-type.         $type = array_shift( $headers['content_type'] );

        // Include filesystem functions to get access to wp_tempnam() and wp_handle_sideload().         require_once ABSPATH . 'wp-admin/includes/file.php';

        // Save the file.         $tmpfname = wp_tempnam( $filename );

        $fp = fopen( $tmpfname, 'w+' );

        if ( ! $fp ) {
            return new WP_Error(
                'rest_upload_file_error',
                __( 'Could not open file handle.' ),
                array( 'status' => 500 )
            );
        }

        
/** * Reads entire file into a string. * * @since 2.5.0 * * @param string $file Name of the file to read. * @return string|false Read data on success, false if no temporary file could be opened, * or if the file couldn't be retrieved. */
    public function get_contents( $file ) {
        $tempfile   = wp_tempnam( $file );
        $temphandle = fopen( $tempfile, 'w+' );

        if ( ! $temphandle ) {
            unlink( $tempfile );
            return false;
        }

        if ( ! ftp_fget( $this->link, $temphandle$file, FTP_BINARY ) ) {
            fclose( $temphandle );
            unlink( $tempfile );
            return false;
        }
if ( empty( $filename ) || in_array( $filename, array( '.', '/', '\\' ), true ) ) {
        $filename = uniqid();
    }

    // Use the basename of the given file without the extension as the name for the temporary directory.     $temp_filename = basename( $filename );
    $temp_filename = preg_replace( '|\.[^.]*$|', '', $temp_filename );

    // If the folder is falsey, use its parent directory name instead.     if ( ! $temp_filename ) {
        return wp_tempnam( dirname( $filename )$dir );
    }

    // Suffix some random data to avoid filename conflicts.     $temp_filename .= '-' . wp_generate_password( 6, false );
    $temp_filename .= '.tmp';
    $temp_filename  = wp_unique_filename( $dir$temp_filename );

    /* * Filesystems typically have a limit of 255 characters for a filename. * * If the generated unique filename exceeds this, truncate the initial * filename and try again. * * As it's possible that the truncated filename may exist, producing a * suffix of "-1" or "-10" which could exceed the limit again, truncate * it to 252 instead. */

    public function get_contents( $file ) {
        if ( ! $this->exists( $file ) ) {
            return false;
        }

        $tempfile   = wp_tempnam( $file );
        $temphandle = fopen( $tempfile, 'w+' );

        if ( ! $temphandle ) {
            unlink( $tempfile );
            return false;
        }

        mbstring_binary_safe_encoding();

        if ( ! $this->ftp->fget( $temphandle$file ) ) {
            fclose( $temphandle );
            
Home | Imprint | This part of the site doesn't use cookies.