heed help about FTP upload

Discussion in 'PHP' started by mahdi_fci3, Jul 14, 2010.

  1. #1
    hi , for a long time i am searching FTP upload image, I get more codes, but I dont know untill now why I get an error message, here is one of the codes I get:

    <?php
    //FORM page
    //----------------form.php---------------------------------

    echo '<form action="upload_page.php" method="post" enctype="multipart/form-data">';
    echo 'Click the Browse button to find the file you wish to upload';
    echo '<input type="file" name="imagefile">';
    echo '<INPUT TYPE="submit" name="upload" value="upload">';
    echo '</form>';



    //upload page
    //--------------------upload_page.php------------------------------------

    //change these values to suit your site
    $ftp_user_name='xxx';
    $ftp_user_pass='xxx';
    $ftp_server='ftp.sitename.com';
    $ftp_dir='/sitename.com/public_html/images/';
    //$web_location is needed for the file_exists function, the directories used by FTP
    //are not visible to it will will always return not found.
    $web_dir='../images/';
    $web_location=$web_dir.$imagefile_name;

    //build a fully qualified (FTP) path name where the file will reside
    $destination_file=$ftp_dir.$imagefile_name;

    // connect, login, and transfer the file
    $conn_id = ftp_connect($ftp_server);
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    $upload = ftp_put($conn_id, $destination_file, $imagefile, FTP_BINARY);

    //use ftp_site to change mode of the file
    //this will allow it be visible by the world,
    $ch=ftp_site($conn_id,"chmod 777 ".$destination_file);
    // close the FTP stream
    ftp_close($conn_id);

    //verify file was written
    if (file_exists($web_location))
    {
    echo "file was uploaded as $web_location";
    }
    else
    {
    echo "Could not create $web_location";
    }
    //end if



    ?>

    I wait your answer please
     
    mahdi_fci3, Jul 14, 2010 IP
  2. dbsuk

    dbsuk Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this function, it works for me:

    
    function ftpUploadFile($ftp_server,$ftpuser,$ftppass,$source_file,$destination_file) {
    
    // set up basic connection
    $conn_id = ftp_connect($ftp_server);
    
    // login with username and password
    $login_result = ftp_login($conn_id, $ftpuser, $ftppass);
    
    // the ftp function
    ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); 
    
    // close the connection
    ftp_close($conn_id);
    
    }
    
    // example usage
    
    // ftp
    $ftphost = 'ftp.somesite.com';
    $ftpuser = 'user';
    $ftppass = 'password';
    $ftpfolder = 'path/to/folder/';
    
    // filename
    $filename = 'filename.ext';
    
    // call to ftpUploadFile() function
    ftpUploadFile($ftphost, $ftpuser, $ftppass, $filename , $ftpfolder.$filename );
    
    
    PHP:
    Hope this helps.
     
    dbsuk, Jul 14, 2010 IP
  3. mahdi_fci3

    mahdi_fci3 Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you dbsuk for your time,
    but when I used your code I get this message error:
    Warning: ftp_put(filename.txt) [function.ftp-put]: failed to open stream: No such file or directory in /home2/ftpusername/public_html/ftppage.php on line 8
    where the $ftpfolder = 'www.sitename.com/public_html/images/'; or $ftpfolder = '/sitename.com/public_html/images/';

    I would like to say also, if I need to use a form, can I replace filename.txt to $filename = $_FILES['myfile']['name'] ;
     
    mahdi_fci3, Jul 15, 2010 IP
  4. dbsuk

    dbsuk Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It looks to me like the script is searching for a file call filename.txt in your /home2/ftpusername/public_html/ folder (your site public folder)

    I think its searching for the file here /home2/ftpusername/public_html/filename.txt and not finding it.

    Try putting a filename.txt in your public folder and see if the error goes away.

    As for your second question you can use the file directly from the form:

    
    
    // example usage
    
    // ftp
    $ftphost = 'ftp.somesite.com';
    $ftpuser = 'user';
    $ftppass = 'password';
    $ftpfolder = 'path/to/folder/';
    
    // filename
    $filename = $_FILES['myfile']['tmp_name'] ; // use tmp_name not name
    $detination_filename =  $ftpfolder.$_FILES['myfile']['name'] ; 
    
    // call to ftpUploadFile() function
    ftpUploadFile($ftphost, $ftpuser, $ftppass, $filename , $detination_filename );
    
    PHP:
     
    dbsuk, Jul 15, 2010 IP