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
Related
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.)
I have a MyAccount page for my website which is going to have a form to allow the users to upload an image.
<from action='javascript:uploadFile'>
<input type='file' name='Upload'>
</form>
What I would like to do is have it call another .php file like:
<script type="text/javascript">
function uploadFile()
{
addPhoto.php giving files $id and Upload (from the form)
}
How can I achieve this without the user actually leaving the MyAccount page?
You can use include or require within a PHP file.
The include statement includes and evaluates the specified file.
You would need AJAX/jQuery to call it through JavaScript.
Documentation
I have the form tag written as such:
<form id="form" action="mail.php" method="post"><!--form here--></form>
Currently this poses some problems, so I am switching the form action to an AJAX solution, however, I would like the AJAX function to be in an external JavaScript file. I know that you can link the form action attribute to a file, but how would I tell the form to look at the specific function in the file?
Any help is greatly appreciated!
just include the script file using <script src="file.js" type="text/javascript" /> and call the function in file wherever you want... you don not need to tell the program where to look for function .. all the script code is included within the file with the use of <script>
you can use:
onsubmit="SubmitFormViaAjax();return false;"
This way the form won't submit itslef (return false) and you can use any function in the included form to submit the form's data directly to the recipient via ajax.
Just make sure to include the ajax functions file in the page.
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.
<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.