将图像文件上传到不同的服务器

upload image file to different server

我想在不同的服务器上传图片。图片正在上传中。但格式不正确。 对于这段代码,它可以工作文件。

同一台服务器(正在运行)

$file_path = "./media/";

$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
    echo "success";
} else{
    echo "fail";
}

另一台服务器(工作但未获取实际图像)

$file_name = $cus_id . ".jpg";
$file_path = CUS_PROFILE_PIC_DOCROOT . $file_name;

$ftp_server = "some_address";
$ftp_user = "username";
$ftp_password = "password";
$conn = ftp_connect($ftp_server) or die ("Cannot connect to host");
if (@ftp_login($conn, $ftp_user, $ftp_password)) {
    ftp_pasv($conn, true);
    $uploaded = ftp_put($conn, $file_path, $_FILES['uploaded_file']['tmp_name'], FTP_ASCII);
    ftp_close($conn);
    if($uploaded){
        echo "success";
    }
    else{
        echo "fail";
    }
} else {
    return "Couldn't connect as $ftp_user\n";
}

我做错了什么? 感谢您的帮助。

您必须对图像使用 FTP_BINARY,因为它们是二进制的。您应该只发送 ASCII 文本文件。

我正在使用此代码上传:

//  set_time_limit(0);
        $connection = ftp_connect("domain.com");
        $login = ftp_login($connection, "admin", "pass");
        if (!$connection)
            {
             die('Connection attempt failed!');
            }
        else{
             echo "connection passed";
             }
    if (!$login)
            {
             die('Login attempt failed!');
            }
            else{
            echo "login passed";
            }
    ftp_pasv($connection, true);

<p>    $source = "image.jpg";
    $dest = "folder/image.jpg";</p>
<p>    //$x = substr($dest,-4);
    // echo $x;</p>
<p>    if((substr($dest,-4) == ".jpg") || (substr($dest,-4) == ".png") )
{
 //no problems;
}
else {
die('only jpg or png.');
}</p>
<p>    $upload = ftp_put($connection, $dest, $source, FTP_BINARY);
    if (!$upload) { echo 'FTP upload failed!'; } else {echo "ftp upload passed";}</p>
<p>    ftp_close($connection);</p>
<p>?></p>