#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0

    PHP ftp upload from html form


    PHP Code:
    $host 'XXXXXXXXXXX'
    $usr 'XXXXXXXXXXX';
    $pwd 'XXXXXXXXXXX';
    $destDir $_POST['filetype'];
    $conn_id=ftp_connect($host);
    $success ftp_login($conn_id,$usr,$pwd);
    $rightName basename($_FILES['uploadfile']['name']);
    ftp_pasv($conn_idtrue);
    if(
    $success)

        
    ftp_put($conn_id,$destDir.'/'.$rightName,$_FILES['uploadfile']['tmp_name'],FTP_BINARY) or die('Cannot ftp');
        
    ftp_close($conn_id);
        echo 
    ' Upload Successful';

    The form I am using for upload is this:

    Code:
    <form enctype="multipart/form-data" action="upload.php" method="POST">
        Type of file: <input name="filetype" id="filetype1" /><br />
        Choose a file to upload:<input name="uploadfile" type="file" /><br />
        <input type="submit" value="Upload File" />
    I have tried multiple options. 'ftp_put' from php's tmp upload location to a working directory and then to a destination directory and 'ftp_put' directly from temporary upload directory of PHP to the desired directory but FTP for some reason does not seem to work at all. It just simply dies. A normal upload using 'move_uploaded_files()' works perfectly. In some of the forums it is mentioned that for ftp to work I can't have 'file' input type and just have a plain text box. I don't understand why that needs to be the case or even if it true. Why is the above code not working? I have checked the permissions on the destination folder and it should not be the issue.
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Please paste your code into the big box, highlight it, THEN click the white "PHP" button. The way you've done it forces your code to be all on one long line, and it can't be read.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by ManiacDan
    Please paste your code into the big box, highlight it, THEN click the white "PHP" button. The way you've done it forces your code to be all on one long line, and it can't be read.
    I think this looks better. Apologies for the inconvenience.
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    There's no error handling here, are you sure FTP is connecting, verifying the user properly, and accepting file uploads? Check the return value of each of these functions.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by ManiacDan
    There's no error handling here, are you sure FTP is connecting, verifying the user properly, and accepting file uploads? Check the return value of each of these functions.
    Yes, I have checked each of those. '$success' returns true. The error message I get is 'Cannot ftp'. So I assume it is the 'ftp_put' step. And the site is hosted by 'mediatemple' and they have a dedicated ftp server. And I am not sure how much this is relevant, but a normal http upload to the same directory works, but I want to use ftp to overcome any size restrictions that might be in place.
  10. #6
  11. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    I presume you've tried to manually FTP a file to the host via command line or app?

    Just to make sure it works at all.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by dmittner
    I presume you've tried to manually FTP a file to the host via command line or app?

    Just to make sure it works at all.
    My apologies for having wasted your valuable time. Unfortunately, my hosting provider provided me the wrong documentation and I was trying to upload the file into a path that was non-existent. FTP root was pointing to a different path to the one that was specified in the documentation and hence the trouble.

    @dmittner: Hats off sir. I had not tried the manual FTP until you posted(silly?! ) and I did a quick check with FileZilla which helped me detect the incorrectness in the documentation. I just tried with the corrected path and ftp works correctly now. Thanks a lot.
  14. #8
  15. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Originally Posted by saikrishnan7
    My apologies for having wasted your valuable time.
    A couple minutes spent helping someone overcome a problem that's taken them much longer, is time well spent. So no worries. We've all been there. It's a learned troubleshooting strategy to try things more directly, if PHP's having a problem with it.

IMN logo majestic logo threadwatch logo seochat tools logo