Using FTP via PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adamjblakey
    New Member
    • Jan 2008
    • 133

    #1

    Using FTP via PHP

    Hi,

    I am trying to upload some files using ftp via php.

    I have built a function which should work as far as i can see but it isn't. Can anyone see why this would not work?


    [PHP]
    function ftpupload($file , $destination){

    // set up the settings
    $ftp_server = "ftp.".$_SESSIO N['domain'];
    $ftpuser = $_SESSION['user'];
    $ftppass = $_SESSION['pass'];

    // delete the below variables if you decide to use a form.
    $source_file = $file;
    $destination_fi le = $destination;

    // set up basic connection
    $conn_id = ftp_connect($ft p_server);

    // login with username and password
    $login_result = ftp_login($conn _id, $ftpuser, $ftppass);

    ftp_put($conn_i d, "$destination_f ile", "$source_fi le", FTP_BINARY); // the ftp function

    // close the connection
    ftp_close($conn _id);

    }
    [/PHP]

    Then:

    [PHP]
    ftpupload($rand _name.$file_ext , "/uploads/images/thumbs/");
    [/PHP]

    Any Ideas?
    Cheers,
    Adam
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    [PHP]$ftp_server = "ftp.".$_SESSIO N['domain']; [/PHP] You don't need the 'ftp' bit for [PHP]ftp_connect($ft p_server); [/PHP]Just the address.
    Whether you use the IP address or the hostname is trial and error.
    It depends on the ftp set up

    Comment

    • adamjblakey
      New Member
      • Jan 2008
      • 133

      #3
      Thank you for your reply,

      I have tried using the IP but this did not work either. I have changed the script a bit now to see where it is failing and it seems to connect fine to the FTP. The problem is with sending the file.

      [PHP]
      function ftpupload($file , $destination){

      // set up the settings
      $ftp_server = "ftp.".$_SESSIO N['domain'];
      $ftpuser = $_SESSION['user'];
      $ftppass = $_SESSION['pass'];

      // delete the below variables if you decide to use a form.
      $source_file = $file;
      $destination_fi le = $destination;

      // set up basic connection
      $conn_id = ftp_connect($ft p_server);

      // login with username and password
      $login_result = ftp_login($conn _id, $ftpuser, $ftppass);

      //Check if login was successful
      if (!$login_result ){
      echo "Sorry there is currently a problem with our servers, please notify use of this problem or try again later.";
      exit();
      }

      $upload = ftp_put($conn_i d, $destination_fi le, $source_file, FTP_BINARY); // the ftp function

      // check upload status
      if (!$upload) {
      echo "FTP upload has failed!";
      exit();
      } else {
      echo "Uploaded $source_file to $ftp_server as $destination_fi le";
      exit();
      }

      // close the connection
      ftp_close($conn _id);

      }
      [/PHP]

      I keep getting this erro: FTP upload has failed!
      Cheers,
      Adam

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        I would suggest incorrect file path to either $file, $destination.
        Echo these values to see what is in there and check the source and remote directories exist.
        Be aware the ftp address is a specific folder and all paths are relative to that folder

        Comment

        • adamjblakey
          New Member
          • Jan 2008
          • 133

          #5
          Right i have given up doing it that way, i have however found a way that does upload but when the file is uploaded on the server it is 0kb but at least it is uploading.

          [PHP]
          function ftpupload($file ){

          $ch = curl_init();
          $localfile = $file;
          $fp = fopen($localfil e, 'r');
          curl_setopt($ch , CURLOPT_URL, 'ftp://'.$_SESSION['name'].':'.$_SESSION['pass'].'@87.229.14.87/httpdocs/uploads/images/thumbs/'.$file);
          curl_setopt($ch , CURLOPT_UPLOAD, 1);
          curl_setopt($ch , CURLOPT_INFILE, $fp);
          curl_setopt($ch , CURLOPT_INFILES IZE, filesize($local file));
          curl_exec ($ch);
          $error_no = curl_errno($ch) ;
          curl_close ($ch);
          if ($error_no == 0) {
          print 'File uploaded succesfully.';
          } else {
          print 'File upload error.';
          }

          }
          [/PHP]

          Any Ideas why it is 0kb?

          Comment

          • code green
            Recognized Expert Top Contributor
            • Mar 2007
            • 1726

            #6
            Could be a few things.
            File space full.
            Blocked by firewall.
            Corrupted file
            Not really hph problem.
            You original function indicated a file path problem.
            Did you investigate this?

            Comment

            • adamjblakey
              New Member
              • Jan 2008
              • 133

              #7
              Thank you for your reply, the disk space is not the problem as i have check this and it is fine. I don't have a firewall on so this cannot be the problem either. The file is not corrupt either.

              I did investigate this but i could not find out the problem even after hours of looking at it, and was feeling like i was going in circles.

              Cheers,
              Adam

              Comment

              • adamjblakey
                New Member
                • Jan 2008
                • 133

                #8
                Hi,

                I have come up with the solution to this now but have a little problem.

                I am now using this to upload the images.

                [PHP]
                function ftpupload($file , $img_tmp, $path){

                $message = '';
                if ($ftp = ftp_connect($_S ESSION['ddomain'])) {
                if (ftp_login($ftp , $_SESSION['dname'], $_SESSION['dpass'])) {
                ftp_pasv($ftp, true);
                if (ftp_put($ftp, $path . $file,
                $img_tmp, FTP_BINARY)) {
                $message = "File uploaded";
                } else {
                die("Could not upload file");
                }
                } else {
                die("Could not login to FTP account");
                }
                } else {
                die("Could not connect to FTP server");
                }
                }
                [/PHP]

                Now if i pass an image to this via a form it is fine, the problem i am having is i am running this function with another function which resizes images and renames it but the ftp function relies on an input of ['name'] and ['tmp_name'] to upload the image.

                This is the function for image rename and resize:

                [PHP]
                function uploadimage($va lue){
                $file_type = $value['type'];
                $file_name = $value['name'];
                $file_size = $value['size'];
                $file_tmp = $value['tmp_name'];

                //check file extension
                $ext = strrchr($file_n ame,'.');
                $ext = strtolower($ext );
                if (($extlimit == "yes") && (!in_array($ext ,$limitedext))) {
                echo "Wrong file extension. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
                exit();
                }

                //get the file extension.
                $getExt = explode ('.', $file_name);
                $file_ext = $getExt[count($getExt)-1];

                //create a random file name
                $rand_name = md5(time());
                $rand_name= rand(0,99999999 9);
                //get the new width variable.
                $ThumbWidth = 175;
                $ImgWidth = 700;

                //keep image type
                if($file_size){
                if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
                $new_img = imagecreatefrom jpeg($file_tmp) ;
                }elseif($file_t ype == "image/x-png" || $file_type == "image/png"){
                $new_img = imagecreatefrom png($file_tmp);
                }elseif($file_t ype == "image/gif"){
                $new_img = imagecreatefrom gif($file_tmp);
                }
                //list width and height and keep height ratio.
                list($width, $height) = getimagesize($f ile_tmp);
                $imgratio=$widt h/$height;
                if ($imgratio>1){
                $newwidth = $ThumbWidth;
                $newheight = $ThumbWidth/$imgratio;
                $newwidth2 = $ImgWidth;
                $newheight2 = $ImgWidth/$imgratio;

                }else{
                $newheight = $ThumbWidth;
                $newwidth = $ThumbWidth*$im gratio;

                $newheight2 = $ImgWidth;
                $newwidth2 = $ImgWidth*$imgr atio;
                }


                $resized_img = imagecreatetrue color($newwidth , $newheight);
                $resized_img2 = imagecreatetrue color($newwidth 2, $newheight2);
                imagecopyresize d($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
                imagecopyresize d($resized_img2 , $new_img, 0, 0, 0, 0, $newwidth2, $newheight2, $width, $height);

                //save image
                ftpupload($file _name, $file_tmp, "httpdocs/uploads/images/thumbs/");
                ftpupload($file _name, $file_tmp, "httpdocs/uploads/images/big/");

                imagedestroy($r esized_img);
                imagedestroy($r esized_img2);
                imagedestroy($n ew_img);


                }
                $value = "$rand_name.$fi le_ext";
                return $value;
                }
                [/PHP]

                This works fine as it is now as i am just passing the real image name but i don't know how to do it with the renamed image.

                Any ideas?
                Cheers,
                Adam

                Comment

                Working...