I've a form that insert some values using AJAX, and now
I want to add also an image, but I can't get the image
simply using the superglobal $_FILE['file_name'] in the
PHP code, cause the comunication with PHP is under AJAX.
So I'm wondering if I could load the file from the PHP
using the file's path, infact I could simply pass it
throught the JS/AJAX.
Or it's a method that permit me to take the whole file
binary code from the JS/AJAX, then pass it to the PHP
and finallu put it in a BLOB of the SQL db?
Ps.: obviously I must use the ajax
If the file already exists on the server, I think it would be best to just pass the file path to PHP via AJAX and use PHP's file_get_contents function to read it in.
If the file does not exist on the server, you're probably better off submitting a form. PHP executing on the server cannot take a file path and get the file from the client.
If you can use jQuery, you could use an HTML form and capture the submit event, serialize the form into a variable, pass it via AJAX, and return false to prevent the form from submitting (causing a page reload).
EDIT
On second thought, serialize wouldn't be the only part of the solution. That would only get you the value of the file input, not the contents of the file. To get the contents would be much more tedious. It looks like the W3C is working on something, though.
Probably best just to use a standard HTML form.
Currently AJAX does not allow you to upload files.
You just need to use the conventional method.
However, it is a bit possible with Firefox and Chrome. But that method does not work in IE and is rather convoluted.
EDIT
Could consider getting Javascript to use this https://developer.mozilla.org/en-US/docs/DOM/FileReader, serialise it and post that via POST using AJAX. Bit convoluted. FileReader works for Chrome and the latest versions of Firefox. I think it also works for Safari and Opera (but not 100% sure). Probably not for IE.
But it would be a bit complicated at the moment in time to get all browsers to work.
I don't have any expertise regarding uploading files via AJAX. However, if you need help inserting the file into the DB as a BLOB, I can offer some assistance.
Using PDO, it is fairly easy. They have a page dedicated to details regarding LOBs.
$stmt = $db->prepare("INSERT INTO table (image) values (?)");
$fp = fopen($_FILES['file']['tmp_name'], 'rb');
$stmt->bindParam(1, $fp, PDO::PARAM_LOB);
$stmt->execute();
Related
I am trying to get an image from the user by using file input method. It is successfully getting the image from the user. But I want this input to be passed to my next line of code.
Any help would be appreciated.
<input type='file' name='image'>
$two=createfrompng("Here i want this input to be passed");
You can use $_FILES global variable.
Example :
var_dump($_FILES['image']);
http://php.net/manual/en/reserved.variables.files.php
You can't. Your PHP code is executed server-side. Before the resulting html is send to the client so you don't know if the user WILL upload a file or not and even less it's contents.
The effect you are trying to achieve should be done client-side by altering the DOM with javascript.
But anyway you can't immediately access the image because, for security reasons, javascript can't access client's filesystem so you need to upload the file (the way you are doing) process it server-side and send back to the client, which is difficult if you haven't a websocket connection to start comunication to the client.
I suggest you to slightly change your approach and submit your form targetted to an existing container in your page (tipically a div) and respond with only the html to render the image inside it.
See the jQuery .load() function. It is a simple solution and I think it will be the best for you.
…or another even simpler solution is to reload the whole page and mantain state data server side (if you want to manage multiple images as I thought to understand). It is less responsive. But not to much expensive (if your html is not huge) because the browser cache will remember your images (if you does'nt change the url to download them).
Check first, if the file exists in $_FILES and the use it, in your case:
$image = getimagesize($_FILES['image']['tmp_name']);
if ($image !== false) {
$two = createfrompng($image);
}
This seems like a simple question...I am trying to allow users to 'load' a saved data file with a Load button, choose file, etc. Can I read the data file directly into a variable from their file or does it need to be uploaded to the server first then opened and read closed and then unlinked?
Thank you,
Todd
Because PHP is SERVERSIDE you can't do anything without uploading the file. Unless this file already is on the server, there is no way around this problem.
I prettier way of doing it could be to use a jQuery-plugin to upload the file (without the page getting refreshed) and then access the content using ajax
It needs to be uploaded for PHP to access it, unless the file's contents are sent via JavaScript to PHP. That relies on a cutting edge browser.
Yes, you have to upload the file first because you have no access to the user's filesystem through browser neither using PHP nor JavaScript.
Alright, so a little preface: I've been working on adding drag and drop file uploading to the course management system called Moodle (specifically 2.0.1). This version of Moodle uses the YUI3 framework and uploads the form data with the file to be uploaded and the save-as name through an io-upload-iframe. The file is stored in the super global $_FILES until the filesystem is sent the relevant data on where to store it permanently.
The trouble I'm having is that instead of using YUI3's drag and drop features (Which, from a cursory look at their website is not the kind of drag and drop I need anyways) I'm using the native HTML5 drag and drop code. This seems to work in most major browsers (I haven't had the time to test much, and it's outside the scope of this project). The trouble I'm having is that this design of DND immediately gives you a file from the Event object in javascript. I could send this file object off to wherever I want, but the filesystem is only designed to handle variables temporarily stored in the $_FILES variable. I've not been able to find any easy way of getting this file stored there, unfortunately. I could to an HTTP request of various forms (either one of YUI3's special Y.io() requests or an XHR), but this requires a lot of duplicated code from the original source code.
Anybody have some suggestions?
Hard to tell what your problem is. But whatever your server or filesystem is, it has nothing to do with the temporarity of the $_FILES array.
When you receive the DND event, and YUI subsequently sends the file, then you will receive some data either in $_FILES or in $_POST. If so, just use move_uploaded_file or file_put_contents and store it elsewhere.
Assign that moved file a md5() hash as name, and have that returned as file identifier for your AJAX-DND upload request.
You can then use that hash id in your Javascript code, and refer to the already uploaded image file by this reference. Should your app initiate a normal form request after the drag'n'dropping, then you just include the collected image reference ids. And thus your server-side code can associate it again.
You could even reconstruct the $_FILES array if you want to:
foreach ((array)$_POST["prev_image_ids"] as $md5) {
$md5 = basename($md5);
$_FILES["image"][] = array(
"tmp_name" => $fn="./already-uploaded/$md5",
"size" => filesize($fn), "type"=>"image/whatever",
"name" => "unknown.jpg", "error"=>UPLOAD_ERR_OK,
); // you could store away those original attributes too
}
How can you bring in a path as a variable from php? I have hundreds of mp3 file names stored in a My SQL database and I want to call the name, load it into a variable and then have it replace the URL in the call to the sound file. I am all set with the PHP and database side of things, but I have been frustrated with the Flash part. I think it would just involve passing a variable from php to flash. The web pages we are building would eventually have 10 - 15 files per page and each one would have a different sound file and a different image that you could click to trigger the sound. First click would start the file and the second would stop the sound. The images and sound files are all stored in the database.
I found this code on another post and it is basically what I want to do. Still missing the button part, but if I can figure out the variable from PHP I think it will open up a bunch of new options.
var soundRequest:URLRequest = "path/to/file.mp3"; //the path would be a variable passed from the database to php and then to the actionscript
var s:Sound = new Sound(soundRequest);
var sChannel = s.play(0, int.MAX_VALUE); //Causes it to repeat by the highest possible number to flash.
//Above starts the sound immediatly (Streaming);
//Now to wait for completion instead, pretend we didnt start it before.
s.addEventLister(Event.SOUND_COMPLETE, onSComplete, false, 0, true);
function onSComplete(e:Event):void
{
var sChannel = s.play(0, int.MAX_VALUE); //Causes it to repeat by the highest possible
}
If you have a click to trigger on the page then you should use Javascript to Flash communication on the page. One flash file and a communication to flash which file to play. With anything flash these days you've probably heard of SWFobject and with Javascript you've probably heard of jQuery. Well what you need is very well documented in the jQuery SWFObject
plugin.
There is a standalone example here.
http://jquery.thewikies.com/swfobject/example_flashInteract.html
It would make sense to send all the file names from PHP to AS3 and store them in an Array, after that everything can be handled on the client side.
You need to check for Flash / PHP communication , there are loads of tutorials available on the net. Here's an example.
Output mySQL data as XML with PHP
The basic idea is to call a PHP script which should return your mp3 information as XML or JSON. I personally favor JSON but you will need to download a library.
as3 corelib
After the data has been retrieved , you can create ValueObjects
Populating Value Objects with web service XML
You should end up with an Array of ValueObjects which can then assign to your various images.
You could create a specific class for each image that would take as parameter a ValueObject.
For more info on AS3 , go here
So I have some form processing code which processes the standard text inputs and also uploaded files (through the $_FILES array)
I want to have the submission done through AJAX
Will jQuery's post(). method still pass that stuff through -> $_FILES or do I need to do something special?
Ajax (as defined, using JavaScript) cannot perform file uploads, as JavaScript cannot access the local filesystem. There are workarounds that seem to use Ajax, such as an iframe or using Flash.
Hunt around Google, you'll find something. There are more libraries (YUI for example) that are putting this together.
To upload files via Ajax, you need to use a workaround using iframes or Flash.
I recommend you Uploadify, it uses flash to do the uploading, and uses jQuery. I like the fact that it has many settings, and it comes with a nice default style for the queue handling. Just follow the instructions to set it up in your site.
In the PHP script you'll receive the file in the $_FILES array, just like if it were a normal submission via a form, the default name is 'Filedata', so you can access your file via $_FILES['Filedata']. Then just echo the response for the Ajax request (JSON or XML), and execute some JS code for the Uploadify 'onComplete' event.
You can try using HTML 5's file upload API, which will require they drag and drop the file into the browser window. Read more here: http://www.appelsiini.net/2009/10/html5-drag-and-drop-multiple-file-upload
Additionally, you can do the same thing with Google Gears (which requires an install for IE but is default on Chrome).
It is possible to detect the presence of either and degrade gracefully.