ragebunny
ragebunny

Reputation: 1760

Uploading a local file to remote server with PHP and FTP

I've been trying to make a system that can upload large files, originally i used HTTP but this had a number of problems with settings that needed to be changed. So i thought i'd give it a go with FTP.

Now i have a ftp connection in PHP and it works find, i can view folders and files as well as make directories, what i can't seem to figure out though is how to get hold of a local file and upload it.

I have been reading lots of information and tutorials such as the PHP manual and a tutorial i found on nettuts But i'm struggling to do it. The tutorial says you can upload a local file but i must be missing something.

Here is the upload method i'm using:

public function uploadFile ($fileFrom, $fileTo)
    {
        // *** Set the transfer mode
        $asciiArray = array('txt', 'csv');
        $extension = end(explode('.', $fileFrom));
        if (in_array($extension, $asciiArray)) 
            $mode = FTP_ASCII;      
        else 
            $mode = FTP_BINARY;

        // *** Upload the file
        $upload = ftp_put($this->connectionId, $fileTo, $fileFrom, $mode);

        // *** Check upload status
        if (!$upload) {

                $this->logMessage('FTP upload has failed!');
                return false;

            } else {
                $this->logMessage('Uploaded "' . $fileFrom . '" as "' . $fileTo);
                return true;
            }
    }

When trying to upload a file i use this:

$fileFrom = 'c:\test_pic.jpg';

$fileTo = $dir . '/test_pic.jpg';

$ftpObj -> uploadFile($fileFrom, $fileTo);

I thought this would get the file from my machine that is stored in the c: and upload it to the destination but it fails (Don't know why). So i changed it a little, changed the $fileFrom = test_pic.jpg and up the picture in the same folder on the remote server. When i ran this code the script copied the file from the one location to the other.

So how would i go about getting the file from my local machine to be sent up to the server?

Thanks in advance.

Upvotes: 1

Views: 3632

Answers (1)

Florian Müller
Florian Müller

Reputation: 7785

Using this you would upload a file from your PHP server to your FTP server, what actually not seems to be your target.

Create an upload form which submits to this PHP file. Store this file temporarily on your server and then upload it to your FTP server from there.

If your try would actually work, this would be a major security issue, because a PHP file would have access to any files on my local machine.

Upvotes: 3

Related Questions