I'm going to upload files from flash to server. When user begin, input his username, then I send it to php, this way:
var myusername:String = username.text;
username.restrict = "A-Za-z0-9";
login_btn.addEventListener(MouseEvent.CLICK,login);
function login (evt:MouseEvent):void{
var loader : URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("http://domain/uploads/upload.php");
request.method = URLRequestMethod.POST;
var variables:URLVariables = new URLVariables();
var userid:String = myusername;
variables.ID = userid;
request.data = variables;
loader.load(request);
}
upload.php :
<?php
$myuser = $_POST['ID'];
$uploads_dir = './uploads/'.$myuser;
if( $_FILES['Filedata']['error'] == 0 ){
if( move_uploaded_file( $_FILES['Filedata']['tmp_name'],
$uploads_dir.$_FILES['Filedata']['name'] ) ){
exit();
}
}
echo 'error';
exit();
?>
The problem is that files upload to uploads folder not in user folder. Anyone could help me please?
What is the value of $_POST['ID']?
I don't see you setting that anywhere in the request. There's this:
variables.UID = userid;
But wouldn't that be $_POST['UID'] and not $_POST['ID']?
Related
I have two pages first page has a form which accepts information like name,mobile-no etc and an image from user.
I want to access the image using jquery and send it other page using $.post() method there i want to access the image and insert it into database
First File
<script>
$(function(){
$("#add_staff").click(function(){
event.preventDefault();
var regex_mobile=/^\d{10}$/;
var regex_email = /^([a-zA-Z0-9_\.\-\+])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var insti=$("#inst_id").val();
var name=$("#Staff_name").val();
var pwd=$("#Staff_pwd").val();
var add=$("#staff_address").val();
var dob=$("#Staff_dob").val();
var mail=$("#Staff_mail").val();
var role=$("#Staff_role").val();
var mobile=$("#Staff_mobile").val();
var img = $('#image').prop('files')[0];
if(name=="" || pwd=="" || add=="" || dob=="" || mail=="" || role=="" || mobile=="")
{
alert("All fields are compulsory");
}
else{
if(!regex_mobile.test(mobile)) {
alert("Mobile Number invalid");
}
else if(!regex_email.test(mail)){
alert("Email Invalid");
}
else{
$.post("add_faculty2.php",
{
insti: insti,
name:name,
pwd:pwd,
add:add,
dob:dob,
mail:mail,
role:role,
mobile:mobile,
img:img
},
function(data,status){
$("#msg").text(data);
alert(data);
alert(status);
});
}
});
});
</script>
In the above file all the information is accepted from the user and in the jquery i have tried to access all the information and image then i am trying to send the data to other page using $.post() method.
Second File
<?php
$insti =$_POST['insti'];
$name =$_POST['name'];
$pwd =$_POST['pwd'];
$add =$_POST['add'];
$dob =$_POST['dob'];
$mail =$_POST['mail'];
$role =$_POST['role'];
$mobile =$_POST['mobile'];
$img =$_POST['img'];
$today = date("Y-m-d H:i:s");
include_once 'conf.php';
$q="insert into tbl_staff_details(inst_id,name,pwd,email,phone,photo,address,dob,role,created_date) values('".$insti."','".$name."','".$pwd."','".$mail."','".$mobile."','".$img."','".$add."','".$dob."','".$role."','".$today."')";
echo $q;
$r=mysqli_query($con,$q);
if($r)
{
echo "Success";
}
else
{
echo "Fail";
}
?>
In the above file i am accessing all the information using $_POST[] sent by the $.post() method in the first file then i am trying to insert all the data in the database
Problem is that the data is not inserted in the database
But when i omit the image all the other data is inserted in the database
Ok first you need to use FormData() in your javascript
example
// formdata
var formdata = FormData();
var insti=$("#inst_id").val();
var name=$("#Staff_name").val();
var pwd=$("#Staff_pwd").val();
var add=$("#staff_address").val();
var dob=$("#Staff_dob").val();
var mail=$("#Staff_mail").val();
var role=$("#Staff_role").val();
var mobile=$("#Staff_mobile").val();
var img = $('#image').prop('files')[0];
formdata.append('upload[insti]', insti);
formdata.append('upload[name]', name);
formdata.append('upload[pwd]', pwd);
formdata.append('upload[add]', add);
formdata.append('upload[dob]', dob);
formdata.append('upload[mail]', mail);
formdata.append('upload[role]', role);
formdata.append('upload[mobile]', mobile);
formdata.append('upload[img]', img);
// now send
$.post("add_faculty2.php", formdata,
function(data,status){
$("#msg").text(data);
alert(data);
alert(status);
}
);
And in your php script you can access the image by calling
$img = $_FILES['upload']['img']['name'];
$img_tmp = $_FILES['upload']['img']['tmp_name'];
others
$insti = $_POST['upload']['insti'];
$name = $_POST['upload']['name'];
$pwd = $_POST['upload']['pwd'];
$add = $_POST['upload']['add'];
$dob = $_POST['upload']['dob'];
$mail = $_POST['upload']['mail'];
$role = $_POST['upload']['role'];
$mobile = $_POST['upload']['mobile'];
$today = date("Y-m-d H:i:s");
// you can see the image printed here..
var_export($img);
Hope this helps.
I don't know why but I can't figure out how to upload a picture from the CameraRoll on my AIR app to my server.
Here's my code :
function initCamera(evt:Event):void{
cameraRoll.addEventListener( MediaEvent.SELECT, imageSelected );
cameraRoll.browseForImage();
}
function imageSelected( event:MediaEvent ):void{
//PREVIEW OF THE IMAGE
var mp:MediaPromise = event.data;
mediaPath.text = mp.file.name + "\n" + mp.file.url;
Imgloader.source = mp.file.url;
uploadImage();
function uploadImage():void{
feedbackText.text = "uploadImage";
var req = new URLRequest();
req.url = ( stage.loaderInfo.parameters.f )? stage.loaderInfo.parameters.f : 'http://www.****-**/***/uploadImg.php';
mp.file.upload(req);
mp.file.addEventListener( ProgressEvent.PROGRESS, progress_func );
}
}
Any idea what's wrong with the code ?
The function "upload" is triggered but nothing is uploading... (and progress_func is not triggered)
I'm trying to upload file using flash So
I've following code in action-script:
var fileRef:FileReferenceList = new FileReferenceList();
fileRef = new FileReferenceList();
fileRef.browse(new Array( new FileFilter( "Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png" )));
fileRef.addEventListener(Event.SELECT, fileSelectHandler);
var uploadURL:URLRequest = new URLRequest();
var uploadPhotoScript:String = "http://localhost/uploader/upload.php";
uploadURL.url = uploadPhotoScript;
function fileSelectHandler(event:Event):void {
for each(var fileToUpload:FileReference in fileRef.fileList){
uploadSingleFile(fileToUpload);
}
}
function uploadSingleFile(file:FileReference):void {
file.addEventListener(ProgressEvent.PROGRESS, onUploadProgress);
file.upload(uploadURL);
file.addEventListener(Event.COMPLETE, completeHandler);
}
function onUploadProgress(e:ProgressEvent):void
{
var f:FileReference = e.currentTarget as FileReference;
var fileName:String = f.name; //Now I know which file it is, I can update accordingly
var progress:Number = (e.bytesLoaded / e.bytesTotal) * 100; //shows percent, you might want to round this off using Math.round(number);
}
function completeHandler(event:Event):void {
trace("upload complete");
}
upload.php:
<?php
if(isset($_SERVER["HTTP_REFERER"]){
$ref=$_SERVER["HTTP_REFERER"];
$ref=explode($ref,"/");
$ref=$ref[2];
if($ref=="localhost"){
if(!empty($_FILES)){
$tmpfile = $_FILES['Filedata']['tmp_name'];
$targetfile = dirname(__FILE__) . '/' . $_FILES['Filedata']['name'];
move_uploaded_file($tmpfile, $targetfile);
}
}
else{
header("location:NotLocalhost.html");
exit;
}
}
else{
header("Location:NOREF.html");
exit;
}
?>
This code works fine in all browsers except firefox. coz In firefox $_SERVER["HTTP_REFRER"] becomes null and NOREF.html is displayed.
Also in firefox when the script is accessed by another html or php script then works fine, but when is accessed vai swf movie which is in turn <embeded> in html page. Then the REFERER becomes null.
Any ideas? How can I solve this?
Hi I'm working on actionscript, saving a raw image from camera then POST with save.php then
I want save.php echo back the variable which is a file name that have just generated by save.php to actionscript
see this line:
var urlParameter:String = "images/test_01.php?img=" + "myfileURL";
navigateToURL(new URLRequest(urlParameter), "_blank");
Thanks in advance
this is AS3 code
function onSaveJPG(e:Event):void{
var myEncoder:JPGEncoder = new JPGEncoder(100);
var byteArray:ByteArray = myEncoder.encode(bitmapData);
var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
var saveJPG:URLRequest = new URLRequest("save.php");
saveJPG.requestHeaders.push(header);
saveJPG.method = URLRequestMethod.POST;
saveJPG.data = byteArray;
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, sendComplete);
urlLoader.load(saveJPG);
function sendComplete(event:Event):void{
warn.visible = true;
addChild(warn);
warn.addEventListener(MouseEvent.MOUSE_DOWN, warnDown);
warn.buttonMode = true;
}
function warnDown(e:MouseEvent):void{
var urlParameter:String = "images/test_01.php?img=" + "myfileURL";
navigateToURL(new URLRequest(urlParameter), "_blank");
// navigateToURL(new URLRequest("images/"), "_blank");
// +saveJPG:URLRequest
// navigateToURL(new URLRequest("images/test_01.php?img=+saveJPG:URLRequest"), "_blank");
warn.visible = false;
}
} // move onSave JPG
} close to here instead of after sendComplete
warn.visible = false;
this is save.php
<?php
if(isset($GLOBALS["HTTP_RAW_POST_DATA"])){
$jpg = $GLOBALS["HTTP_RAW_POST_DATA"];
$img = $GET["img"];
$filename = "images/poza". mktime(). ".jpg";
file_put_contents($filename, $jpg);
echo "thisPic=" . $filename;
// echo $filename;
} else{
echo "Encoded JPEG information not received.";
}
?>
In your case you are just using mktime() for the file name, and images/poza is hard coded. You can post the file name along the jpeg data from flash, and when onSaveJPG is called you simply display the file name to the user.
Below is example of how you can send file_name.
var JPGFileName = YOUR TIME FUNCTION
var saveJPG:URLRequest = new URLRequest("save.php?file_name=" + JPGFileName);
saveJPG.requestHeaders.push(header);
saveJPG.method = URLRequestMethod.POST; saveJPG.data = byteArray;
getURL('http://www.google.com',_blank);
The above can open google.com,but how to fetch data from server side(PHP) on localhost?
I found this tutorial
<?
$first="this";
$second = "that";
echo "myfirst=$first&mysecond=$second";
exit;
?>
Flash Actionscript:
var lvSend = new LoadVars();
var lvReceive = new LoadVars();
lvSend.SendAndLoad("www.domain.com/script.php",lvReceive,"POST");
lvReceive.onLoad = function(bSuccess) {
if(bSuccess == true) {
trace(this.myfirst);
trace(this.mysecond);
}
}