tags:

views:

30

answers:

3

Hi,

I am using the code below to upload an image through ftp

$sFile=$ftp_dir."/".$image_name;

$image=$database_row["image"];//image is store in database

$fh = tmpfile();
$fwrite($fh, $image);

$uploadFile = ftp_fput($conn_id, $sFile, $fh, FTP_ASCII); 

fclose($fh);

The ftp is creating the file and has a size BUT the file i get is not an image.When try to open on image viewer i get error.

Before switch to ftp i had this code

$image=$database_row["image"];//image is store in database
   $file = fopen( "images/".$image_name, "w" );
   fwrite( $file, $image);
   fclose( $file );

and was working fine, but now i have to use ftp.

What am i missing.

+1  A: 

Try using FTP_BINARY instead of FTP_ASCII. If all else fails, open the resulting file with a hex editor.

nc3b
+1  A: 

you are telling ftp to read the image as ascii (text) change it ot FTP_BINARY.

aleo
+1  A: 

You need to fseek to the beginning of the file after writing content to it and you need to use binary upload mode:

$sFile=$ftp_dir."/".$image_name;
$image=$database_row["image"];//image is store in database
$fwrite($fh, $image);
fseek($fh, 0);
$uploadFile = ftp_fput($conn_id, $sFile, $fh, FTP_BINARY); 
fclose($fh);
zaf