Jump to content

Creating temp file and transfering through ftp


AtomicRax

Recommended Posts

From the beginning, the user accesses my website and uploads an image.

 

Currently I'm using SimpleImage.php available at http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php to resize uploaded pictures.

 

I create two copies, a regular size and a thumbnail size. Then I transfer the converted files through FTP to my storage server. The regular size works great, but the thumbnail transfers as a blank file. I don't want to save the files on the non-storage server at all, so I'm trying to work with temporary files.

 

include('./SimpleImage.php');

// A few variables
$file_name = basename($_FILES['photo1']['name']);
$random_digit=mt_rand(111111111,2147483647);

// New file name
$new_file_name = $userInfo['id'].'_'.$random_digit.$random_digit.'_'.substr($file_name, -5);

// Paths to temporary files
$target_path = $_FILES['photo1']['tmp_name'];
$thumb_path = tmpfile();

// Start SimpleImage and load temp file
$simImage = new SimpleImage();
$simImage->load($target_path);

// Get the temp image dimensions
list($width, $height) = getimagesize($target_path);

// Resize the file if it's too tall
if ($height > $var['maxPhotoHeight']) {
   $simImage->resizeToHeight($var['maxPhotoHeight']);
   $simImage->save($target_path);
   }

// Get the newly resized image dimensions
list($width, $height) = getimagesize($target_path);

// Resize the image if it's too wide
   if ($width > $var['maxPhotoWidth']) {
   $simImage->resizeToWidth($var['maxPhotoWidth']);
   $simImage->save($target_path);
   }

// Resize for thumbnail width
   $simImage->resizeToWidth($var['thumbWidth']);
   $simImage->save($thumb_path);

// Get thumbnail dimensions
list($width, $height) = getimagesize($thumb_path);

// Resize the thumbnail if it's too tall
if ($height > $var['thumbHeight']) {
   $simImage->resizeToHeight($var['thumbHeight']);
   $simImage->save($thumb_path);
   }

// Was told this was important, but I've tried with and without it and got the same result
fseek($thumb_path,0);

// Perform File Transfer
ftpTransfer($target_path, "$new_file_name");
ftpTransfer($thumb_path, "thumbs/$new_file_name");



// My Simple FTP Transfer Function:
function ftpTransfer($source, $name) {
global $ftp;

$ftpMeta = "ftp://" . $ftp['user_name'] . ":" . $ftp['user_pass'] . "@" . $ftp['server'] . $ftp['dir'] . $name;
$ftpData = file_get_contents($source);

file_put_contents($ftpMeta, $ftpData);
}

 

Like I said, the regular sized image works great!! the thumbnail file is only created on the storage server but it has a 0kb file size. I'm sure it has to do with me working with a temporary file. tmpfile() claims to remove the file after it's closed, so maybe by using the Save feature in SimpleImage, it's closing the file and then the server automatically removes the temporary file?

 

SimpleImage has a feature where I can write the image output [possibly to a file] using $simImage->output() for the data. If that helps with a solution? I don't know.

 

I know it might be complicated, but any help is appreciated!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.