php - AS3 upload a file to the server without using FileReference.browse() -
updated: developing air app mobile , have file called "database.db" in applicationdirectory
included or added via flash cc ide. offering bounty of 150 if can me through issue, is, me upload script on server getting actionscript upload "database.db" file it.
what hope script achieve?
i want database time time hoping upload server. how this?
the code below not work , gives error.
update: here error getting:
typeerror: error #1034: type coercion failed: cannot convert "app:/database.db" flash.net.filereference.
i don't believe question exact duplicate need upload file , example given not show how data turned datatype upload.
do have server? yes, of don't have php script needed handle upload, @ moment trying simple file as3 ready upload , can't do.
package { import flash.display.movieclip; import flash.events.mouseevent; import flash.net.urlloader; import flash.net.urlrequest; import flash.net.urlvariables; import flash.net.urlloaderdataformat; import flash.net.urlrequestmethod; import flash.events.event; import flash.net.filereference; import flash.net.filefilter; import flash.utils.*; import flash.filesystem.file; public class main extends movieclip { private const upload_url: string = "http://mywebsite.com/upload.php"; private var fr: filereference; public function main() { var dir: file = file.applicationdirectory; dir = dir.resolvepath("database.db"); trace(dir.url); // app:/database.db var file: filereference = filereference(dir.url); var request: urlrequest = new urlrequest(); request.url = upload_url; fr.upload(request); } } }
your error comes code:
var file:filereference = filereference(dir.url);
first, note don't use new filereference()
, use filereference()
form of casting. because trying cast string dir.url
filereference
, error (a cast string
filereference
not possible, error says). second, if meant write new filereference(dir.url)
still not valid, because filereference
has no constructor arguments. thirdly, fr
never assigned anything, fr.upload()
fail if execution ever got there.
solution: file
inherits filereference
, allows upload
called without restrictions. call upload
on database file
:
var file:file = file.applicationdirectory.resolvepath("database.db"); var request:urlrequest = new urlrequest(); request.url = upload_url; file.upload(request);
update
i offering bounty of 150 if can me through issue, is, me upload script on server getting actionscript upload database.db file it.
since using php starting point example given in documentation. boiling down, need use move_uploaded_file()
against uploaded file data found in $_files['filedata']
(which consist of $_files['filedata']['tmp_name']
, $_files['filedata']['name']
uploaded file):
<?php $tmp_file = $_files['filedata']['tmp_name']; $upload_file = $_files['filedata']['name']; $backup_file = basename($upload_file, '.db') . '_' . date('y-m-d_h:i:s') . '.db'; $backup_dir = './db_backups/'; move_uploaded_file($tmp_file, $backup_dir . $backup_file); ?>
note:
- your backup directory needs writable (
0777
) php beforemove_uploaded_file()
work. can using ssh, ftp or, depending on how php running, directly php script usingchmod($backup_dir, 0777)
. - it's not great idea have have arbitrary public file upload exposed this, , should not make backup directory public. ideally, should require kind of authorization upload file, , validation on uploaded file type.
update 2
my file taken applacationstoragedirectory. how move file app directory storage directory testing purposes?
you can use file/copyto()
copy file:
var file:file = file.applicationdirectory.resolvepath("database.db"); var copyfile:file = file.applicationstoragedirectory.resolvepath("database_backup.db"); file.copyto(copyfile, true);
Comments
Post a Comment