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

User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

I am a little bit confused by why you need to know how to specify the location of the file on the local machine .... heres how it works, you click on the button next to your file upload and you browse for the file(s), once you have selected the file(s) and completed the rest of the form you submit it. When you submit the form the files are uploaded to your server and stored in a temporary folder, this is where the temp name associated with file uploads comes in. You then move the file from the temp folder to where you want it to be by using

Code: Select all

move_uploaded_file($_FILES['photo1_upload']['tmp_name'], $nameandpathyouwantfiletobe);
etc. The php script never touches the local machine, that is not possible!! There is a difference between what happens server side and what happens client side, I hope that clears things up.
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Hi microrobot

I did read your posts - yesterday I tried changing the permissions on the upload directory, however it gave me the error I stated after I changed them. I assumed that the / slashes were correct because I found the empty file in the target directory they were referring to.

I'm just trying the image code - it seems to take an age to load up the page when I click on the upload photos button, which I take it means it is currently uploading the file. I'll take a look at the server when it seems to have finished (or try a smaller file next time..)

Thanks

Mark
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

can you manually ftp from/to that location?
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

I'm not sure what the new upload code is doing. When I click on the upload photos button on the form, it seems to be doing some activity, but it never loads the page. Rather, the progress indicator at the bottom of my IE window gradually gets to 100%, where it remains. This seems to happen no matter what the size of file I put into photo1_upload.
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Yes I can manually upload
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

can you look at your ftp log either locally or on the server?
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Yes but I'm just going to try and get hold of a good log file interpreter so I can make sense of it. Admittedly I can't see any sign of files being uploaded so far.
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

magic - I've looked at the log file and I don't think there's been any uploading activity.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

which one on your machine or on the server?
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

magic, I've tried this code now, and I appear to be at least getting somewhere:

Code: Select all

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

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

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

    echo "Logged in as $user.";
    echo "Uploading file:";

    $filename = $_FILES['photo1_upload']['name'];

    echo $filename;

    if(!$success = ftp_put($conn, '/tmp/test.mp3', $filename, FTP_BINARY))
    {
       echo 'Error: Could not upload file to server';
        ftp_quit($conn);
        exit();
    }

    echo 'File uploaded successfully';

    ftp_quit($conn);
However, it's still giving me the unsuccessful upload message. I've changed the FORM on the previous page to include ENCTYPE="multipart/form-data" (this was probably a major source of problem previously).
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Code: Select all

// not sure however are you still using 

move_uploaded_file() ?
might wanna try @copy
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

I CAN'T GET IT TO WORK!!!! WHY IS IT SO DIFFICULT TO DO THE SIMPLEST OF THINGS AS TO UPLOAD A FILE FROM THE USER'S MACHINE AS THEY HAVE SELECTED VIA A FILE OBJECT AND THEN UPLOAD IT TO MY SITE'S FTP!!!??? WHAT IS GOING ON!! ARRRGHH!!!
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Code: Select all

if(!$success = ftp_fput($conn, '/tmp/test.mp3', $filename, FTP_BINARY)) 

//try the ftp_fput() instead of ftp_put()
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Or try something simple like this.. to make sure its not youre code thats messing it up.

Code: Select all

// code can be found on http://www.zend.com

<?php
$file = 'somefile.txt';
$remote_file = 'readme.txt';
$ftp_server = ' ';
$ftp_user_name = ' ';
$ftp_user_pass = ' ';

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
 echo "successfully uploaded $file\n";
} else {
 echo "There was a problem while uploading $file\n";
}

// close the connection
ftp_close($conn_id);
?>
Last edited by ol4pr0 on Tue Mar 23, 2004 10:47 am, edited 1 time in total.
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Hi ol4pr0

I think the problem is that what I'm asking it to do is in fact impossible - I don't think PHP can access the local machine's files in this way.

On top of this, I'm now changing the method to using the FILE-POST method and then using move_upload. This should work, all it will mean is a rearrangement of my form into two distinct pieces rather than a single one.

Web dev is might frustrating at times, especially for a file upload/download newbie!
Post Reply