Forum Moderators: coopster

Message Too Old, No Replies

FTP uploads

cannot get files to upload

         

bubone2

11:14 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



I have written an ftp script. When I execute it, the file is recognized by the script, the remote server is connected, and the ftp user is able to log in. However, I have only been able to upload one particular file and no others. I know this is weird, but the only file that it will accept is the source code of the script itself. It transfers this file with no problems.

This is a weird problem. At the risk of spamming my code and iritating people, in order to save time, I will go ahead an post the code for this script.


//HMTL
<form>
<input type=file name=target_file>
<input type=submit name=submit_file>
</form>

//php
$SUBMIT_FILE = $_POST['SUBMIT_FILE'];

if($SUBMIT_FILE)
{
$filename = $_FILES['UPLOADED_FILE']['name'];
$filesize = $_FILES['UPLOADED_FILE']['size'];

$ftp_server = "server";
$ftp_login = "user";
$ftp_pass = "pass";
$remote_path = "remote_path/ftp_uploads";

if(($filename==NULL)&&($filesize==NULL))
echo "<font color=\"#D20000\">ERROR:</font> Please select a valid file to upload.<br>";
else
{ $ftp_connection_handle = ftp_connect($ftp_server, 21);

if(!$ftp_connection_handle)
echo "<font color=\"#D20000\">ERROR:</font> Could not establish an ftp connection.<br>";
$ftp_login_handle = ftp_login($ftp_connection_handle, $ftp_login, $ftp_pass);

if(!$ftp_login_handle)
echo "<font color=\"#D20000\">ERROR:</font> Could not successfully login to ftp server.<br>";

ftp_pasv($ftp_connection_handle, true);

if(ftp_put($ftp_connection_handle, $remote_path.$filename, $filename, FTP_BINARY))
echo "File <b>$filename</b> uploaded successfully in BINARY format.<br>";
else if(ftp_put($ftp_connection_handle, $remote_path.$filename, $filename, FTP_ASCII))
echo "File <b>$filename</b> uploaded successfully in ASCII format.<br>";
else
echo "<font color=\"#D20000\">ERROR:</font> File <b>$filename</b> was not uploaded.<br>";

$server_OS = ftp_systype($ftp_connection_handle);

echo "<br><br><u>File info</u><br>File: $filename<br>Size: $filesize";
echo "<br><br><u>Server info</u><br>Server OS: $server_OS";

ftp_close($ftp_connection_handle);
}
}

Sry, I know that makes for a long post. But can anyone lend some ideas? I've been pouring over it for quite some time. I don't know ... might be something simple.

Thanks,
Josh

P.S. I tried putting all the code inside the [code] tags, but apparently didn't work

coopster

9:41 pm on Dec 21, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Are you sure you are explicity opening and closing your logic statements correctly, particulary your "if" control structures? I would start there, for example:

<?php
$SUBMIT_FILE = (isset($_POST['SUBMIT_FILE']))? $_POST['SUBMIT_FILE'] : '';
if ($SUBMIT_FILE) {
$filename = $_FILES['UPLOADED_FILE']['name'];
$filesize = $_FILES['UPLOADED_FILE']['size'];
$ftp_server = "server";
$ftp_login = "user";
$ftp_pass = "pass";
$remote_path = "remote_path/ftp_uploads";
if (($filename==NULL) && ($filesize==NULL)) {
echo "<font color=\"#D20000\">ERROR:</font> Please select a valid file to upload.<br>";
} else {
$ftp_connection_handle = ftp_connect($ftp_server, 21);
}
if (!$ftp_connection_handle) {
echo "<font color=\"#D20000\">ERROR:</font> Could not establish an ftp connection.<br>";
}
$ftp_login_handle = ftp_login($ftp_connection_handle, $ftp_login, $ftp_pass);
if (!$ftp_login_handle) {
echo "<font color=\"#D20000\">ERROR:</font> Could not successfully login to ftp server.<br>";
}
ftp_pasv($ftp_connection_handle, true);
if (ftp_put($ftp_connection_handle, $remote_path.$filename, $filename, FTP_BINARY)) {
echo "File <b>$filename</b> uploaded successfully in BINARY format.<br>";
} else if (ftp_put($ftp_connection_handle, $remote_path.$filename, $filename, FTP_ASCII)) {
echo "File <b>$filename</b> uploaded successfully in ASCII format.<br>";
} else {
echo "<font color=\"#D20000\">ERROR:</font> File <b>$filename</b> was not uploaded.<br>";
}
$server_OS = ftp_systype($ftp_connection_handle);
echo "<br><br><u>File info</u><br>File: $filename<br>Size: $filesize";
echo "<br><br><u>Server info</u><br>Server OS: $server_OS";
ftp_close($ftp_connection_handle);
}
?>