本文整理汇总了PHP中CFile::createTemporary方法的典型用法代码示例。如果您正苦于以下问题:PHP CFile::createTemporary方法的具体用法?PHP CFile::createTemporary怎么用?PHP CFile::createTemporary使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CFile
的用法示例。
在下文中一共展示了CFile::createTemporary方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testClone
public function testClone()
{
$filePath0 = CFile::createTemporary();
$filePath1 = CFile::createTemporary();
$filePath2 = CFile::createTemporary();
$filePath3 = CFile::createTemporary();
CFile::write($filePath0, "The quick");
CFile::write($filePath1, " brown fox");
CFile::write($filePath2, " jumps over");
CFile::write($filePath3, " the lazy dog.");
$hash0 = new CHash(CHash::SHA256);
$hash0->computeMoreFromFile($filePath0);
$hash0->computeMoreFromFile($filePath1);
$hash0->computeMoreFromFile($filePath2);
$hash0->computeMoreFromFile($filePath3);
$hash1 = clone $hash0;
$hash1->computeMoreFromFile($filePath0);
$this->assertTrue($hash0->finalize()->equals("ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c"));
$this->assertTrue($hash1->finalize()->equals("14dc40e99be202c4e59a0c6d1a8854bb50253624080435ed8c65bd6e5e880c95"));
CFile::delete($filePath0);
CFile::delete($filePath1);
CFile::delete($filePath2);
CFile::delete($filePath3);
}
示例2: maybeUpdateThirdParty
//.........这里部分代码省略.........
if (CFile::exists($lastUpdateTimeFp)) {
$lastUpdateTime = new CTime(CString::toInt(CFile::read($lastUpdateTimeFp)));
$currTime = CTime::now();
if ($lastUpdateTime->isBefore($currTime)) {
$numDaysSinceLastUpdate = $currTime->diffInDays($lastUpdateTime);
if ($numDaysSinceLastUpdate < $minTimeBetweenDoUpdatesDays) {
// It is too early for updates yet.
return false;
}
} else {
assert('false', vs(isset($this), get_defined_vars()));
}
}
$date = CShell::currentDate();
CShell::say("Started on {$date}.");
if (isset($numDaysSinceLastUpdate)) {
CShell::say("It has been {$numDaysSinceLastUpdate} day(s) since last successful update.");
}
$concurrLockFp = CFilePath::add($thirdPartyDp, self::$ms_thirdPartyConcurrLockFn);
// Try locking the operation.
if (!self::setLock($concurrLockFp, false)) {
assert('false', vs(isset($this), get_defined_vars()));
CShell::onError(false, "Could not obtain a lock on the operation.");
CShell::writeToLog("\n");
return false;
}
$phpConfigNeedsReload = false;
$totalNumComponents = CMap::length($components);
$numComponentsUpdated = 0;
// The Browser Capabilities Project (BrowsCap).
if (CMap::hasKey($components, "browsCap")) {
$browsCap = $components["browsCap"];
$skip = $browsCap["skip"];
if (!$skip) {
CShell::say("Updating the Browser Capabilities Project (BrowsCap) ...");
$lookupFileUrl = $browsCap["lookupFileUrl"];
assert('!CString::isEmpty($lookupFileUrl)', vs(isset($this), get_defined_vars()));
// Component-related constants.
static $s_configOptName = "browscap";
static $s_lookupFileDownloadTimeoutSeconds = 120;
if (self::hasConfigOption($s_configOptName)) {
$browsCapLookupFp = CString::trim(self::configOption($s_configOptName));
if (!CString::isEmpty($browsCapLookupFp)) {
$browsCapDp = CFilePath::directory($browsCapLookupFp);
CShell::say("Downloading a BrowsCap lookup file from '{$lookupFileUrl}' ...");
$temporaryFp = CFile::createTemporary($browsCapDp);
$downloadRes = CInetRequest::downloadFile($lookupFileUrl, $temporaryFp, $s_lookupFileDownloadTimeoutSeconds);
if ($downloadRes) {
// After the file is downloaded into a temporary one, move it to the destination,
// safely replacing the existing file, if any.
CFile::move($temporaryFp, $browsCapLookupFp);
$numComponentsUpdated++;
$phpConfigNeedsReload = true;
$downloadedFileSizeKB = CUUnit::convertStoragef((double) CFile::size($browsCapLookupFp), CUUnit::BYTE, CUUnit::KILOBYTE);
$downloadedFileSizeKB = CMathf::round($downloadedFileSizeKB, 2);
CShell::say("Done. The downloaded file is {$downloadedFileSizeKB} KB in size.");
} else {
CShell::onError(false, "Could not download a BrowsCap lookup file from '{$lookupFileUrl}'.");
}
// Just in case, check for any temporary files that could have been left by any previous
// operations in the directory.
$leftoverFiles = CFile::findFiles(CFilePath::add($browsCapDp, CFile::DEFAULT_TEMPORARY_FILE_PREFIX . "*"));
if (!CArray::isEmpty($leftoverFiles)) {
// Cleanup the directory from the temporary files.
$len = CArray::length($leftoverFiles);
for ($i = 0; $i < $len; $i++) {
CFile::delete($leftoverFiles[$i]);
}
}
} else {
CShell::onError(false, "Could not read the value of '{$s_configOptName}' option " . "in the PHP CLI configuration file.");
}
} else {
CShell::onError(false, "Could not find '{$s_configOptName}' option in the PHP CLI configuration file.");
}
} else {
CShell::say("Skipping the Browser Capabilities Project (BrowsCap).");
}
}
// All the components have been processed. Unlock the operation.
self::unsetLock($concurrLockFp);
$date = CShell::currentDate();
if ($numComponentsUpdated != 0) {
// One or more third-party components have been updated. Put a time stamp on the directory where the
// components are located.
CFile::write($lastUpdateTimeFp, CString::fromInt(CTime::currentUTime()));
if ($numComponentsUpdated == $totalNumComponents) {
CShell::speak("Success. All {$totalNumComponents} third-party component(s)");
} else {
CShell::speak("Partial success. {$numComponentsUpdated} out of {$totalNumComponents} third-party component(s)");
}
CShell::say("have been updated. Completed on {$date}.");
} else {
CShell::say("No third-party components have been updated. Completed on {$date}.");
}
return $phpConfigNeedsReload;
} else {
return false;
}
}
示例3: setUploadData
/**
* For any request that needs to upload a file, specifies the data to be uploaded and, for HTTP requests, the
* file's metadata.
*
* For large data, `setUploadFile` method would be preferred to upload an already existing file.
*
* @param data $data The data to be uploaded.
* @param string $mimeType **OPTIONAL.** *Required for `HTTP_UPLOAD` requests only.* The MIME type of the file.
* @param string $fileName **OPTIONAL.** *Required for `HTTP_UPLOAD` requests only.* The custom name of the file.
* @param string $fileId **OPTIONAL.** *Required for `HTTP_UPLOAD` requests only.* The ID under which the file is
* to arrive to the destination server.
*
* @return void
*
* @link #method_setUploadFile setUploadFile
*/
public function setUploadData($data, $mimeType = null, $fileName = null, $fileId = null)
{
assert('is_cstring($data) && (!isset($mimeType) || is_cstring($mimeType)) && ' . '(!isset($fileName) || is_cstring($fileName)) && (!isset($fileId) || is_cstring($fileId))', vs(isset($this), get_defined_vars()));
$this->m_fileUploadTempFp = CFile::createTemporary();
CFile::write($this->m_fileUploadTempFp, $data);
$this->setUploadFile($this->m_fileUploadTempFp, $mimeType, $fileName, $fileId);
}
示例4: testIsPosPastEnd
public function testIsPosPastEnd()
{
$filePath = CFile::createTemporary();
CFile::write($filePath, "Hello");
$file = new CFile($filePath, CFile::READ);
$file->readBytes(5);
$this->assertFalse($file->isPosPastEnd());
$file->done();
// only needed in these tests
CFile::write($filePath, "Hello");
$file = new CFile($filePath, CFile::READ);
$file->setPosToEnd();
$this->assertFalse($file->isPosPastEnd());
$file->done();
// only needed in these tests
CFile::write($filePath, "Hello");
$file = new CFile($filePath, CFile::READ);
$file->readBytes(5);
$data = $file->readAvailableBytes(5);
$this->assertTrue($file->isPosPastEnd() && $data->equals(""));
$file->done();
// only needed in these tests
CFile::delete($filePath);
}
示例5: start
/**
* Starts a session by sending out the added requests.
*
* @param reference $success **OPTIONAL. OUTPUT.** After the method is called with this parameter provided, the
* parameter's value tells whether the session was successful.
*
* @return void
*/
public function start(&$success = null)
{
$success = true;
if ($this->m_hasError) {
$success = false;
return;
}
if (CArray::isEmpty($this->m_requestRecordsQueue)) {
// Nothing to do.
return;
}
// Current policy is to disable HTTP pipelining.
$res = curl_multi_setopt($this->m_multiCurl, CURLMOPT_PIPELINING, 0);
if (!$res) {
// Should never get in here as long as cURL options are being set correctly, hence the assertion.
assert('false', vs(isset($this), get_defined_vars()));
$this->m_hasError = true;
$this->m_errorMessage = "The 'curl_multi_setopt' function failed.";
$success = false;
$this->finalize();
return;
}
$anySuccessfulRequests = false;
// Disable the script's execution timeout before getting into the session.
$timeoutPause = new CTimeoutPause();
$numRunningRequests = 0;
// also the index of the next request to send
while (true) {
// From the request queue, add as many normal cURL handles to the multi cURL handle as it is allowed by the
// maximum number of concurrent requests, priorly setting internal options for every request.
while ($numRunningRequests < CArray::length($this->m_requestRecordsQueue) && $numRunningRequests < $this->m_maxNumConcurrentRequests) {
$requestRecord = $this->m_requestRecordsQueue[$numRunningRequests];
$request = $requestRecord[0];
$onCompleteCallback = $requestRecord[1];
$newCookieSession = $requestRecord[2];
$requestCurl = $request->curl();
// Set cURL options for the normal cURL handle, having created a temporary file for cookie storage if
// needed.
$requestSetOptSuccess;
if ($this->m_cookiesAreEnabled && $request->isHttp()) {
if (!isset($this->m_cookiesFp)) {
$this->m_cookiesFp = CFile::createTemporary();
}
$request->setInternalOptions($requestSetOptSuccess, $this->m_cookiesFp, $newCookieSession);
} else {
$request->setInternalOptions($requestSetOptSuccess);
}
if (!$requestSetOptSuccess) {
if (isset($onCompleteCallback)) {
call_user_func($onCompleteCallback, false, "", $request, $this);
}
CArray::remove($this->m_requestRecordsQueue, $numRunningRequests);
continue;
}
// Add the normal cURL handle to the multi cURL handle.
$res = curl_multi_add_handle($this->m_multiCurl, $requestCurl);
if ($res != 0) {
$this->m_hasError = true;
$curlError = curl_multi_strerror($res);
$this->m_errorMessage = is_cstring($curlError) && !CString::isEmpty($curlError) ? $curlError : "The 'curl_multi_add_handle' function failed.";
$success = false;
$timeoutPause->end();
$this->finalize();
return;
}
$numRunningRequests++;
}
if ($numRunningRequests == 0) {
break;
}
// Process the currently added requests until complete or no more data is available. Although
// `CURLM_CALL_MULTI_PERFORM` is deprecated since libcurl 7.20, keep it for compatibility reasons.
$numRunningTransfers;
do {
$multiExecRes = curl_multi_exec($this->m_multiCurl, $numRunningTransfers);
} while ($multiExecRes == CURLM_CALL_MULTI_PERFORM);
if ($multiExecRes != CURLM_OK) {
$this->m_hasError = true;
$curlError = curl_multi_strerror($multiExecRes);
$this->m_errorMessage = is_cstring($curlError) && !CString::isEmpty($curlError) ? $curlError : "The 'curl_multi_exec' function failed.";
$success = false;
$timeoutPause->end();
$this->finalize();
return;
}
// Check for completed requests, call the callback function for any completed one (if such a function is
// defined), finalize completed requests, and remove completed requests from the queue.
while (true) {
$completedRequestInfo = curl_multi_info_read($this->m_multiCurl);
if (!is_cmap($completedRequestInfo)) {
break;
}
//.........这里部分代码省略.........