Lithium PHP Framework - unable to upload files on live site - php

I'm working on a site, that is live already, and when I attempt to upload a file - I receive "Status Code: 201 Created", but no content after that.
And when I run the site locally and upload the file - I get the same status code, but after that I get content - JSON with the data of the newly created file.
In both cases I see in the Mongo database that the file is created, and when I attempt to access it through a controller for it through
http://({domain}file/{{file-id}}
I see the file - even on live.
The problem appears to be somewhere after that saving ... and before [[something]] returns the JSOn with the file data ...
... but because everything in Lithium is soooooooooooooooo muuuuuuucccchhhhhhhh automated ... I don't know how to find the problem.
(And I don't want to dump inside the framework itself ... I'm supposed to use the framework, not to debug it! ...)

Well, the problem appeared to be that after the upload the server should return JSON with the data for the file, but on live json_encode() was returning false because of some non-UTF8 text in the object. I managed to workaround this ... and the solution can be found in this question I posted today: json_encode() turn non-UTF8 strings into null, but on live site returns false

Related

FileNotFoundException in Silex when returning a php image resource

i have an API route in my silex server that renders a php image resource and returns it via a BinaryFileResponse. I tried several response types and the BinaryFileResponse was the only one working properly. Nevertheless, every time i use this route to retrieve an image i get following error message in the logs:
silex: CRITICAL Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException: The file "1" does not exist (uncaught exception) at /opt/pyriand3r/vendor/symfony/http-foundation/File/File.php line 41 | host='sv35' exception='error'
I believe this is, because i do not return an actual file but only a file resource not persisted in the filesystem.
Is there a way to erase this error without saving the image temporarly in the filesystem? Maybe another response type or something?
BinaryFileResponse is indeed intended for items in the filesystem. For your scenario, StreamedResponse would be appropriate.
In case StreamedResponse was among the response classes you unsuccessfully tried to get to work, you should post the failing code.

Upload content:// file to server

I have uri of file on android phone : content://com.android.contacts/contacts/472/photo. I got it from PhoneGap Contacts plugin. It is actually contact image. Now I need to upload that image on server but when I upload it and try to see it it is not visible.
I managed to google a little bit and found that for file upload I need to have full file:// path.
Now my question is how to convert content:// to file:// or how to upload files with content:// uri on server?
I have tried using window.resolveLocalFileSystemURI(uri, win, fail); and than
function win(fe){
alert("win");
alert(fe.fullPath);
}
function fail(a){
alert('fail');
}
But somehow it just "skipp" window.resolveLocalFileSystemURI(uri, win, fail);. It is never executed... No error in LogCat and no any alert from these 3 ...
Can this be solution of my problem and why no response is returned?
I don't have direct experience with the contacts plugin, but the docs make me think that you are not looking at a filesystem URI, rather an entry in the contacts database.
[http://docs.phonegap.com/en/3.3.0/cordova_contacts_contacts.md.html#contacts.find]
So, you are likely mis-interpreting the concept of a URI, especially in the context of the filesystem. [http://en.wikipedia.org/wiki/Uniform_resource_identifier] and [http://www.html5rocks.com/en/tutorials/file/filesystem/].
From the plugin docs, we do see that 'photos' is a property of each contact.
how to upload files with content:// uri on server?
for the desired contact, you could create a file using the photos property, and then upload using the phonegap file-transfer plugin. [http://docs.phonegap.com/en/3.3.0/cordova_file_file.md.html#FileTransfer]

CakePHP Images with special chars in name are not displayed

I'm using CakePHP 2.0.4, PHP 5.3.1, Apache 2.2.14.
For example: the filename is F#7m7~1.gif. It really exists, the path and filename are correct.
Before print HTML tag, I encode it by using urlencode() and the tag goes like:
<img src="/chord/img/chords/F%2523m7~1.gif" alt="F#m7">
But the image is not loading. ('Chord' is a CakePHP Plugin)
I also tried to load it directly on the browser, http://myapp.localhost/chord/img/chords/F%2523m7~1.gif but what I get is: "Missing Controller Error: Chord.ImgController could not be found."
Everything works fine with the file as A~1.gif, but it brokes with filenames such as F#m~1.gif, B(7)~1.gif etc.
Everthing was working ok in pure PHP coded version. Now, it's not working at CakePHP.
Is there anybody have a clue?
You seem to be double-encoding it, the right urlencode for F#7m7~1.gif is F%237m7~1.gif, not F%2523m7~1.gif. Just remove one of them.
The Missing Controller error is just because there is no such file on your server, so Cake thinks you're trying to call an ImgController, within the Chord plugin. Try http://myapp.localhost/chord/img/chords/F%237m7~1.gif, it should work.
Anyway, as #GordonM pointed out, it's best to stick with normal characters for filenames.

Load/Modify/Save XML with javascript

Before this question gets stamped as a duplicate, I am sorry! I've read ALL the duplicate questions and if anything, it has confused me even more. So maybe this question is slightly different.
I've written a little Javascript library that makes ajax calls and fetches and parses information from the graph facebook API.
This enables me to pretty much show all my page status' on my web page. However I'm just about to launch, and I have done as much testing as I can.
However. I'm sure errors will occur, and I've written many error catches blah blah blah.
What I want to do, is save all my errors in a xml file.
So when an error occurs, I want the javascript to load the xml file from the server, add the errors, then save the changes.
I know how to load the xml doc using XmlHttpRequests, And I'm sure I can figure out how to modify the xml just by using dom manipulation.
All i really want to know is. How do i save these changes? does it save automatically?
Or do i have to "somehow" pass the updated xml version to php and get that to save it?
Im not quite sure how to go about it.
I would use mySQL and php but that means "somehow" passing the error information to php, then saving it.
However id much prefer XML seeing as I'm the only person that will be reading the xml file.
Thanks very much.
Alex
Or do i have to "somehow" pass the updated xml version to php and get that to save it?
Yes, you'll want to use an XML HTTP request to send the XML DOM to the server where PHP can save it:
function postXML(xmlDOM, postURL, fileName, folderPath){
try{
// Create XML HTTP Request Object
oXMLReq = new ActiveXObject("MSXML2.XMLHTTP.3.0");
// Issue Synchronous HTTP POST
oXMLReq.open("POST",postURL,false);
// Set HTTP Request Headers
if(fileName != null){ oXMLReq.setRequestHeader("uploadFileName", fileName); } // What should file be named when saved on server?
if(folderPath != null){ oXMLReq.setRequestHeader("uploadDir", folderPath); } // What folder should file be saved in on server?
// SEND XML
///WScript.Echo(xmlDOM.xml);
oXMLReq.send(xmlDOM.xml);
return oXMLReq.responseText;
}catch(e){
return "postXML failed - check network connection to server";
}
}

Ajax issues, Invalid JSON

I'am building simple Ajax application (via jquery). I have strange issue. I found where the problem is, but I don't know how to solve it.
This is simple server-side php code:
<?php
require('some.php');
$return['pageContent'] = 'test';
echo(json_encode($return));
?>
On the client side, the error "Invalid JSON" is thrown.
I have discovered that if I delete require function, everything work fine.
Just for information, the "some.php" is an empty php file. There is no error when I open direct php files.
So, conclusion: I cant use require or include function if I want to use ajax?
Use Firebug to see what you're actually getting back during the AJAX call. My guess is that there's a PHP error somewhere, so you're getting more than just JSON back from the call (Firebug will show you that). As for your conclusion: using include/require by itself has absolutely no effect on the AJAX call (assuming there are no errors).
Try changing:
<?php
require('some.php');
$return['pageContent'] = 'test';
echo(json_encode($return));
?>
To:
<?php
$return = array(
'pageContent' => 'test'
);
echo json_encode($return);
?>
The problem might have to do with $return not being declared as an array prior to use.
Edit: Alright, so that might not be the problem at all. But! What might be happening is you might be echoing out something in the response. For example, if you have an error echoing out prior to the JSON, you'd be unable to parse it on the client.
if the "some.php" is an empty php file, why do you require it at all?!
require function throws a fatal error if it could't require the file. try using include function instead and see what happens, if it works then you probably have a problem with require 'some.php';
A require call won't have any effect. You need to check your returned output in Firebug. If using Chrome there is a plugin called Simple REST Client. https://chrome.google.com/extensions/detail/fhjcajmcbmldlhcimfajhfbgofnpcjmb through which you can quickly query for stuff.
Also, it's always good to send back proper HTTP headers with your response showing the response type.
It's most likely the BOM as has been discussed above. I had the same problem multiple times and used Fiddler to check the file in hex and noticed an extra 3 bytes that didn't exist in a prior backup and in new files I created. Somehow my original files were getting modified by Textpad (both in Windows). Although when I created them in Notepad++ I was fine.
So make sure that you have your encoding and codepages set up properly when you create, edit, and save your files in addition to keeping that consistent across OSes if you're developing on windows let's say and publishing to a linux environment at your hosting provider.

Categories