PDA

View Full Version : upload file



flaria
6 May 2015, 5:58 AM
hello, I'm trying to upload a file (.txt) with extjs and PHP. I take example but it's not working. I don't understand what happened. I'have success with json. but I don't find my file!
thanks


My Extjs code:

var msg = function(title, msg) {
Ext.Msg.show({
title: title,
msg: msg,
minWidth: 200,
modal: true,
icon: Ext.Msg.INFO,
buttons: Ext.Msg.OK
});
};
var tpl = new Ext.XTemplate(
'File processed on the server.<br />',
'Name: {fileName}<br />',
'Size: {fileSize:fileSize}'
);


var myform = Ext.create('Ext.form.Panel', {
// renderTo: 'fi-form',
// renderTo: document.body,,
width: 500,
frame: true,
// title: 'File Upload Form',
bodyPadding: '10 10 0',


defaults: {
anchor: '100%',
allowBlank: false,
msgTarget: 'side',
labelWidth: 50
},


items: [{
xtype: 'textfield',
fieldLabel: 'Name'
},{
xtype: 'filefield',
id: 'form-file',
emptyText: 'Select an image',
fieldLabel: 'File',
name: 'File-path',
buttonText: '',
buttonConfig: {
iconCls: 'upload-icon'
}
}],


buttons: [{
text: 'Save',
handler: function(){
var form = this.up('form').getForm();
if(form.isValid()){
console.log (form);
form.submit({
method: 'POST',
url: 'resources/data/file-upload.php',
waitMsg: 'Uploading your file...',
success: function(fp, o) {
msg('Success', tpl.apply(o.result));
}
});
}
}
},{
text: 'Reset',
handler: function() {
this.up('form').getForm().reset();
}
}]
});


my Php code:

<?php
$returnResponse = $_REQUEST['returnResponse'];
sleep(1);
if ($returnResponse != "") {
header('HTTP/1.0 '.$returnResponse.' Server status', true, $returnResponse);
echo '{success:false, message:"Faked error from server", errors:{"file-path":"The server returned this"}}';
} else {
$file = $_FILES['File-path'];
returned this"}}';


$fileName = $_FILES['File-path']['name'];
$tmpName = $_FILES['File-path']['tmp_name'];
$fileSize = $_FILES['File-path']['size'];
$fileType = $_FILES['File-path']['type'];
$fp = fopen($fileName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);


$fileName = $file['name'];
$fileSize = $file['size'];
if (!$fileSize) {
$fileSize = $_SERVER['CONTENT_LENGTH'];
}


echo json_encode(array(
"success" => true,
"fileName" => $fileName,
"fileSize" => $fileSize
));
}
?>

Gary Schlosberg
6 May 2015, 3:31 PM
Could be a permissions issue. Any messages in the web server error log?

flaria
7 May 2015, 12:11 AM
I find the error log file ! [:error] [pid 6980:tid 1768] [client 127.0.0.1:55070] PHP Notice: Undefined index: returnResponse in D:\\Aither\\Www\\PFLGestion\\resources\\data\\file-upload.php on line 2, referer: http://localhost/pflgestion/I just begin in Web development and I don't understand what I have to make to avoid this error.

flaria
7 May 2015, 12:55 AM
Yes I find the log error file:[:error] [pid 6980:tid 1768] [client 127.0.0.1:55070] PHP Notice: Undefined index: returnResponse in D:\\Aither\\Www\\PFLGestion\\resources\\data\\file-upload.php on line 2, referer: http://localhost/pflgestion/But I'm begin in web develpment. I don't understand what to do with this error.

Gary Schlosberg
7 May 2015, 7:18 AM
Are you able to tell if the file is getting to your server? What happens if you access your PHP script directly as a test?

flaria
7 May 2015, 8:12 AM
I don't know if the file is getting to your server.
When I access my PHP script directly it is weird.
if I try with :
http://localhost/pflgestion/resources/data/file-upload.php

reponse : {"success":true,"fileName":null,"fileSize":null}

it's normal that fileName and fileSize are null because I don't give any parameters, but I have "success":true! it is weird! no?

if I try with :
http://localhost/pflgestion/resources/data/file-upload.php?File-path=C:/Documents/aide.txt
same reponse : {"success":true,"fileName":null,"fileSize":null}.

I do not know how to test it.

Gary Schlosberg
15 May 2015, 3:04 PM
I don't know much PHP, but it appears that the undefined index error comes from accessing an array key that isn't defined. Line 2 of your script is referenced, so it seems as though $_REQUEST['returnResponse'] isn't there.


$returnResponse = $_REQUEST['returnResponse'];


Is $_REQUEST defined at all?