I would like to scan a folder, sort the files within that folder by modified time, and display the most recent file. Here's what I have so far:
<?php
function scanDir ($dir){
$fileTimeArray = array();
// Scan directory and get each file date
foreach (scandir($dir) as $fileTime){
$fileTimeArray[$fileTime] = filemtime($dir . '/' . $fileTime);
}
//Sort file times
$fileTimeArray = arsort($fileTimeArray);
return($fileTimeArray[0]);
}
?>
I'm calling this function in another php file, within the src of an img tag.
As of now
<img src=Array>
Where am I going wrong within the function? Thank you!!!
Related
I want to display a download link inside a WordPress widget. The file to be downloaded is located in the download subfolder of the site root, so that it can be uploaded via FTP. The name of the file and the text to be displayed for the download link shall be stored in a simple text file in the same folder.
Assuming WordPress is installed on www.mysite.com. The file name is setup_1_0.zip and the link display is Setup 1.0.
I am open to the file format how this information is stored as long as I can upload that file via FTP, too.
How can I embed this information inside a Custom HTML widget to get a valid download link with the text taken from that file?
How to automate the process of uploading latest software's build and download link creation in WordPress?
Based on your logic.
You are trying to automate the download process of your latest software version.
You don't want to update things manually and you just want to upload your latest build in the /download/ folder. (Only drop your latest version using FTP; that's all)
This is how I would do it:
Referencing those questions:
Get the latest file addition in a directory
How to force file download with PHP
I propose two solutions: First two separte codes, Second One inline code.
Just for educational purpose
First solution: Quick and short usage:
(You might need a way or a plugin to activate running PHP in Widget; this plugin helps PHP Code Widget)
<?php
$path = "download/";
$latest_ctime = 0;
$latest_filename = '';
$d = dir($path);
while (false !== ($entry = $d->read())) {
$filepath = "{$path}/{$entry}";
// could do also other checks than just checking whether the entry is a file
if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
$latest_ctime = filectime($filepath);
$latest_filename = $entry;
}
}
echo 'Download '. $latest_filename . '';
?>
Second solution:
(Again, you might need a way or a plugin to activate running PHP in Widget; this plugin helps PHP Code Widget)
A) Create download.php in http://www.example.com/download.php
Add the following code:
<?php
$path = "download";
$latest_ctime = 0; //ctime stands for creation time.
$latest_filename = '';
$d = dir($path);
while (false !== ($entry = $d->read())) {
$filepath = "{$path}/{$entry}";
// could do also other checks than just checking whether the entry is a file
if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
$latest_ctime = filectime($filepath);
$latest_filename = $entry;
}
}
// echo $latest_filename; un-comment to debug
$file_url = 'http://www.example.com/download/'.$latest_filename;
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url); // do the double-download-dance (dirty but worky)
?>
B) in your WordPress HTML Widget add the following code
<?php
$path = "download";
$latest_ctime = 0;
$latest_filename = '';
$d = dir($path);
while (false !== ($entry = $d->read())) {
$filepath = "{$path}/{$entry}";
// could do also other checks than just checking whether the entry is a file
if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
$latest_ctime = filectime($filepath);
$latest_filename = $entry;
}
}
echo 'Download '. $latest_filename . '';
?>
Further explanation:
A) is responsiple for downloading the latest software build automatically.
B) is responsiple for displaying Latest build name and Creating the link.
Now, You only need to upload one file to your /download/ folder which is your latest build (setup_1_0.zip, setup_1_1.zip, setup_1_2.zip ...etc. The proposed solution will check creation date regardless of file's name.)
Important note: You can see that the latest file checker function is repeated twice; once in download.php and once in WordPress Widget. Because if we combine in one file we will get header already sent error.
Dose this answer your question please? Kindly feedback.
In mediawiki, I can show a link to a file using:
[[Media:File.pdf|A file]]
Results in
A file
But how can I get the the last modified date for an uploaded file? I would like to show the timestamp alongside that link, rather than having to go to the file page for it. Is there a way to do this?
A file [Timestamp]
Does this require an extension of some sort? I am unable to find any documentation on getting metadata for uploaded files.
It would require custom logic, yes (which is normally packaged into extensions but in simple cases you can just add it directly to your config file). You can use the HtmlPageLinkRendererEnd hook for example:
global $wgHooks;
$wgHooks['HtmlPageLinkRendererEnd'][] = function(
LinkRenderer $linkRenderer, LinkTarget $target,
$isKnown, &$text, &$attribs, &$ret
) {
if ( $linkTarget->inNamespace( NS_FILE ) ) {
$file = wfFindFile( $linkTarget->getText() );
if ( $file && $file->exists() && $file->isLocal() ) {
$text .= ' ' . $file->getTimestamp();
}
}
};
(untested) which will put the timestamp inside the link but it's close enough.
Let's say I have completed my index.html file with all the CSS and JavaScripts and I want to create some other ones like: "contact", "about us", "music" etc.
They all have to go into the same root folder as the index.html. Well this is ok with me since there's not that many, but what about sub-categories? Like in music I would like to have 10 different genres.html and inside that, 20 more artists.html and so on. This would entirely cluster my root folder. And putting them into a sub-folder doesn't work either, because then all the links to the centralized resources (like: CSS files, images, JavaScript) break. And having to manually adjust every absolute path is also a pain. I gave <base> a try but it messed other things up.
What is the best and simplest way to organize your website's page structure (preferably without a CMS)?
If PHP is an possibility, you could use an very simple script like this:
{root}/index.php
<?php
if(!isset($_GET['route'])){ // If no route is given
require_once 'pages/index.html'; // Load the default index page
}else if(!file_exists('pages/' . $_GET['route'] . '.html')){ // If an route is given, check if the page exists
require_once 'pages/404.html'; // If not, load an 404 page
}else{
require_once 'pages/' . $_GET['route'] . '.html'; // Or else load the given route
}
The url's will then be something like this:
www.yoursite.com/index.php?route=index (index.html inside the pages folder)
www.yoursite.com/index.php?route=contact (pages/contact.html)
www.yoursite.com/index.php?route=catalog/category/something/list (pages/catalog/category/something/list.html)
This is very simple and basic PHP using so called $_GET variables (More about that here )
All the requests will be handeld by the index.php inside the root of your website.
Because of that, your include link for your JS and CSS files, will always be from the root directory. Therefore, you don't need to worry about all the different paths.
If you need more help, just ask.
Update
Your folder structure could be something like this than:
root/
css/
js/
img/
pages/
music/
artist.html
something/
else/
stuff.php
index.html
contact.html
index.php
And instead of doing $_GET['route'] . 'html', you could also use just $_GET['route'] and append the file extension to the url. This way you can use all different types of file extensions. (www.yoursite.com/index.php?route=music/artist.php)
Or you could just change .html to .php. That's all up to you!
I use to design my page's structure with the help of PHP.
Here's how I would do it:
Template page
you can make your own template and only fill in the content:
<?php
$root = "";
require_once $root . 'class/page.php';
$page = new Page();
?>
<!DOCTYPE html>
<html>
<head>
<?php
$page->metaTags();
$page->metaDescription("");
$page->metaKeywords("");
$page->defaultStyles();
$page->addStyle("styleName");
$page->title("your page title");
$page->defaultScripts();
?>
</head>
<body>
<?php
$page->navigation();
$page->header();
$page->openContainer();
$page->openContentSection();
?>
Your page content
<?php
$page->closeContentSection();
$page->closeContainer();
?>
</body>
</html>
Now the page class will handle the layout and the links so you can make your changes in 1 place and still affect all the pages in your site.
Page class
class Page{
private $db;
private $root;
private $terms;
public function __construct() {
$this->db = new db();
...
}
public function metaDescription($desc){
echo '
<meta name="description" content="' . $desc . '" />';
}
public function defaultStyles(){
echo '
<link href="' . $this->root . 'css/bootstrap.min.css" rel="stylesheet" />';
}
....
}
Now, the pages can be anywhere you want them to be, you just set the $root to your absolute website url and all the includes will be correct no matter where your files are being saved.
I just simply want to copy some image files and store them in a folder...
My code seems to work for txt documents but when I want to copy something else it doesn't work :(
<?php
$ImageLocation = $_POST["ImageLocation"];
//$ImageLocation = '/xampp/htdocs/cw/mysql/images/test.txt';
$ext = (new SplFileInfo($ImageLocation))->getExtension();
$newFile = "/xampp/htdocs/cw/mysql/images/$ProductName.$ext";
if (!copy($ImageLocation, $newFile)) {
echo "failed to copy $file...\n";
}
?>
If anyone could help that would be very handy, ta.
I need to bootstrap Drupal. I have a php-file with all my functions and in one of those functions, I need to use a Drupal function which is located in the bootstrap.inc file.
Structure of the server:(d) drupal (sd)includes (f)bootstrap.inc(d) scripts (sb)functions (f) functions.php
So I need to include in a self-written function the "variable_set" function, located in bootstrap.inc.
A little piece of the function my college wrote (I'm terribly sorry, but I don't know how to format php on the forum. If someone does, please let me know so I can edit this mess):
function readxml()
{
echo "<br/>READING...<br/>";
$file = './config.xml';
$xml = simplexml_load_file($file);
if($xml !== false)
{
foreach($xml->config->children() as $item){
$name = $item->getName(); // GETS CHILDREN UNDER 'CONFIG'
switch($name)
{
case 'website':
foreach($xml->config->website->children() as $kid){
$childname = $kid->getName();
switch($childname)
{
case 'theme':
if(inserttheme($kid)or die ('failed to insert theme<br/>')){
echo 'theme is installed.<br/>';}
break;
case 'slogan':
if(insertslogan($kid)or die('failed to insert slogan<br/>')){
echo 'slogan is installed.<br/>';}
break;
case 'sitename':
if(insertname($kid)or die('failed to insert name<br/>')){
echo 'website name is installed.<br/>';}
break;
}
}
break;
`
So, somewhere in the theme/slogan/name section, I have to call the variable_set function which is located in the bootstrap.inc file.
Somewhere I found this (again sorry for the non-formated text):
$drupal_directory = "/home/httpdocs/drupal"; // wherever Drupal is
$current_directory = getcwd();
chdir($drupal_directory);
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
chdir($current_dir);
return;
I included it both in my function.php as well as in my final php-file (where all the functions are called) but no result... What am I doing wrong?
That code you found looks about right, what is exactly is "no result", are you getting errors or nothing or...? Also, where did you put it exactly? (If it is not in a function, you need to remove the last line (return))
Also, the proper way to fix this would be to integrate your custom code as a Drupal module, then you don't have to worry about stuff like this: http://drupal.org/developing/modules
Or if it is a CLI script, expose it as a drush command: http://drupal.org/project/drush
Answer to the question, just include the file inside the Drupal folder!