本文整理汇总了PHP中wp_tempnam函数的典型用法代码示例。如果您正苦于以下问题:PHP wp_tempnam函数的具体用法?PHP wp_tempnam怎么用?PHP wp_tempnam使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了wp_tempnam函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: save_temp_image
/**
* Save the submitted image as a temporary file.
*
* @todo Revisit file handling.
*
* @param string $img Base64 encoded image.
* @return false|string File name on success, false on failure.
*/
protected function save_temp_image($img)
{
// Strip the "data:image/png;base64," part and decode the image.
$img = explode(',', $img);
$img = isset($img[1]) ? base64_decode($img[1]) : base64_decode($img[0]);
if (!$img) {
return false;
}
// Upload to tmp folder.
$filename = 'user-feedback-' . date('Y-m-d-H-i');
$tempfile = wp_tempnam($filename);
if (!$tempfile) {
return false;
}
// WordPress adds a .tmp file extension, but we want .png.
if (rename($tempfile, $filename . '.png')) {
$tempfile = $filename . '.png';
}
if (!WP_Filesystem(request_filesystem_credentials(''))) {
return false;
}
/**
* WordPress Filesystem API.
*
* @var \WP_Filesystem_Base $wp_filesystem
*/
global $wp_filesystem;
$success = $wp_filesystem->put_contents($tempfile, $img);
if (!$success) {
return false;
}
return $tempfile;
}
示例2: download_url
function download_url($url)
{
/*
http://core.trac.wordpress.org/browser/trunk/wp-admin/includes/file.php?rev=17928#L467
changes:
- wanted a timeout < 5 min
- SSL fails when trying to access github
*/
if (!$url) {
return new WP_Error('http_no_url', __('Invalid URL Provided.'));
}
$tmpfname = wp_tempnam($url);
if (!$tmpfname) {
return new WP_Error('http_no_file', __('Could not create Temporary file.'));
}
$handle = @fopen($tmpfname, 'wb');
if (!$handle) {
return new WP_Error('http_no_file', __('Could not create Temporary file.'));
}
// This! is the one line I wanted to get at
$response = wp_remote_get($url, array('sslverify' => false, 'timeout' => 30));
if (is_wp_error($response)) {
fclose($handle);
unlink($tmpfname);
return $response;
}
if ($response['response']['code'] != '200') {
fclose($handle);
unlink($tmpfname);
return new WP_Error('http_404', trim($response['response']['message']));
}
fwrite($handle, $response['body']);
fclose($handle);
return $tmpfname;
}
示例3: is_win_paths_exception
public function is_win_paths_exception($repository_id)
{
if (!isset($this->is_win_paths_exception[$repository_id])) {
$this->is_win_paths_exception[$repository_id] = false;
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$windows_max_path_length = 256;
$longest_path['wpml'] = 109;
$longest_path['toolset'] = 99;
$margin = 15;
$upgrade_path_length = strlen(WP_CONTENT_DIR . '/upgrade');
$installer_settings = WP_Installer()->settings;
if (is_array($installer_settings['repositories'][$repository_id]['data']['downloads']['plugins'])) {
$a_plugin = current($installer_settings['repositories'][$repository_id]['data']['downloads']['plugins']);
$url = WP_Installer()->append_site_key_to_download_url($a_plugin['url'], 'xxxxxx', $repository_id);
$tmpfname = wp_tempnam($url);
$tmpname_length = strlen(basename($tmpfname)) - 4;
// -.tmp
if ($upgrade_path_length + $tmpname_length + $longest_path[$repository_id] + $margin > $windows_max_path_length) {
$this->is_win_paths_exception[$repository_id] = true;
}
}
}
}
return $this->is_win_paths_exception[$repository_id];
}
示例4: synved_option_item_addon_install
function synved_option_item_addon_install($id, $name, $item)
{
$return = null;
$type = synved_option_item_type($item);
$target = synved_option_item_property($item, 'target');
$folder = synved_option_item_property($item, 'folder');
$field_name = synved_option_name_default($id);
$path = null;
if (file_exists($target)) {
$path = $target;
}
if ($type != 'addon' || $path == null) {
return false;
}
$path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
if (substr($path, -1) != DIRECTORY_SEPARATOR) {
$path .= DIRECTORY_SEPARATOR;
}
if (isset($_FILES[$field_name])) {
foreach ($_FILES[$field_name]["error"] as $key => $error) {
if ($key == $name && $error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES[$field_name]["tmp_name"][$key];
$name = $_FILES[$field_name]["name"][$key];
$tmpfname = wp_tempnam($name . '.zip');
if (move_uploaded_file($tmp_name, $tmpfname)) {
global $wp_filesystem;
$unzip_path = realpath($path);
$dirs = glob($path . '*', GLOB_ONLYDIR);
if ($wp_filesystem != null) {
$unzip_path = $wp_filesystem->find_folder($unzip_path);
}
wp_mkdir_p(realpath($path));
$return = unzip_file($tmpfname, $unzip_path);
if ($wp_filesystem != null) {
$wp_filesystem->delete($tmpfname);
}
$dirs_new = glob($path . '*', GLOB_ONLYDIR);
$dirs_diff = array_values(array_diff($dirs_new, $dirs));
$addon_path = $path;
if ($dirs_diff != null) {
$folder_path = null;
foreach ($dirs_diff as $dir) {
if (basename($dir) == $folder) {
$folder_path = $dir;
}
}
// XXX no correct path, was unzip successful?
if ($folder_path == null) {
$folder_path = $dirs_diff[0];
}
$addon_path = $folder_path;
}
synved_option_set($id, $name, $addon_path);
}
}
}
}
return $return;
}
示例5: dumpTable
private function dumpTable($table)
{
global $wpdb;
$fileName = wp_tempnam();
$handle = fopen($fileName, 'w');
$create = $wpdb->get_row("SHOW CREATE TABLE `{$table}`", ARRAY_N);
@fwrite($handle, "{$create[1]};\n\n");
$rowCount = $wpdb->get_var("SELECT Count(*) FROM `{$table}`");
if ($rowCount > $this->qryLimit) {
$batches = ceil($rowCount / $this->qryLimit);
} else {
if ($rowCount > 0) {
$batches = 1;
}
}
for ($i = 0; $i < $batches; $i++) {
$sql = "";
$limit = $i * $this->qryLimit;
$query = "SELECT * FROM `{$table}` LIMIT {$limit}, {$this->qryLimit}";
$rows = $wpdb->get_results($query, ARRAY_A);
if (is_array($rows)) {
foreach ($rows as $row) {
$sql .= "INSERT INTO `{$table}` VALUES(";
$values = array();
foreach ($row as $value) {
if (is_null($value) || !isset($value)) {
$values[] = 'NULL';
} else {
$values[] = '"' . @esc_sql($value) . '"';
}
}
$sql .= join(',', $values);
$sql .= ");\n";
}
fwrite($handle, $sql);
}
}
$sql = "\nSET FOREIGN_KEY_CHECKS = 1; \n\n";
fwrite($handle, $sql);
fclose($handle);
$zip = new \ZipArchive();
$zipFile = wp_tempnam();
$zip->open($zipFile, \ZipArchive::OVERWRITE);
$zip->addFile($fileName, 'sql');
$zip->close();
unlink($fileName);
return $zipFile;
}
示例6: download_wpcom_theme_to_file
protected static function download_wpcom_theme_to_file($theme)
{
$wpcom_theme_slug = preg_replace('/-wpcom$/', '', $theme);
$file = wp_tempnam('theme');
if (!$file) {
return new WP_Error('problem_creating_theme_file', __('Problem creating file for theme download', 'jetpack'));
}
$url = "themes/download/{$theme}.zip";
$args = array('stream' => true, 'filename' => $file);
$result = Jetpack_Client::wpcom_json_api_request_as_blog($url, '1.1', $args);
$response = $result['response'];
if ($response['code'] !== 200) {
unlink($file);
return new WP_Error('problem_fetching_theme', __('Problem downloading theme', 'jetpack'));
}
return $file;
}
示例7: download_url
function download_url($url, $timeout = 300)
{
//WARNING: The file is not automatically deleted, The script must unlink() the file.
if (!$url) {
return new WP_Error('http_no_url', __('Invalid URL Provided.', 'themify-flow'));
}
$tmpfname = wp_tempnam($url);
if (!$tmpfname) {
return new WP_Error('http_no_file', __('Could not create Temporary file.', 'themify-flow'));
}
$response = wp_safe_remote_get($url, array('cookies' => $this->cookies, 'timeout' => $timeout, 'stream' => true, 'filename' => $tmpfname));
if (is_wp_error($response)) {
unlink($tmpfname);
return $response;
}
if (200 != wp_remote_retrieve_response_code($response)) {
unlink($tmpfname);
return new WP_Error('http_404', trim(wp_remote_retrieve_response_message($response)));
}
return $tmpfname;
}
示例8: wpqi_download_url
/**
* Downloads a url to a local file using the Snoopy HTTP Class.
*
* @since unknown
* @todo Transition over to using the new HTTP Request API (jacob).
*
* @param string $url the URL of the file to download
* @return mixed WP_Error on failure, string Filename on success.
*/
function wpqi_download_url($url)
{
//WARNING: The file is not automatically deleted, The script must unlink() the file.
if (!$url) {
return new WP_Error('http_no_url', __('Invalid URL Provided'));
}
$tmpfname = wp_tempnam($url);
if (!$tmpfname) {
return new WP_Error('http_no_file', __('Could not create Temporary file'));
}
$response = wp_remote_get($url, array('timeout' => 60, 'stream' => true, 'filename' => $tmpfname));
if (is_wp_error($response)) {
unlink($tmpfname);
return $response;
}
if ($response['response']['code'] != 200) {
unlink($tmpfname);
return new WP_Error('http_404', trim($response['response']['message']));
}
return array($tmpfname, $response);
}
示例9: download
protected static function download($url, $timeout = 300)
{
//WARNING: The file is not automatically deleted, The script must unlink() the file.
if (!$url) {
return new WP_Error('http_no_url', __('Invalid URL Provided.', 'wp-e-commerce'));
}
$tmpfname = wp_tempnam($url);
if (!$tmpfname) {
return new WP_Error('http_no_file', __('Could not create Temporary file.', 'wp-e-commerce'));
}
$args = array('timeout' => $timeout, 'stream' => true, 'filename' => $tmpfname, 'headers' => array('X-WP-Domain' => Sputnik_API::domain()), 'user-agent' => 'WP eCommerce Marketplace: ' . WPSC_VERSION);
Sputnik_API::sign_download($url, $args);
$response = wp_safe_remote_get($url, $args);
if (is_wp_error($response)) {
unlink($tmpfname);
return $response;
}
if (200 != wp_remote_retrieve_response_code($response)) {
unlink($tmpfname);
return new WP_Error('http_404', trim(wp_remote_retrieve_response_message($response)));
}
return $tmpfname;
}
示例10: download_url
/**
* Downloads a url to a local temporary file using the WordPress HTTP Class.
* Please note, That the calling function must unlink() the file.
*
* @since 2.5.0
*
* @param string $url the URL of the file to download
* @param int $timeout The timeout for the request to download the file default 300 seconds
* @return mixed WP_Error on failure, string Filename on success.
*/
function download_url( $url, $timeout = 300 ) {
//WARNING: The file is not automatically deleted, The script must unlink() the file.
if ( ! $url )
return new WP_Error('http_no_url', __('Invalid URL Provided.'));
$tmpfname = wp_tempnam($url);
if ( ! $tmpfname )
return new WP_Error('http_no_file', __('Could not create Temporary file.'));
$response = wp_safe_remote_get( $url, array( 'timeout' => $timeout, 'stream' => true, 'filename' => $tmpfname ) );
if ( is_wp_error( $response ) ) {
unlink( $tmpfname );
return $response;
}
if ( 200 != wp_remote_retrieve_response_code( $response ) ){
unlink( $tmpfname );
return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ) );
}
$content_md5 = wp_remote_retrieve_header( $response, 'content-md5' );
if ( $content_md5 ) {
$md5_check = verify_file_md5( $tmpfname, $content_md5 );
if ( is_wp_error( $md5_check ) ) {
unlink( $tmpfname );
return $md5_check;
}
}
return $tmpfname;
}
示例11: put_contents
/**
* @param string $file
* @param string $contents
* @param bool|int $mode
* @return bool
*/
public function put_contents($file, $contents, $mode = false)
{
$tempfile = wp_tempnam($file);
$temp = fopen($tempfile, 'wb+');
if (!$temp) {
return false;
}
mbstring_binary_safe_encoding();
$data_length = strlen($contents);
$bytes_written = fwrite($temp, $contents);
reset_mbstring_encoding();
if ($data_length !== $bytes_written) {
fclose($temp);
unlink($tempfile);
return false;
}
fseek($temp, 0);
// Skip back to the start of the file being written to
$ret = @ftp_fput($this->link, $file, $temp, FTP_BINARY);
fclose($temp);
unlink($tempfile);
$this->chmod($file, $mode);
return $ret;
}
示例12: put_contents
function put_contents($file, $contents, $type = '')
{
if (empty($type)) {
$type = $this->is_binary($contents) ? FTP_BINARY : FTP_ASCII;
}
$this->ftp->SetType($type);
$temp = wp_tempnam($file);
if (!($temphandle = fopen($temp, 'w+'))) {
unlink($temp);
return false;
}
fwrite($temphandle, $contents);
fseek($temphandle, 0);
//Skip back to the start of the file being written to
$ret = $this->ftp->fput($file, $temphandle);
fclose($temphandle);
unlink($temp);
return $ret;
}
示例13: upload_from_data
/**
* Handles an upload via raw POST data.
*
* @since 4.7.0
* @access protected
*
* @param array $data Supplied file data.
* @param array $headers HTTP headers from the request.
* @return array|WP_Error Data from wp_handle_sideload().
*/
protected function upload_from_data($data, $headers)
{
if (empty($data)) {
return new WP_Error('rest_upload_no_data', __('No data supplied.'), array('status' => 400));
}
if (empty($headers['content_type'])) {
return new WP_Error('rest_upload_no_content_type', __('No Content-Type supplied.'), array('status' => 400));
}
if (empty($headers['content_disposition'])) {
return new WP_Error('rest_upload_no_content_disposition', __('No Content-Disposition supplied.'), array('status' => 400));
}
$filename = self::get_filename_from_disposition($headers['content_disposition']);
if (empty($filename)) {
return new WP_Error('rest_upload_invalid_disposition', __('Invalid Content-Disposition supplied. Content-Disposition needs to be formatted as `attachment; filename="image.png"` or similar.'), array('status' => 400));
}
if (!empty($headers['content_md5'])) {
$content_md5 = array_shift($headers['content_md5']);
$expected = trim($content_md5);
$actual = md5($data);
if ($expected !== $actual) {
return new WP_Error('rest_upload_hash_mismatch', __('Content hash did not match expected.'), array('status' => 412));
}
}
// Get the content-type.
$type = array_shift($headers['content_type']);
/** Include admin functions to get access to wp_tempnam() and wp_handle_sideload() */
require_once ABSPATH . 'wp-admin/includes/admin.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));
}
fwrite($fp, $data);
fclose($fp);
// Now, sideload it in.
$file_data = array('error' => null, 'tmp_name' => $tmpfname, 'name' => $filename, 'type' => $type);
$overrides = array('test_form' => false);
$sideloaded = wp_handle_sideload($file_data, $overrides);
if (isset($sideloaded['error'])) {
@unlink($tmpfname);
return new WP_Error('rest_upload_sideload_error', $sideloaded['error'], array('status' => 500));
}
return $sideloaded;
}
示例14: launch_editor_for_input
/**
* Launch system's $EDITOR to edit text
*
* @param [string] $input Text to be put into the temp file for changing
* @param [string] $title Name for the temporary file
* @return [string] $output Output string if input has changed, false otherwise
*/
function launch_editor_for_input($input, $title = 'Terminus')
{
$tmpfile = wp_tempnam($title);
if (!$tmpfile) {
\Terminus::error('Error creating temporary file.');
}
$output = '';
file_put_contents($tmpfile, $input);
$editor = getenv('EDITOR');
if (!$editor) {
if (isset($_SERVER['OS']) && strpos($_SERVER['OS'], 'indows') !== false) {
$editor = 'notepad';
} else {
$editor = 'vi';
}
}
\Terminus::launch("{$editor} " . escapeshellarg($tmpfile));
$output = file_get_contents($tmpfile);
unlink($tmpfile);
if ($output == $input) {
return false;
}
return $output;
}
示例15: get_google_drive_backup
function get_google_drive_backup($args)
{
require_once $GLOBALS['iwp_mmb_plugin_dir'] . '/lib/Google/Client.php';
require_once $GLOBALS['iwp_mmb_plugin_dir'] . '/lib/Google/Service/Drive.php';
//refresh token
$client = new IWP_google_Client();
$client->setClientId($args['clientID']);
$client->setClientSecret($args['clientSecretKey']);
$client->setRedirectUri($args['redirectURL']);
$client->setScopes(array('https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile'));
//$client->setUseObjects(true);
$accessToken = $args['token'];
$refreshToken = $accessToken['refresh_token'];
try {
$client->refreshToken($refreshToken);
} catch (Exception $e) {
echo 'google Error ', $e->getMessage(), "\n";
return array("error" => $e->getMessage(), "error_code" => "google_error_refresh_token");
}
//downloading the file
$service = new IWP_google_Service_Drive($client);
$file = $service->files->get($args['backup_file']);
$downloadUrl = $file->getDownloadUrl();
$temp = wp_tempnam('iwp_temp_backup.zip');
try {
if ($downloadUrl) {
$request = new IWP_google_Http_Request($downloadUrl, 'GET', null, null);
$signHttpRequest = $client->getAuth()->sign($request);
$httpRequest = $client->getIo()->makeRequest($signHttpRequest);
if ($httpRequest->getResponseHttpCode() == 200) {
file_put_contents($temp, $httpRequest->getResponseBody());
return $temp;
} else {
// An error occurred.
return array("error" => "There is some error.", "error_code" => "google_error_bad_response_code");
}
} else {
// The file doesn't have any content stored on Drive.
return array("error" => "Google Drive file doesnt have nay content.", "error_code" => "google_error_download_url");
}
} catch (Exception $e) {
echo 'google Error ', $e->getMessage(), "\n";
return array("error" => $e->getMessage(), "error_code" => "google_error_download_url_catch_excep");
}
}