I followed this question:
flash Actionscript 3 and php image upload
I copied the code:
function uploadFile( e:Event ):void
{
fileRef.upload( new URLRequest( "http://localhost/php5dev/test/upload_script.php" ), "as3File", false );
}
and
<?php
$target = "uploads/" . basename( $_FILES[ "as3File" ][ "name" ] );
if ( move_uploaded_file( $_FILES[ "as3File" ][ "tmp_name" ], $target ) )
echo( "file upload success<bt />" );
else
echo( "error uploading file<br />" );
?>
It works great, I just need to know how to save the image under a specific name, how do I pass an argument to the php script? or is that not necessary, can I change it before calling the script?
How would I then call URLrequest to show user the file he uploaded ?
Thanks, for any answers!
You could send the filename via a GET query:
fileRef.upload( new URLRequest( "http://localhost/php5dev/test/upload_script.php?filename=myFile.txt" ), "as3File", false );
And then retrieve it in PHP as $_GET['filename'];
As a word of advice, be careful with those scripts since they are very insecure (one could upload any file to your server and exploit it very easily).
Related
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.
I am working on a custom theme and have included acf pro inside that theme as mentioned in the docs. The theme is working fine and acf is activated on theme activation. Here is the code.
// customize ACF path
add_filter('acf/settings/path', 'my_acf_settings_path');
function my_acf_settings_path( $path ) {
$path = get_stylesheet_directory() . '/inc/advanced-custom-fields-pro/';
return $path;
}
// customize ACF dir
add_filter('acf/settings/dir', 'my_acf_settings_dir');
function my_acf_settings_dir( $dir ) {
$dir = get_stylesheet_directory_uri() . '/inc/advanced-custom-fields-pro/';
return $dir;
}
// Include ACF
include_once( get_stylesheet_directory() . '/inc/advanced-custom-fields-pro/acf.php' );
The issue i am facing for couple of hours now is due to the groups/fields that i want to be instantiated inside acf. I have some field groups that i want to be shown on fresh installation. Below are the methods i have tried:
Method 1:
I have exported the fields as json inside a folder called acf-json. ACF does recognize it and shows as a sync field. But when i try syncing it, it just creates a new empty field.
Method 2:
I have also tried exporting the field groups as php files and then including it in my functions.php file but acf doesn't recognize this code.
Distributing ACF in a theme or plugin is a bit tricky because of lack of information in the docs. The tricky part is exporting your fields with your theme and plugin, in a way that your users dont have to do anything different then what they are used to with any other theme or plugin. I will go through the procedure in detail.
For theme and plugin Development:
Referring the official docs, it should be pretty easy to copy and paste the code in your functions.php file for theme development, while for plugin development you can add it to the main plugin file. This will accomplish these 4 tasks.
Add ACF Path
Add ACF Dir
Hide ACF from clients (if needed)
Include ACF
Up till now what you have done actually doesn't do anything special. It just activates ACF whenever you activate your theme/plugin and similarly deactivates ACF on theme/plugin deactivation.
Exporting Fields: (via JSON sync)
At this stage if you distribute your theme/plugin it will just activate ACF, but it wont have any fields inside it. ACF uses JSON to keep track of all the fields and field groups. By default ACF will look for a folder called acf-json in the root of your theme. If you have this folder then ACF will automatically add/update a new json file for each field group you add or update.
You can change the location of this folder, if you want to keep it inside your includes folder. Somehow you cant change the default location on a theme, but for plugins you can by adding this code.
add_filter('acf/settings/save_json', 'set_acf_json_save_folder');
function set_acf_json_save_folder( $path ) {
$path = dirname(__FILE__) . '/includes/acf-json';
return $path;
}
add_filter('acf/settings/load_json', 'add_acf_json_load_folder');
function add_acf_json_load_folder( $paths ) {
unset($paths[0]);
$paths[] = dirname(__FILE__) . '/includes/acf-json';
return $paths;
}
Now if you share this theme/plugin with someone, when they go inside ACF they should see a new option for sync. On syncing all the files fields should be available to them.
Automatting the SYNC Process:
If you want to hide ACF completely then obviously you cant have your users go inside ACF and sync fields. So in this case you need a script that would automatically sync all the fields from the json folder. You can add this code inside your functions.php for themes or inside your main plugin file. You dont have to change any paths in this script because in the previous code you have already told ACF where to load the JSON files from.
add_action( 'admin_init', 'article_gamification_sync_acf_fields' );
function article_gamification_sync_acf_fields() {
// vars
$groups = acf_get_field_groups();
$sync = array();
// bail early if no field groups
if( empty( $groups ) )
return;
// find JSON field groups which have not yet been imported
foreach( $groups as $group ) {
// vars
$local = acf_maybe_get( $group, 'local', false );
$modified = acf_maybe_get( $group, 'modified', 0 );
$private = acf_maybe_get( $group, 'private', false );
// ignore DB / PHP / private field groups
if( $local !== 'json' || $private ) {
// do nothing
} elseif( ! $group[ 'ID' ] ) {
$sync[ $group[ 'key' ] ] = $group;
} elseif( $modified && $modified > get_post_modified_time( 'U', true, $group[ 'ID' ], true ) ) {
$sync[ $group[ 'key' ] ] = $group;
}
}
// bail if no sync needed
if( empty( $sync ) )
return;
if( ! empty( $sync ) ) { //if( ! empty( $keys ) ) {
// vars
$new_ids = array();
foreach( $sync as $key => $v ) { //foreach( $keys as $key ) {
// append fields
if( acf_have_local_fields( $key ) ) {
$sync[ $key ][ 'fields' ] = acf_get_local_fields( $key );
}
// import
$field_group = acf_import_field_group( $sync[ $key ] );
}
}
}
**Now when you distribute your theme/plugin, on activation it will also activate ACF, then copy all the json files and execute them. This will automatically sync all the field groups, now you can even hide your ACF plugin, and none of your users ever have to go inside ACF to sync fields, infact they wont even have to know that ACF exists on their site. Secondly even if you make a new change in ACF, it should automatically update changes to the json files. You can now even version control them for better control. **
I've programmed my website by Yii2. When I refresh my website it works like Ctl + F5, and all the font awesome and all the cache of my site reload again. It look likes I open the page first time.
Link of my website
Add, this in your config file. According to your need.
$linkAssets
Whether to use symbolic link to publish asset files. Defaults to
false, meaning asset files are copied to $basePath. Using symbolic
links has the benefit that the published assets will always be
consistent with the source assets and there is no copy operation
required. This is especially useful during development.
'components' => [
'assetManager' => [
'linkAssets' => true,
],
]
Or
$forceCopy
Whether the directory being published should be copied even if it is
found in the target directory. This option is used only when
publishing a directory. You may want to set this to be true during the
development stage to make sure the published directory is always
up-to-date. Do not set this to true on production servers as it will
significantly degrade the performance.
'components' => [
'assetManager' => [
'forceCopy' => true,
],
]
For more info, Please click these useful links
Link Assets - Yii2 Asset Manager
Force Copy - Yii2 Asset Manager
Assets-Clear-Cache - Yii2 (Stack Overflow)
Or,
As, I am using Yii2-App-Basic. So, My Assets are getting created in ROOT/web/assets folder. So, I manually hit this action to clear my cache. This is not a good way to clear cache. Even though, it's useful for time being.
This function, I created in SiteController.php.
And, I hit URL Like : MyWebsite.com/site/clear-cache.
<?
public function actionClearCache(){
$cacheDirPath = $_SERVER['DOCUMENT_ROOT'].'/assets';
if($this->destroy_dir($cacheDirPath, 0)){
Yii::$app->session->setFlash('success', 'Cache cleared.');
}
return $this->render('some-page');
}
private function destroy_dir($dir, $i = 1) {
if (!is_dir($dir) || is_link($dir))
return unlink($dir);
foreach (scandir($dir) as $file) {
if ($file == '.' || $file == '..') continue;
if (!$this->destroy_dir($dir . DIRECTORY_SEPARATOR . $file)) {
chmod($dir . DIRECTORY_SEPARATOR . $file, 0777);
if (!$this->destroy_dir($dir . DIRECTORY_SEPARATOR . $file))
return false;
};
}
if($i == 1)return rmdir($dir);
return true;
}
<?php
# Alert the user that this is not a valid access point to MediaWiki if they try to access the special pages file directly.
if ( !defined( 'MEDIAWIKI' ) ) {
echo <<<EOT
To install my extension, put the following line in LocalSettings.php:
require_once "$IP/extensions/Userprofile/Userprofile.php";
EOT;
exit( 1 );
}
$wgExtensionCredits['specialpage'][] = array(
'path' => __FILE__,
'name' => 'Userprofile',
'author' => 'matsuiny2004',
'url' => 'http://localhost/mywiki/index.php/Extension:Userprofile',
'descriptionmsg' => 'userprofile-desc',
'version' => '0.0.0',
);
$wgAutoloadClasses['SpecialUserprofile'] = __DIR__ . '/SpecialUserprofile.php'; # Location of the SpecialMyExtension class (Tell MediaWiki to load this file)
$wgMessagesDirs['Userprofile'] = __DIR__ . "/i18n"; # Location of localisation files (Tell MediaWiki to load them)
$wgExtensionMessagesFiles['UserprofileAlias'] = __DIR__ . '/Userprofile.alias.php'; # Location of an aliases file (Tell MediaWiki to load it)
$wgSpecialPages['Userprofile'] = 'SpecialUserprofile'; # Tell MediaWiki about the new special page and its class name
function extensionFunction() {
# Assume $title is the title object
if( $title->isProtected( 'edit' ) ) {
# Protected from editing, do things
} else {
# Not protected from editing
}
}
//test code here
echo '<div id="navigation">Navigation</div>';
?>
<?php
echo '<div id="account">account</div>';
?>
<?php
echo '<div id="editpage">edit page</div>';
?>
<div id='border-search'>
<img src="http://s6.postimage.org/z6ixulv6l/searchbox_border.png"></img>
</div>
?php>
<div class='rectangle-box'>
<div class='rectangle-content'></div>
</div>
I am including extra php and html code from a seperate file that I load as an extension in mediawiki to create a custom layout. the problem is that the code loads before the doctype making the page render in quirks mode in IE and safari. How can i get it to load after the doctype tag?
The problem is that you have top-level echo statements all over your code (by top level I mean ones that are not included in any function). Which is why the PHP engine executes them as soon as it sees them, which is before the MediaWiki itself starts running.
MediaWiki has some good documentation explaining all kinds of hooks. What you should do is write a few functions and place all your echo-ing code there, and expect this function to be executed when the event in question occurs.
I see a good starter page here. Relevant example is:
$wgHooks['ArticleSave'][] = 'wgAddStub';
function wgAddStub( &$article, &$user, &$text, &$summary, $minor, $watchthis, $sectionanchor, &$flags, &$status ) {
$text = ( $article->exists() ? "" : "{{stub}}\n" ) . $text;
return true;
}
The SWF is located on a web server. I am calling the function using this code in AS3...
myPDF.save(Method.REMOTE, "http://www.example.com/generator/createpdf.php",
Download.ATTACHMENT, "line.pdf");
Here is my PHP script located on the server...
$method = $_GET['method'];
$name = $_GET['name'];
if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] )) {
// get bytearray
$pdf = $GLOBALS["HTTP_RAW_POST_DATA"];
// add headers for download dialog-box
header('Content-Type: application/pdf');
header('Content-Length: '.strlen($pdf));
header('Content-disposition:'.$method.'; filename="'.$name.'"');
echo $pdf;
} else echo 'An error occured.';
It used to work, but stopped a while back. Any help would be greatly appreciated.
1) This stopped working for me as well, until I added the following -
if(!$HTTP_RAW_POST_DATA){
$HTTP_RAW_POST_DATA = file_get_contents(‘php://input’);
}
2) I also patched /src/org/alivepdf/pdf/PDF.as::save() per this post enter link description here