FTP with PHP

  • Katushai
  • Graduate
  • Graduate
  • User avatar
  • Posts: 152

Post 3+ Months Ago

for some reason, every time i try to upload a file via FTP with PHP, it searches the server's directory for it. For example, i tried to upload a zip file with the path D:/Backup1/MyMusic.zip, and the PHP program tried to open the file /var/www/html/D:/Backup1/MyMusic.zip. I was wondering how i would upload a file from MY computer...can anybody help me? maybe im just coding it wrong, and seeing some example code would help...thanks for ANY help given.

Heres the code i have for the server:

PHP Code: [ Select ]
 
<?php
 
    $file = 'D:/Backup1/MyMusic.zip';
 
    $remote_file = '/home/stimme/MyMusic.zip';
 
                                                                                                                             
 
    echo "Welcome!!!";
 
    echo "Attempting to connect...<br>";
 
                                                                                                                             
 
 // set up basic connection
 
    if (!($sockfd = ftp_connect('192.168.1.101')))
 
        exit("Error connecting. Exiting...Goodbye!");
 
    else
 
        echo "Connected. ";
 
                                                                                                                             
 
 // login with username and password
 
    if (!($login_result = ftp_login($sockfd, 'stimme', 'cheese')))
 
        exit("Error logging in. Exiting...Goodbye!");
 
    else
 
        echo "Logging in...Login Successful!<br><br>Welcome, Stimme!";
 
                                                                                                                             
 
 // upload a file
 
    echo "Attempting to send file...May take a few minutes...<br>";
 
    if (ftp_put($sockfd, $remote_file, $file, FTP_BINARY))
 
        echo "successfully uploaded $file\n";
 
    else
 
        echo "There was a problem while uploading $file\n";
 
                                                                                                                             
 
    ftp_close($sockfd);
 
?>
 
 
  1.  
  2. <?php
  3.  
  4.     $file = 'D:/Backup1/MyMusic.zip';
  5.  
  6.     $remote_file = '/home/stimme/MyMusic.zip';
  7.  
  8.                                                                                                                              
  9.  
  10.     echo "Welcome!!!";
  11.  
  12.     echo "Attempting to connect...<br>";
  13.  
  14.                                                                                                                              
  15.  
  16.  // set up basic connection
  17.  
  18.     if (!($sockfd = ftp_connect('192.168.1.101')))
  19.  
  20.         exit("Error connecting. Exiting...Goodbye!");
  21.  
  22.     else
  23.  
  24.         echo "Connected. ";
  25.  
  26.                                                                                                                              
  27.  
  28.  // login with username and password
  29.  
  30.     if (!($login_result = ftp_login($sockfd, 'stimme', 'cheese')))
  31.  
  32.         exit("Error logging in. Exiting...Goodbye!");
  33.  
  34.     else
  35.  
  36.         echo "Logging in...Login Successful!<br><br>Welcome, Stimme!";
  37.  
  38.                                                                                                                              
  39.  
  40.  // upload a file
  41.  
  42.     echo "Attempting to send file...May take a few minutes...<br>";
  43.  
  44.     if (ftp_put($sockfd, $remote_file, $file, FTP_BINARY))
  45.  
  46.         echo "successfully uploaded $file\n";
  47.  
  48.     else
  49.  
  50.         echo "There was a problem while uploading $file\n";
  51.  
  52.                                                                                                                              
  53.  
  54.     ftp_close($sockfd);
  55.  
  56. ?>
  57.  
  58.  
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Axe
  • Genius
  • Genius
  • User avatar
  • Posts: 5739
  • Loc: Sub-level 28

Post 3+ Months Ago

Are you sure you mean through FTP? Or are you talking about the http upload form?
  • Katushai
  • Graduate
  • Graduate
  • User avatar
  • Posts: 152

Post 3+ Months Ago

im logging in to my vsftpd server via PHP's FTP stuff
  • Axe
  • Genius
  • Genius
  • User avatar
  • Posts: 5739
  • Loc: Sub-level 28

Post 3+ Months Ago

Are you running PHP in safe mode?

I'm not sure how Apache/PHP interprets local paths on a windows box, you might have to use a URL file parameter..

file://d:/whatever as though you were browsing through IE.
  • Katushai
  • Graduate
  • Graduate
  • User avatar
  • Posts: 152

Post 3+ Months Ago

i switched to linux, im using linux now -- ill try that, though
  • placid psychosis
  • Proficient
  • Proficient
  • User avatar
  • Posts: 284
  • Loc: Warsaw, IN

Post 3+ Months Ago

Err, PHP can't connect to your computer an upload a file to another server... You would have to first upload the file to the Web server, and then from the Web server to the FTP server.
  • Katushai
  • Graduate
  • Graduate
  • User avatar
  • Posts: 152

Post 3+ Months Ago

didnt work. :(
  • Katushai
  • Graduate
  • Graduate
  • User avatar
  • Posts: 152

Post 3+ Months Ago

my ftp server is on the same computer as my web server, and i want to upload the file from another one of my computers to my computer(i want to get the file from computer a to computer b by using my program)
  • placid psychosis
  • Proficient
  • Proficient
  • User avatar
  • Posts: 284
  • Loc: Warsaw, IN

Post 3+ Months Ago

Right, but your error:
Quote:
...and the PHP program tried to open the file /var/www/html/D:/Backup1/MyMusic.zip.


indicates that this script is running on a Linux server, and the file is on a Windows computer. The script is using the cwd to search for the supplied filename. This can't be done without an HTTP upload to the Web server from computer A, and then FTP transaction from Webserver to computer B.
  • Katushai
  • Graduate
  • Graduate
  • User avatar
  • Posts: 152

Post 3+ Months Ago

i see, so(this has no relevance to my problem, but out of curiosity) if i had two windows machines, would i need to upload via http, like youre saying? or even two linux machines?

ok back to the question...

so how would i pursue this goal of uploading to the web server? is there any way to do it?
  • placid psychosis
  • Proficient
  • Proficient
  • User avatar
  • Posts: 284
  • Loc: Warsaw, IN

Post 3+ Months Ago

PHP Manual: Handling File Uploads

Everything you need, right there. Just note that there is no way to display a progress bar for uploads using pure PHP. You have to use a bit of Perl or other CGI language. If you don't need a progress bar, you're good to go with that info. If you do need a progress bar, look into Mega Upload.
  • Katushai
  • Graduate
  • Graduate
  • User avatar
  • Posts: 152

Post 3+ Months Ago

thanks

Post Information

  • Total Posts in this topic: 12 posts
  • Users browsing this forum: No registered users and 149 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.