Jump to content

Php & Ftp


dph1077

Recommended Posts

Posting this before I go to bed (& wake up in 4 hours! LOL). To make a long story short, I am trying to use an HTML page to take a file name, send it to an PHP page and then use that page to FTP the file to the site. This is the code that I have so far. I hope the experts can easily see the mistakes I'm making. Thanks in advance for any help/advice!

 

HTML PAGE:

<html>

<body>

<form action="upload.php" method="post" enctype="multipart/form-data">

File: <input type="file" name="file" size="20">

<input type="submit" value="Upload">

<input type="reset">

</form>

</body>

</html>

 

PHP PAGE:

<?php

 

$ftp_server = '****';

$ftpuser = '*****'; //username omitted for posting

$ftppass = '*****'; //password omitted for posting

 

$destination_file = "/home/xxxxxx/www/images";

 

// set up basic connection

$conn_id = ftp_connect($ftp_server);

 

// login with username and password

$login_result = ftp_login($conn_id, $ftpuser, $ftppass);

 

// check connection

if ((!$conn_id) || (!$login_result)) {

echo "FTP connection has failed!";

echo "Attempted to connect to $ftp_server for user $ftp_user_name";

exit;

} else {

echo "Connected to $ftp_server, for user $ftp_user_name";

}

 

 

// upload the file

$upload = ftp_put($conn_id, $destination_file, '$file', FTP_BINARY);

 

// check upload status

if (!$upload) {

echo "FTP upload has failed!";

} else {

echo "Uploaded $source_file to $ftp_server as $destination_file";

}

 

// close the FTP stream

ftp_close($conn_id);

?>

 

 

 

And here is a copy of the error I receive:

Connected to ****, for user

Warning: ftp_put(): /home/xxxxxx/www/images: No such file or directory in /home/xxxxxx/public_html/enfieldmotorsales/upload.php on line 26

FTP upload has failed!

 

Edit TCH-Rob to remove account information.

Link to comment
Share on other sites

First, I am not great at PHP but the error states that there is no image directory in your www directory. The www directory on our servers is just a link to the public_html directory so what happens if you take out the www and just make it $destination_file = "/home/xxxxxx/images"; instead?

 

I could be waaaaay off so I will let someone with more PHP knowledge give a real answer.

Link to comment
Share on other sites

Thanks for the help so far.

I tried updating the $destination_file to "/www/images" but I still get an error:

 

Connected to thewebxp.com, for user

Warning: ftp_put(): /www/images: Not a regular file in /home/xxxxxx/public_html/enfieldmotorsales/upload.php on line 26

FTP upload has failed!

 

The folder "images" does exist in my "www" directory, so I'm not sure what it

means by a regular file.

 

Hmmm, I wonder if this might have something to do with it. I moved my HTML and PHP file into the "www" directory, with the "images" folder now above it. Now I get a different error, "Connected to thewebxp.com, for user FTP upload has failed! ".

Does this new error shed any light?

Link to comment
Share on other sites

Thanks for the help so far.

I tried updating the $destination_file  to "/www/images" but I still get an error:

 

Connected to thewebxp.com, for user

Warning: ftp_put(): /www/images: Not a regular file in /home/xxxxxx/public_html/enfieldmotorsales/upload.php on line 26

FTP upload has failed!

 

The folder "images" does exist in my "www" directory, so I'm not sure what it

means by a regular file.

A "regular file" means that it is a file, as opposed to a directory. :wallbash:

 

According to the PHP documentation for ftp_put(), the destination for the FTP transfer is supposed to be specified as a full path and file name, not just a directory path.

 

Unless you want to use to save the file on the FTP server with a different file name, I'd suggest changing your code as follows:

>// upload the file
$upload = ftp_put($conn_id, "$destination_file/$file", '$file', FTP_BINARY);

Link to comment
Share on other sites

Hmmm, still not working. I get this error:

EDIT:(HAD WRONG ERROR LISTED

Connected to thewebxp.com, for user

Warning: ftp_put(): /www/images//tmp/phpiE0vBP: No such file or directory in /home/xxxxxx/public_html/upload.php on line 26

FTP upload has failed!

 

This is what the code looks like now:

><?php

$ftp_server = 'thewebxp.com';
$ftpuser = '******';
$ftppass = '******';

$destination_file = "/www/images";

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

// login with username and password
$login_result = ftp_login($conn_id, $ftpuser, $ftppass);

// check connection
if ((!$conn_id) || (!$login_result)) {
       echo "FTP connection has failed!";
       echo "Attempted to connect to $ftp_server for user $ftp_user_name";
       exit;
   } else {
       echo "Connected to $ftp_server, for user $ftp_user_name";
   }


// upload the file
$upload = ftp_put($conn_id, "$destination_file/$file", $file, FTP_BINARY);

// check upload status
if (!$upload) {
       echo "FTP upload has failed!";
   } else {
       echo "Uploaded $source_file to $ftp_server as $destination_file";
   }

// close the FTP stream
// ftp_close($conn_id);
?>

Link to comment
Share on other sites

:) :wallbash:

It worked, but then again it didn't! lol

It didn't work until I removed the singe quotes ' from the '$file' part of the command. It then said that it uploaded it. But when I check the /images/ folder

in cPanel, the file that is there is called phpKt8YQr.

Link to comment
Share on other sites

Yes and no. Yes, it is finally putting a file into the images directory, but no, its not the right file. I tried uploading a .txt and .bmp file a few times, and what it put into the images directory was this :

phpATSuOc 0 k 644

phpKt8YQr 11 k 644

phpPKnsGp 11 k 644

 

 

opening the files shows just a bunch of gobbley-junk characters as one would expect.

Edited by dph1077
Link to comment
Share on other sites

Just a thought, but I added an echo command to print the name of

the file on the php page, and its showing the file name as those messed up characters. It seems the problem might then be with the info that is getting passed from the html form to the php page. Here is my html code:

 

<html>

<body>

For Upload.php

<form action="upload.php" method="post" ENCTYPE="multipart/form-data">

File: <input type="file" name="file" size="20">

<input type="submit" value="Upload">

<input type="reset">

</form>

</body>

</html>

Link to comment
Share on other sites

:D Ok, after more online research, it works, but "with errors". I'm not sure what this message means.

 

Connected to thewebxp.com, for user

Warning: move_uploaded_file(): open_basedir restriction in effect. File(/www/images/bg.gif) is not within the allowed path(s): (/home/xsnkqf/:/usr/local/sqmail/:/usr/lib/php:/usr/local/lib/php:/tmp) in /home/xsnkqf/public_html/upload.php on line 37

Uploaded /tmp/phpdUzCTH to thewebxp.com as /www/images/tmp/phpdUzCTH

 

Even though at the end it lists the gibberish file names, it actually puts the file in correctly as it should be named. Go figure. Does anyone know how to remove the warning message? I thought I had changed the permissions, hence the ability to upload the files. :P

Link to comment
Share on other sites

I did some reading about PHP file uploads in the PHP manual. I don't think it's necessary to use FTP in your PHP script.

 

Your HTML form is fine. When you click the "Upload" button, the file is automatically uploaded via HTTP to the server's tmp directory, and given a temp file name. PHP keeps track of the original file name, and the temp file name, in the global $_FILES variable. The uploaded file just needs to be moved to the proper directory and renamed to its proper name.

 

It appears that the $file variable in your script is accessing the temp file name of the file after it has been uploaded to the /tmp directory. I'm guessing this is due to the PHP option 'register_globals' being enabled, and things like this occur very easily when the PHP global variables for form data are not used.

 

Taking the example from the PHP documentation, I used this script with your form to upload a file and move it into the proper directory with the proper name:

><?php

$upload_dir = "/home/xxxxxx/public_html/images/";
$upload_file = $upload_dir . basename($_FILES['file']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['file']['tmp_name'], $upload_file)) {
  echo "File is valid, and was successfully uploaded.\n";
} else {
  echo "Possible file upload attack!\n";
}

echo 'Here is some more debugging info:';
print_r($_FILES);

echo "</pre>";

?>

Since this file upload script will run as the user 'nobody' whatever directory you want the uploaded files to be moved into must have 0777 permissions.

 

Hope this helps...

Link to comment
Share on other sites

:dance: :blink:

I would like to thank everyone who has helped!

TCH-David, that code in your last post works great too. I think I will use that

instead of FTPing.

 

I hope in the future I am able to help out others on the boards as you guys have done for me!

 

Hats off to everyone from TCH! :tchrocks:

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...