function actionDefault() { // Get the file object for the current file $file = new YDFSFile(__FILE__); // Dump the object echo __FILE__; YDDebugUtil::dump($file); // Dump the object echo '<br>Basename: ' . $file->getBasename(); echo '<br>Extension: ' . $file->getExtension(); echo '<br>Path: ' . $file->getPath(); echo '<br>LastModified: ' . $file->getLastModified(); echo '<br>File size: ' . $file->getSize(); // Get the contents YDDebugUtil::dump($file->getContents(), '$file->getContents()'); // Get the partial contents YDDebugUtil::dump($file->getContents(2, 3), '$file->getContents( 2, 3 )'); // Create a dummy file $dir = new YDFSDirectory('.'); $file = $dir->createFile('dummy.txt', 'initial contents'); // Update the contents $file->setContents('new contents'); // Get the contents YDDebugUtil::dump($file->getContents(), '$file->getContents() after update'); // Append the contents $file->setContents(YD_CRLF . 'appended contents', true); // Get the contents YDDebugUtil::dump($file->getContents(), '$file->getContents() after append'); // Delete the file $file->delete(); // Get the file object for the current file $file = new YDFSFile('nofile.php'); }
function actionAdd() { // We return to the default action $action = 'default'; // Process the form if ($this->form->validate()) { // Move the uploaded file $file = $this->form->getElement('image'); if ($file->isUploaded()) { // Get the new filename $filename = YDStringUtil::stripSpecialCharacters($file->getBaseName()); // Move the upload if (!is_dir($this->dir_rel)) { @mkdir($this->dir_rel); } @$file->moveUpload($this->dir_rel, $filename); // Convert it to an object $fileObj = new YDFSFile($this->dir_rel . $file->getBaseName()); // Check if it's a ZIP file if (strtolower($fileObj->getExtension() == 'zip')) { // Include the unzip library include YD_DIR_HOME . '/3rdparty/zip/unzip.lib.php'; // Convert it to a ZIP object $zip = new SimpleUnzip($fileObj->getAbsolutePath()); // Get the directory as a path $dir = new YDFSDirectory($this->dir_rel); // Extract the images foreach ($zip->Entries as $entry) { // Save it as a filee $entryFile = $dir->createFile($entry->Name, $entry->Data); // Delete it if it's not an image if (!$entryFile->isImage()) { @unlink($entryFile->getAbsolutePath()); } else { $entryFile = $this->weblog->resizeUploadedImage($entryFile); } } } // Check if it's an image if (!$fileObj->isImage()) { @unlink($fileObj->getAbsolutePath()); } else { $fileObj = $this->weblog->resizeUploadedImage($fileObj); } // Delete the thumbnails @unlink($this->dir_rel . 's_' . $file->getBaseName()); @unlink($this->dir_rel . 'm_' . $file->getBaseName()); } } // Redirect to the list view $this->redirect(YD_SELF_SCRIPT . '?id=' . $this->item['id']); }
/** * The class constructor of the YDFSFile class takes the path to the file as it's first argument. * It will then provide you with a number of functions to get the properties of the file. * * @param $path Path of the file. * @param $create (optional) Force the creation of the file if it doesn't exist. Default: false. */ function YDFSFile($path, $create = false) { // Initialize YDBase $this->YDBase(); // Check if the path if the file exists if (!is_file($path)) { // Check if the file should be created if ($create) { // Create a new YDFSDirectory object $dir = new YDFSDirectory(dirname($path)); // Create the file $dir->createFile(basename($path), ' '); } else { trigger_error('The file with path "' . $path . '" does not exist.', YD_ERROR); } } // Save the path $this->_path = realpath($path); }
/** * This function will save the XML data to the specified file. * * @remark * The default format is "RSS2.0". If you specify no argument indicating the requested format, the "RSS2.0" * format will be used. * * @param $path The path to save the XML data to. * @param $format (optional) The format in which the items should be converted. */ function saveXml($path, $format = 'RSS2.0') { // Get the XML data $xml = $this->toXml($format); // Get the directory information $dir = new YDFSDirectory(YDPath::getDirectoryName($path)); // Create the file $dir->createFile(YDPath::getFileName($path), $xml); }
/** * Function to get the contents of the URL. It will get the contents using Gzip compression if possible in * order to save bandwidth. It uses the HTTP Client class from Simon Willison to do the dirty work. * * More information about the HTTP client class can be found on: http://scripts.incutio.com/httpclient/ * * If it fails to retrieve the data, it will raise a fatal error. * * By default, it will cache the downloaded data based on the etag and last-modified headers. The cache files * are stored in the temp directory of the Yellow Duck framework and have the extension "wch". You can delete * these automatically as they will be recreated on the fly if needed. * * For configuring the cache, there are two configuration variables you can redefine if needed: * YD_HTTP_CACHE_TIMEOUT: the lifetime of the cache in seconds (default: 3600). * YD_HTTP_CACHE_USEHEAD: if a HEAD HTTP request should be used to verify the cache validity (default: 1). * * @param $cache (optional) Indicate if the web content should be cached or not. By default, caching is * turned on. * @param $fail (optional) Whether to fail or not if the contents cannot be downloaded. Defaults to true. * * @returns Returns the contents of the URL. */ function getContents($cache = true, $fail = true) { // Check if caching is enabled $cacheFName = null; // Check the cache if ($cache == true) { // Include the filesystem library include_once YD_DIR_HOME_CLS . '/YDFileSystem.php'; // Check if we need to use the HTTP HEAD function if (YDConfig::get('YD_HTTP_CACHE_USEHEAD') == 1) { // Get the headers $headers = $this->getHeaders(); // Check if we have etag or last modified if (isset($headers['etag']) || isset($headers['last-modified'])) { $cacheFName = $this->getUrl(); if (isset($headers['etag'])) { $cacheFName .= $headers['etag']; } if (isset($headers['last-modified'])) { $cacheFName .= $headers['last-modified']; } if (isset($headers['content-length'])) { $cacheFName .= $headers['content-length']; } $cacheFName = YD_TMP_PRE . 'W_' . md5($cacheFName) . '.wch'; $cacheFName = YD_DIR_TEMP . '/' . $cacheFName; } } // If the cache filename is null, use the default one if ($cacheFName == null) { $cacheFName = YD_DIR_TEMP . '/' . YD_TMP_PRE . 'W_' . md5($this->getUrl()) . '.wch'; } // Use the cache file if any if (is_file($cacheFName)) { $file = new YDFSFile($cacheFName); $cacheValidTime = $file->getLastModified() + YDConfig::get('YD_HTTP_CACHE_TIMEOUT'); if (time() < $cacheValidTime) { return $file->getContents(); } } } // Create a new HTTP client $client = $this->_getHttpClient(); // Now send the request $result = @$client->doRequest(); // Check if there was a result if ($result == false) { if ($fail) { trigger_error('Failed to retrieve the data from the url "' . $this->getUrl() . '". ' . $client->getError(), YD_ERROR); } else { return false; } } else { $data = @$client->getContent(); } // Check if caching is enabled if ($cache == true) { // Save the cached data if ($cacheFName != null) { $dir = new YDFSDirectory(YD_DIR_TEMP); $dir->createFile($cacheFName, $data); } } // Return the data return $data; }