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

时间:2015-01-02 21:18:07

标签: php file-upload upload

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

SAME 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";
}

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

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

我正在使用此代码上传:

//  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>