ftp_put(): while adding an image to a separate server, it is also adding it to the local server
This is your everyday upload and crop your "profile" pic script/function. Someone browses their computer and uploads an image. This image is then uploaded to a separate server. After this, that same image is brought up for them to "crop" and then saving that new version and removing the old larger image. I have this all working well, except one little detail...
Once they save the cropped version, it is also saved to the local server in the main directory. I can't figure out why it would be doing this as it's specifically set to upload to the other one only.
Here is the info:
$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;
Here is the function to resize the cropped image:
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 $开发者_Go百科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
}
Lastly, here is when the function is called:
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);
}
Again, everything is working correctly, except it saving the finalized (cropped) image to the local server as well (yes, it is saving it to the other server too, so that part is fine).
Well, you do two things:
- Create the JPG on disk with
imagejpeg
, called$thumb_image_name
- Upload the JPG to an FTP server with
ftp_put
You never remove the local file, so it is still there.
精彩评论