Uploading a file using Ajax and PHP's FTP?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Uploading a file using Ajax and PHP's FTP?

  1. #1
    Join Date
    Mar 2006
    Posts
    4

    Question Uploading a file using Ajax and PHP's FTP?

    Good day, First of all I am as happy as monkey in a tree with the discovery of this forum dedicated to Ajax!

    Ok, my situation is as follows. I have a php script that sends an email. This is used in a type of "employment section" of the website where a document can be "uploaded" by the user (his or her cv). The mentioned script then uploads the specified document (does all the checks on size and type etc..) and then adds the document to the email as an attachement.
    PS: the "employment page" has a normal form as mentioned above with a <input type="file" ...> field as well.

    Currently it works with the whole: <form action="phpFile.php" ......>. The page moves to the specified php and then i use the $_FILES array in the php to upload the file via php's ftp functions and then attach it to the soon-to-be-sent email.

    I have manged to get the form to be validated first when the 'submit' button is pressed.
    Feedback on the validation also happens in an ajax kind of way.
    After the form is validated i can send the details to the php file via ajax, thus all happens in backend without page 'reloading', again feedback then happens 'dynamically' ie. "sorry could not send" or "thank you for sending" without anything having to be refreshed.
    The email actually sends but there is no attachement to the email.

    I am having trouble getting the value of the "file" input field to the php so that i may use the $_FILES array in the php file.
    The only way i can get a value to the php file is to pass the "fileField.value" as a parameter but then the php gets a LONG string of the local directory of the given document, which is not what i want.

    So does anyone know how to get the "file" input-field value from the form to the php via ajax (variouse javascript methods) so that i can get to use php's $_FILES array?
    PS: the $_FILES array is used to access elements needed by php's ftp ie:
    PHP Code:
     $name$_FILES['filename']['name']; //name of doc
    $tmp_location$_FILES['filename']['temp_name'];//temp reference of the file on the server
    .....etc 
    I am aware that part of the problem is php orientated but all my php forums elswhere give me odd looks when i ask the ajax question of above! So now i ask ajax people what i believe to be an ajax orientated question.

    I ask for any assistance, even if it is to be directed to a place where i can read or research the problem.

    Thank you upfront.

  2. #2

    Re: Uploading a file using Ajax and PHP's FTP?

    What does your AJAX request look like? It sounds like a GET instead of a POST...

  3. #3
    Join Date
    Mar 2006
    Posts
    4

    Re: Uploading a file using Ajax and PHP's FTP?

    ok let me show the steps with the relevant code snippets.

    When the submit button is pressed, a local function in the html page 'sendMail(..)' is called. It's parameters are the values of the input fields of the form. It does validation checks and then passes to the Ajax.

    PHP Code:
    function sendMail(formfnamelnamecnummail)
    {
        if(
    validFields(getTag(form),form))
        {
            
    document.getElementById('result').innerHTML "Processing Your Message...";  // To show the user the code is working while the ajax does its work
            
    sendEmail(fnamelnamecnummail); // Ajax call/request
            
    clearForm(form); // clears all values of the form fields
        
    }
        return 
    false;

    If the form is classified valid, the ajax request is called: sendEmail(..). Again its parameters are the form values. The function is in an external Javascript file "included" into the html file mentioned with previouse function.
    It looks as follows:
    PHP Code:
    function sendEmail(fname,lname,cnum,email)
    {        
    http.open('POST','emailsend_contact.php?nullv=0&fname='+fname+'&lname='+lname+'&email='+email+'&cnumber='+cnum);
              
    http.onreadystatechange handleEmail;
              
    http.setRequestHeader('Content-Type''application/x-www-form-urlencoded');
              
    http.send('emailsend_contact.php?nullv=0&fname='+fname+'&lname='+lname+'&email='+email+'&cnumber='+cnum);  

    Please note from the above function that this is from my attempts to figure out how to access the "file" information without passing the fileField's value directly to the function.

    Thank you, feel free to ask for any other information if needed!

  4. #4

    Re: Uploading a file using Ajax and PHP's FTP?

    Forgive me, but where does the e-mail attachment come into play? It doesn't look like it's ever added to the request...

  5. #5
    Join Date
    Mar 2006
    Posts
    4

    Re: Uploading a file using Ajax and PHP's FTP?

    The attachement to the email message happens in the php script. The php script uploads the specified file via php's ftp functions to the server, then a specific message is generated and finally the uploaded doc is attached to the email message, and sent using phpMailer, then deletes the uploaded file/doc.

    All i want to do is get the needed info from the html form to the above mentioned php script so that i can do all the above mentioned.
    .:The Art of War is to win without the need for conflict.:.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center