Hello friends, I need a help. Suppose, I have a upload form here -> www.mysite.com/upload.php (by this form I can upload files to my dir like here -> www.mysite.com/file/video) But, I want to upload file to a dir that located another server. Such as - My upload form : www.mysite.com/upload.php File upload dir : www.anothersite.com/file/video (I have ftp username & pass for www.anothersite.com) Is it possible? Please explain HOW. Thanks in advance.
You can make a ftp connection with php. <?php // set up basic connection $conn_id = ftp_connect($ftp_server); // login with username and password $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); // check connection if ((!$conn_id) || (!$login_result)) { echo "FTP connection has failed!"; echo "Attempted to connect to $ftp_server for user $ftp_user_name"; exit; } else { echo "Connected to $ftp_server, for user $ftp_user_name"; } // upload the file $upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); // check upload status if (!$upload) { echo "FTP upload has failed!"; } else { echo "Uploaded $source_file to $ftp_server as $destination_file"; } // close the FTP stream ftp_close($conn_id); ?> PHP:
Kaizoku, Your code works fine. thanks. I need little bit more with this script. I want that if the specified dir/folder doesn't exists then the script will create an dir/folder (that is specified) and upload file there. How can I check and make dir? Please help.
ftp_chdir should return a true or false if it can or can't change to that directory, which should then give you an idea if the dir already exists. but this is not always true. there might be other reasons why it can't change to a specified directory aside from it not existing yet, so you should have something to watch on the errors returned by the function.
You can always use ftp_nlist to return array of files and folders, then just cross reference if it exists or not. Or You can just do ftp_mkdir, it will only create the directory if it doesn't exist... (I think)
I want to create a text (.txt) file in ftp dir and write something inside that file by php. How can I do that?
<?php $fp = fopen('data.txt', 'w'); fwrite($fp, '1'); fwrite($fp, '23'); fclose($fp); // the content of 'data.txt' is now 123 and not 23! ?> Code (markup): From the php manual. You can google for more .