image handle -> file handle

Alexej Kubarev

New member
well.. most ftp directories have this path:

so why not set that path to some variable and try uploading? I have never tested that myself..
One more thing... dont forget that you MUST have rights to write to that directory
Firstly, let me preface this with some advice: You're a lot better off just writing the image to a file and using the FTP functions to upload the file. I don't know why you're so averse to that; it doesn't tax the server overmuch, and it doesn't take up any space assuming you delete the local copy once you're done with it. That said, this was an interesting exercise, so here's what I came up with.

Your problem is two-fold. First, you have to get the image data into a string (something which PHP's image functions weren't designed for), and secondly you need to write that string directly to a file on a remote FTP server (something which PHP's FTP functions weren't designed for)

So for the first part of the problem, we turn to the modern wonder known as Output Buffering. With imagejpeg() you have two options: either you can write the image data to a file, or you can output the data to the browser. Since you don't want to do the first for some reason, that leaves us option number two. But with output buffering, we can capture the output before it is sent to the browser and instead put its contents into a string. The code to do so would look something like this:

// all your image stuff (except the final imagejpeg()) goes up here

ob_start(); // start output buffering
  imagejpeg($img_resource);  // output the image data to the buffer
  $data = ob_get_contents(); // copy the buffer to a string
ob_end_clean(); // delete the buffer and end buffering

After the above code, we have the JPEG data in a string, all without having to write a file our output anything to the browser.

The second part is to take that string and write it to a file on an FTP server. This is a bit trickier because it depends on one thing: that in PHP.ini you have the allow_url_fopen setting turned on. If you're running in a shared hosting environment and the option is turned off, then sorry, you're out of luck.

If you have allow_url_fopen enabled, then we can open a file on an FTP server as a stream and treat it like any other file (well, mostly) and manipulate it with the fopen() and fread() functions. There's lots of documentation here about what you can and cannot do with FTP streams. If you have any problems with this part, check (and double-check) there first before asking about it here.

So basically what we have to do is open a stream to the file on our FTP server (it will be created automatically if it doesn't exist) using fopen(), write the contents using fwrite(), and we're done! The rest of the code, then, would look like this:

  $username = 'your_ftp_username';
  $password = 'your_ftp_password';
  $filename = 'somefile.jpg'; // the name of the file you want to create/write

  if(!$handle = fopen("ftp://$username:$$filename", 'w')) {
    // if the file wasn't opened successfully, throw a fit
    echo 'Error opening file!';

  // now try to write the file
  if(fwrite($handle, $data) === false) {
    // if the file wasn't written successfully, throw a fit
    echo 'Cannot write to file.';
  // it worked!
  echo "File $filename written successfully!\n";