PDA

View Full Version : PHP File Upload using FTP_PUT()



Streakfury
25 Oct 2006, 07:13 AM
Hey all,

I'm trying to make a simple script for uploading a file to my web server. Here's the code:



<form action="client_upload_confirmation.php" method="post" enctype="multipart/form-data">
<input name="source_file" type="file" />
<input type="submit" value="Upload" title="Upload your file" />
</form>



<?php

// gets the file from the input form and puts it into a PHP array
$source = $_FILES["source_file"];

// extracts the source file name
$source_file = $_FILES["source_file"]["name"];

// sets the target directory on the web server
$target_directory = "_uploads/";

// sets the target file name
$destination_file = $target_directory . $source_file;

// includes the FTP variables stored in another PHP file
include("../php/ftp_include.php");

// connects to the FTP server
$connection = ftp_connect($ftp_server);

// displays an error message if the connection cannot be made
if (!$connection) {
echo '<p>Could not connect to the FTP server.</p>';
} else {
echo '<p>Connection established.</p>';
}

// logs in to the FTP server
$login = ftp_login($connection, $ftp_username, $ftp_password);

// displays an error message if the script cannot log in
if (!$login) {
echo '<p>Could not log in to the FTP server.</p>';
} else {
echo '<p>Login successful.</p>';
}

// uploads the file
$upload = ftp_put($connection, $destination_file, $source_file, FTP_BINARY);

// displays an error message if the file could not be uploaded
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file";
}

// close the FTP stream
ftp_close($connection);

?>

The problem I have is that it doesn't work! What am I doing wrong?

I get the confirmation messages that the connection has been established and that the login was successful, but the actual upload fails.

I've checked my php.ini file and it has uploads enabled, so I'm really stuck.

Any help would be awesome, thanks!

:)

Streakfury
25 Oct 2006, 05:03 PM
I've tried checking the permissions for the folder, and they're set to 755, and I can also use a PHP script to successfully read from the directory, but for some reason the upload just doesn't work.

Can anyone think of any other reasons (possibly to do with the PHP.ini settings) why my script won't work?

Thanks everyone.

:)