【问题标题】:ftp_put(): while adding an image to a separate server, it is also adding it to the local serverftp_put():将图像添加到单独的服务器的同时,也将其添加到本地服务器
【发布时间】:2011-06-06 22:56:52
【问题描述】:

这是您的日常上传和裁剪“个人资料”图片脚本/功能。有人浏览他们的计算机并上传图像。然后将该图像上传到单独的服务器。在此之后,为他们“裁剪”相同的图像,然后保存该新版本并删除旧的较大图像。除了一个小细节,我的一切都运行良好......

一旦他们保存裁剪后的版本,它也会保存到主目录中的本地服务器。我不知道为什么它会这样做,因为它专门设置为仅上传到另一个。

这里是信息:

$upload_dir = "profile_images/". $username;         // The directory for the images to be saved in
$upload_path = $upload_dir."/";             // The path to where the image will be saved
$large_image_name = "resized_pic.jpg";      // New name of the large image
$thumb_image_name = $userid ."00". $i .".jpg";  // New name of the thumbnail image
$max_file = "200000";                       // Approx 200kb
$max_width = "500";                         // Max width allowed for the large image
$thumb_width = "80";                        // Width of thumbnail image
$thumb_height = "80";                       // Height of thumbnail image

//Image Locations
$large_image_location = $upload_path.$large_image_name;
$thumb_image_location = $upload_path.$thumb_image_name;

这里是调整裁剪图像大小的函数:

function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale){
global $ftp_server;
global $ftp_user_name;
global $ftp_user_pass;
global $thumb_image_location;
global $large_image_location;
global $user;
global $userid;
global $username;
global $success;
global $error;

$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_pasv($conn_id, true);

if ($login_result) {
    $newImageWidth = ceil($width * $scale);
    $newImageHeight = ceil($height * $scale);
    $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
    $source = imagecreatefromjpeg($image);
    imagecopyresampled($newImage,$source,0,0,$start_width,$start_height,$newImageWidth,$newImageHeight,$width,$height);
    imagejpeg($newImage,$thumb_image_name,90);
    // chmod($thumb_image_name, 0777);

    $upload = ftp_put($conn_id, $thumb_image_location, $thumb_image_name, FTP_BINARY);// upload the file
    ftp_chmod($conn_id, 0777, $thumb_image_location);

    if ($upload) {
        // User DB Queries
        $time = strtotime("now");
        $time = $time + 3600;
        $date = date(c, $time);
        $path = 'http://www.otherserver.net/'. $thumb_image_location;

        $fileQuery = "INSERT INTO `avatars`(userid, username, path, date) VALUES('$userid', '$username', '$path', '$date')";
        $fileResult = mysql_query($fileQuery);
        $userQuery = "UPDATE `users` SET avatar = '$path' WHERE username = '$user'";
        $userResult = mysql_query($userQuery);

        if ($fileResult && $userResult) {
            $success = 'Your avatar has been uploaded successfully.';
        } else {
            $error = 'Something went wrong with your upload.';
        }
    }
}
ftp_close($conn_id); // close the FTP stream
}

最后,这里是函数的调用时间:

if (isset($_POST["upload_thumbnail"]) && strlen($large_photo_exists) > 0) {
    //Get the new coordinates to crop the image.
    $x1 = $_POST["x1"];
    $y1 = $_POST["y1"];
    $x2 = $_POST["x2"];
    $y2 = $_POST["y2"];
    $w = $_POST["w"];
    $h = $_POST["h"];
    //Scale the image to the thumb_width set above
    $scale = $thumb_width/$w;
    resizeThumbnailImage($thumb_image_name, "http://otherserver.net/". $large_image_location,$w,$h,$x1,$y1,$scale);

}

再一次,一切正常,除了它将最终(裁剪)的图像也保存到本地服务器(是的,它也将它保存到另一个服务器,所以这部分很好)。

【问题讨论】:

  • 全部修复。我只需要“取消链接”上传到本地服务器的图像。

标签: php upload ftp


【解决方案1】:

好吧,你做两件事:

  • 使用imagejpeg在磁盘上创建JPG,名为$thumb_image_name
  • 使用ftp_put 将 JPG 上传到 FTP 服务器

您永远不会删除本地文件,所以它仍然存在。

【讨论】:

  • 说得通哈哈...有没有办法让 imagejpeg() 在 ftp 服务器上工作?所以它根本不上传到本地?我不希望本地服务器发生任何事情,我需要在 ftp 服务器上完成所有操作:/ 我试着让它 $thumb_image_name = imagejpeg($newImage,$thumb_image_name,90);但它失败了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-31
  • 2017-11-10
  • 2011-03-16
  • 1970-01-01
  • 2014-09-04
  • 2018-01-01
相关资源
最近更新 更多