jquery ajax file upload , parameters to be sent - php

<form action="upload_file.php" method="post" enctype="multipart/form-data">
Above is the line for a form to upload a file using php.
If i use the form as I wrote above I can get the various properties of the file to be uploaded from upload_file.php page.
Using jquery $.post , how can I do the same thing in upload_file.php ?
I googled but can't find the exact snippet of code necessary here.

Uploading images using jquery's ajax methods is a pain, generally involving dynamically creating an iframe and submitting a form inside it. I usually use this plugin: http://plugins.jquery.com/project/jquery-upload
You can write some php to echo the uploaded image data as a json object, which you can access once the upload is complete through the plugin.

Related

php file upload not working with jquery Validator

I'm using the jquery validator to validate that my form, but it doesn't upload the file properly.
If I add enctype="multipart/form-data" to the form it uploads the file but
doesn't grab the form data passed.
Well I solved the problem I changed.
$the_field = $_REQUEST['field'];
$the_field = $_FILES['field']['name'];

Multiple post requests for same form

I have a form as:<form action="test.php" method="post" name="testForm">
And I have validations of image upload in another file "upload.php" which has some code as:
if (in_array($_FILES['myfile']['type'], $types)) {
if(filesize($_FILES['myfile']['tmp_name']) < $max_size) {
$destination_path = getcwd().DIRECTORY_SEPARATOR."images".DIRECTORY_SEPARATOR;
So is there any way by which I can call the upload.php file without altering test.php file from form action attribute and perform my image upload validations.
you could use
include('upload.php');
OR
by using copy of the function into test.php
For the record, it would actually be better to use a jquery function which would submit the form to upload.php and then to test.php.
If you can alter the HTML code of your form, you could post to upload.php instead of test.php and then repost from upload.php to test.php (using CURL, for example)
If I get you right there are many ways. I recommend reading a bit in the manual:
include
functions.
class
filter

Getting Uploaded Image Name into a Database

I'm using this image uploader:
http://www.jotform.org/jquery/ajax-multi-file-upload-with-progress-bar/#more-291
It's jQuery/Ajax, while I have it working I know little about either language. I want to modify it so that the filenames of all uploaded files get put into a database.
The included upload.php file doesn't appear to work as I've deleted that and the uploader is still functioning. If that was required then I could quite easily write the required PHP to do the job.
One solution would be for a PHP file to be executed after the upload finishes, how can I do that?
Appreciate any help here.
<form id="upload" method="post" action="upload.php" enctype="multipart/form-data">
<div id="drop">
Drop Here
<a>Browse</a>
<input type="file" name="upl" multiple />
</div>
<ul>
<!-- The file uploads will be shown here -->
</ul>
</form>
Edited above - this is the form that the upload uses, is there anyway I could use an "isset" to check it's been used, there's no submit button though.
The above code is what I've got on the page with the uploader on it, it doesn't seem to actually call the upload.php file.
I've not used this file uploader before. Having looked at it though, upload.php is responsible for processing the file after upload.
If you look at this script:
if(move_uploaded_file($_FILES['upl']['tmp_name'], 'uploads/'.$_FILES['upl']['name'])){
echo '{"status":"success"}';
exit;
}
You can see that on successful upload, it moves the file to the included uploads directory. It's at this point that you could extend this script and save $_FILES['upl']['name'] to a database. You might want to look in to the possible PHP database extensions
$('#upload').fileupload({
// This element will accept file drag/drop uploading
dropZone: $('#drop'),
// This function is called when a file is added to the queue;
{***SNIP***}
});
// Automatically upload the file once it is added to the queue
var jqXHR = data.submit();
},
The second to last line var jqXHR = data.submit(); is when it submits the form. That's the AJAX part of it. Since you deleted the upload.php file, it actually gives a 404. However, the code was poorly written and doesn't seem to alert you it cant reach said file.
Nonetheless, upload.php IS getting called, and IS being used. As someone else above suggested, you can extend that to get any details about the uploaded files you need. (And actually, I would put any logic before the echo. That echo is what tells the JavaScript everything is hunky-dorey.)

how to upload file php through jquery and smarty

i need to upload a file doc or pdf or rich text through jquery and smarty
i try to pass the file in jquery is given below
window.location.href= sitePath+'download?p='+$('#file').val();
but the p doesn't have value. how to get the file path or name and how can stored in server
in controller i write just pass the query to model is given below
$model=new download();
$id=$model->upload($param);
and i can't develop the model code....
please help me
You can't send files like that.
You should either submit a form with enctype="multipart/form-data" or use some AJAX uploader (which uses form submit to iframe and then stores a file using PHP script).
EDIT: Some references
http://www.w3schools.com/php/php_file_upload.asp - tutorial on how to upload a file using PHP
http://valums.com/ajax-upload/ - example of ajax uploader.
EDIT2: The problem is not with your model code but with the way you try to submit a file.

Upload a file, but not inside a form

Upload a file, but not inside a form
For example :
<input type="file" name="taskuploadfile" />
<input type="button" name="taskupload" value="Task Upload" onclick="taskupload()" />
Using Ajax for javascript to php.
Can I get temp path of file in php or Is it possible ?
Use a library that can do it the best way that you can not reach even after a month of coding.
One of the best examples is JqUploader.
Here is the example: http://pixeline.be/experiments/jqUploader/test.php
You can use the File API to read the data and then send it via XHR as per an example on MDN or by using XHR2 as per HTML 5 Rocks' example.
These methods do have limited browser support though, so you are probably still better off using a real form and submitting to an iframe for the time being.
Since it looks like you are using dojo (from the tags you put in your post)... why not using the HTML5 multi-file upload widget ? It has a plugin to do ajax uploads...
More on that here : http://dojotoolkit.org/documentation/tutorials/1.6/uploader/
Reference documentation here : http://dojotoolkit.org/reference-guide/dojox/form/Uploader.html
It is not possible to do a file upload with ajax. The only trick is to use an IFRAME. And since you want to POST something to the server, you have to use a form tag.

Categories