Uploading a local file to site FTP server using PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Uploading a local file to site FTP server using PHP

Post by mjseaden »

Dear All

Can anyone tell me briefly how to upload a file from my computer (local file) to my remote FTP server for my site, using PHP.

For all of the resources I have looked at on this subject (including my PHP and MySQL Manual, and online), none of them seem to specifically cover this point, more transferring files from one remote server to another, or they don't really clarify exactly what direction they pertain to be an 'upload' or a 'download'.

I am using the file path and name given by a FILE input object, the contents of which have been posted from a form on the previous page.

Thank you very much for any help

Yours sincerely,

Mark
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

The following code I have obtained is unfortunately producing an error. 'target.bin' is I am assuming the destination file, and $_POST['photo1_upload'] is the contents of the FILE input object on a form on the previous page. This variable contains the pathname of the file local to my computer, which I want to upload to my FTP server to the file 'target.bin'.

Is reports that parameter 3 in ftp_put is an invalid FTP resource.

Can anyone interpret this problem?

Code: Select all

if(!$success = ftp_put($conn, 'target.bin', $_POSTї'photo1_upload'], FTP_BINARY))
    {
    	echo 'Error: Could not upload file to server';
        ftp_quit($conn);
        exit();
    }
Many thanks

Mark
coreycollins
Forum Commoner
Posts: 67
Joined: Sun Feb 01, 2004 1:04 pm
Location: Michigan

Post by coreycollins »

Can you echo $_POST["photo1_upload"]? I want to see what value is in it. Then I may be able to help you.
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

D:\\BreakXL7.mp3

It's a file on my hard drive. I want to upload this file to the FTP server on which my site is stored.

Many thanks

Mark
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

/bump
coreycollins
Forum Commoner
Posts: 67
Joined: Sun Feb 01, 2004 1:04 pm
Location: Michigan

Post by coreycollins »

Let me know if this helps... This is how I did it and it works like a champ:

Code: Select all

<?
if(isset($_POST["action"]))
{
	if($_POST["action"] == "upload")
	{
			// the file uploaded from the form
			$filename = $HTTP_POST_FILES['myfile']['tmp_name'];
			
			// set up basic connection
			$conn_id = ftp_connect("ftp.coreycollins.com");
			
			// login with username and password
			$login_result = ftp_login($conn_id, "user", "password");
			
			// upload a file
			if (ftp_put($conn_id, "test.txt", $filename, FTP_ASCII)) {
			echo "successfully uploaded $filename\n";
			} else {
			echo "There was a problem while uploading $file\n";
			}

			// close the connection
			ftp_close($conn_id);

	}
}

?>

Code: Select all

&lt;FORM action="ftpUpload.php" method="post" enctype="multipart/form-data"&gt;
	&lt;INPUT Type="hidden" name="action" value="upload"&gt; 
	&lt;INPUT TYPE="file" NAME="myfile" SIZE=30&gt; &lt;BR&gt;
	&lt;INPUT TYPE="submit" NAME="Upload File" Value="Upload File"&gt; 
&lt;/FORM&gt;
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Thanks Corey I'll give it a go
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Okay - it's telling me that parameter 3 in ftp_put is not a valid FTP resource.

I get this feeling maybe ftp_put is the wrong way round - I am effectively looking to 'download' this file from my computer to the server - shouldn't it be ftp_get not ftp_put? This can get a bit confusing.
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Oops! I just noticed I put ftp_quit before ftp_put. That was causing the resource error. However it's still not uploading, it's now giving me the custom unsuccessful upload error.
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Corey - I'm using $_FILES['photo1_upload']['tmp_name'] but echo tells me it's empty. $HTTP_POST_FILES I don't think is used anymore under my version of PHP.

As I said I'm a newbie to uploading - can you tell me what tmp_name is, and the mechanics of downloading and uploading? What is the direction of uploading under PHP - my computer to the server on which my site is stored (and on which PHP is running), or the server on which my site is stored (and on which PHP is running) to my computer? Confusion!

Mark
coreycollins
Forum Commoner
Posts: 67
Joined: Sun Feb 01, 2004 1:04 pm
Location: Michigan

Post by coreycollins »

Don't change the myfile and tmpname. Those are PHP file functions. Here is a link that talks about them:

http://www.php.net/features.file-upload
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Thanks
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Right, the following code seems to work:

Code: Select all

$conn = ftp_connect("$host");
    if(!$conn)
    &#123;
    	echo 'Error: could not connect to FTP server. Please try again later.';
        exit();
    &#125;

    echo "Connect to $host.<br />";

    // log in to host
    @ $result = ftp_login($conn, $user, $pass);
    if(!$result)
    &#123;
    	echo "Error: Could not log on as $user<br />";
        ftp_quit($conn);
        exit();
    &#125;

    echo "Logged in as $user.";

    echo "Uploading file:";

    $filename = $_POST&#1111;'photo1_upload'];

    echo $filename;

    if(!$success = ftp_get($conn, '/tmp/test.mp3', $filename, FTP_BINARY))
    &#123;
    	echo 'Error: Could not upload file to server';
        ftp_quit($conn);
        exit();
    &#125;

    echo 'File downloaded successfully';

    ftp_quit($conn);
However, whilst the /tmp/test.mp3 file appears in the directory, and the script seems to successfully find local file $filename on my computer, the following messages are produced by the code:
Connect to ftp.XXXX.biz.
Logged in as XXXX.Uploading file:D:\\BreakXL7.mp3
Warning: ftp_get(): Error opening /tmp/test.mp3 in \\XXXX\domains\s\XXXX.biz\user\htdocs\XXXX.php on line 2405
Error: Could not upload file to server
Whilst the file /tmp/test.mp3 appears, it is 0 bytes in size and the code produces this error output.

Is there any obvious error in my approach?

Thanks very much

Mark
coreycollins
Forum Commoner
Posts: 67
Joined: Sun Feb 01, 2004 1:04 pm
Location: Michigan

Post by coreycollins »

My guess is the file is still on your computer. The reason I did it like I did above is because you have to send the file to the web server where the php code is getting executed. I don't think this is going to work for the file name:

Code: Select all

$filename = $_POST['photo1_upload'];
You're going to most likely do the form and the submission like I had above.
southeastweb
Forum Newbie
Posts: 14
Joined: Sun Mar 21, 2004 3:28 am
Location: Florida

I might be able to help

Post by southeastweb »

Do you want to upload via a web interface from your computer? What types of files are you looking to transfer?

J
Post Reply