简体   繁体   English

PHP上传图像与FTP问题

[英]php upload image with ftp problem

I am using the code below to upload an image through ftp 我正在使用下面的代码通过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. ftp正在创建文件,但文件大小不一,但我得到的不是图像。尝试在图像查看器上打开时出现错误。

Before switch to ftp i had this code 在切换到ftp之前,我有此代码

$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. 并且工作正常,但现在我必须使用ftp。

What am i missing. 我想念什么。

Try using FTP_BINARY instead of FTP_ASCII. 尝试使用FTP_BINARY而不是FTP_ASCII。 If all else fails, open the resulting file with a hex editor. 如果所有其他方法均失败,请使用十六进制编辑器打开生成的文件。

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);

您告诉ftp以ascii(文本)格式读取图像,请通过FTP_BINARY进行更改。

//打开被动模式,然后它将正常工作

  ftp_pasv($conn_id, true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM