I am using this code to upload via a batch file:
@echo off
echo user [email protected]> ftpcmd.dat
echo password>> ftpcmd.dat
echo cd public_html/new/data>> ftpcmd.dat
echo put abc.pdf>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat 11.111.111.111
del ftpcmd.dat
If I upload this same file (4MB pdf) with filezilla, it uploads in its entirety to the public_html/new/data folder. I have uploaded many files without issues, mainly txt, htm, csv and xls files and have never had an issue with the above code.
But for this pdf file it shows the same size of file as having been uploaded, but when I open the file I get an error message that is has been corrupted and it only partially displays the page contents.
I have tried removing the @echo off and setting the folder permissions to 777, but I end up with the same result.
CodePudding user response:
The Windows ftp
command supports an ASCII (text) mode and a binary mode, the former of which is the default setting.
In text mode (entered by the command ascii
), end-of-line markers become converted, and end-of-file characters may be recognised. In binary mode however (entered by the command binary
), no such conversions occur and files remain unedited.
Since a PDF file is not a text file, binary mode is required; otherwise, its contents becomes altered, rendering the file corrupt.
CodePudding user response:
Despite you having already received a solution in the comments, I have posted this answer, because it was my understanding that from Windows 8 onwards, that the script file, (as a parameter to the -s:
option), is supposed to be Unicode UTF-8 text file, and your batch file was by default writing in ANSI to a .dat
file.
It may not solve your specific issue, but I thought it worth a try as an alternative to using a .dat
file and containing the Binary
command.
This idea should register the script file as ftpcmd.txt
in UTF-8 with BOM, (I'm unsure if a BOM is or isn't a requirement), and hopefully the file will be accepted as not corrupted.
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F "Delims==" %%G In ('"(Set _cp) 2>NUL"') Do Set "%%G="
For /F "Tokens=*" %%G In ('%SystemRoot%\System32\chcp.com') Do For %%H In (%%G
) Do Set "_cp=%%~nH"
If Not %_cp% Equ 1252 (Set "_cpc=TRUE"
%SystemRoot%\System32\chcp.com 1252 1>NUL)
(Set /P "=" 0<NUL
%SystemRoot%\System32\chcp.com 65001 1>NUL
Echo user [email protected]
Echo password
Echo cd public_html/new/data
Echo put abc.pdf
Echo quit
%SystemRoot%\System32\chcp.com 1252 1>NUL
) 1>"ftpcmd.txt"
If Defined _cpc %SystemRoot%\System32\chcp.com %_cp% 1>NUL
%SystemRoot%\System32\ftp.exe -n -s:"ftpcmd.txt" 11.111.111.111
Del "ftpcmd.txt"