php upload image with ftp problem
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开发者_如何学C 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.
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);
Try using FTP_BINARY instead of FTP_ASCII. If all else fails, open the resulting file with a hex editor.
you are telling ftp to read the image as ascii (text) change it ot FTP_BINARY.
//turn passive mode on then it will work fine
ftp_pasv($conn_id, true);
精彩评论