ThaiSEOBoard.com

พัฒนาเว็บไซต์ => Programming => ข้อความที่เริ่มโดย: smkaffiliate ที่ 31 ธันวาคม 2010, 02:31:50



หัวข้อ: สงสัยการ ftp จาก host โดย php ขอความช่วยเหลือหน่อยจ้า
เริ่มหัวข้อโดย: smkaffiliate ที่ 31 ธันวาคม 2010, 02:31:50
เรื่องก็คือว่าผมต้องการ ftp ด้วยสคริปต์ php ซึ่งผมก็ลองไปค้นหาจากพี่ Goo (Google) แล้วก็ใช้ได้ปกติถ้า ftp ผ่านฟอร์ม
และผู้ใช้เป็นคนเลือกไฟล์จากในเครื่องตัวเองขึ้นไปนะครับ

แต่คราวนี้ที่ผมต้องการคือว่าผมอยากจะให้มันเป็นแบบอัตโนมัิติ คือทำการ ftp ไฟล์จาก host A ไปใส่ host B โดยไม่ต้อง
ผ่านฟอร์มหน่ะครับ

ปัญหาก็คือ ผมไม่รู้ว่าจะเรียกหา path ไฟล์หรือเก็บค่าไฟล์ใน host A ไว้ในตัวแปรอย่างไร ก่อนจะทำการ ftp ส่งเข้า host B
ครับ ตัวอย่างปัญหานะครับ

ที่ Host A ผมเก็บไฟล์ที่ต้องการ ftp ไว้ที่ landingpage/test.php
ที่ Host B ผมแค่ต้องการเอาไฟล์ test.php ขึ้นไปไว้ใน root ก็ได้แล้วครับ

นี่คือตัวสคริปที่ผมทดสอบแบบ ftp ผ่านฟอร์มได้ปกติครับ

โค๊ด:
<?php
if($upload){
//change these values to suit your site
$ftp_user_name='ssmk';
$ftp_user_pass='111111';
$ftp_server='ftp.domain.com';
$ftp_dir='domain.com/';

//$web_location is needed for the file_exists function, the directories used by FTP

$web_dir='doman.com/';
$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$imagefileFTP_BINARY);

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
}

//prepare a form similiar to this and have it call the below file
echo '<form action="ftp.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>';

?>

ผมอยากขอความช่วยเหลือว่า ถ้าไม่เอาฟอร์มแต่ไปดึงไฟล์จากใน host A เลยตรงค่า $imagefile จะรับมาอย่างไรดีครับ

ขอบคุณทุกท่านมากครับ
 :wanwan017: :wanwan017:


หัวข้อ: Re: สงสัยการ ftp จาก host โดย php ขอความช่วยเหลือหน่อยจ้า
เริ่มหัวข้อโดย: kobkung ที่ 31 ธันวาคม 2010, 12:35:48
โดยปกติทำไม่ได้หรอกครับ การโอนข้ามเครื่องต้องได้รับการอนุญาตก่อน ซึ่งบาง host เขา lock ไว้
อย่าง ftp program ทั่วไป ถ้ามีตัว ข้าม server ได้ เป็น transfer server บางทีก็ทำไม่ได้ เพราะ host เขาไม่ให้
ส่วนโค้ดไม่ได้ยากครับ เท่าที่เขียนมาก็น่าจะใช้ได้ ให้ระบุเป็น ip กับ home path


หัวข้อ: Re: สงสัยการ ftp จาก host โดย php ขอความช่วยเหลือหน่อยจ้า
เริ่มหัวข้อโดย: DJJome ที่ 01 มกราคม 2011, 07:58:20

- hard code กำหนดลงไปในสคริปต์เลย ถ้าไม่รู้ path ลองคำสั่ง dirname(__FILE__); มันจะบอกโดยอ้างอิงจากตำแหน่งที่ไฟล์นี้ถูกเรียกใช้
- หลายโฮสต์ ไม่ให้ใช้คำสั่ง ftp ผ่านทาง php ให้ลองหาโค้ดใหม่ที่ใช้ curl ในเน็ตมีเยอะครับ หาดู เริ่มที่ phpclasses.org ก็ได้

โชคดีปีใหม่ครับ