PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 17th, 2012, 07:58 AM
lancellx lancellx is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 1 lancellx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 m 31 sec
Reputation Power: 0
PHP-General - Upload file and some data with json

I'm trying to upload a file to the sever. And also some comments from a input form about this data.

Here I sent the data in js:

var postContainer = new Array();
postContainer.push(
{
file : file,
f_name : file.name,
input1 : it1Value
});

var newJ = JSON.stringify(postContainer);
xhr.setRequestHeader("X-File-Name", file.name);
var resText;
xhr.setRequestHeader("Content-type","application/json");
xhr.onreadystatechange = function(){
if(xhr.readyState != 4){
return;
}
console.log(xhr.responseText);
}
xhr.send(newJ);
Then I read the data and send the file to the upload folder:

$data = file_get_contents("php://input");
$data = json_decode($data);
var_dump($data);
$this->fileName = $data[0]->{"f_name"};
$this->genre = $data[0]->{"input1"};
$this->file = $data[0]->{"file"};
file_put_contents(
$this->path . $this->fileName,
$this->file
);
The output in console of var_damp($data) is:

array
0 =>
object(stdClass)[2]
public 'file' =>
object(stdClass)[3]
public 'webkitRelativePath' => string '' (length=0)
public 'lastModifiedDate' => string '2012-06-26T06:25:34.000Z' (length=24)
public 'name' => string 'Belgium.png' (length=11)
public 'type' => string 'image/png' (length=9)
public 'size' => int 28228
public 'f_name' => string 'Belgium.png' (length=11)
public 'input1' => string 'Genre' (length=5)
You can see the data is sent to the php page. But when I check my upload folder. I can find the file I upload. But the size is zero. That means I upload an empty file!!!

Can some one help me to solve this problem? Thank you very much!!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP-General - Upload file and some data with json

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap