Solved

ftp_put works with Text based files but not Images

Posted on 2007-04-03
3
271 Views
Last Modified: 2009-12-16
I'm using ftp_put to upload files.  It works with text based files but not on images. I've tried with small file sized images and used FTP_BINARY. The images are well under the upload liimit and ftp iIS enabled with PHP.

I'm using the following script to FTP files to server.

<?php
if($submit){

// set up basic connection
$ftp_server = "www.skunk2.com";
$conn_id = ftp_connect($ftp_server);
$ftp_user_name = "****";
$ftp_user_pass = "******";
//capture file source from form
$source_file = $file;
$destination_file = "test.jpg";
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

ftp_chdir($conn_id, "websites");
ftp_chdir($conn_id, "www.skunk2.com");
ftp_chdir($conn_id, "html");

// 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, $source_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);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

      <head>
            <meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
            <meta name="generator" content="Adobe GoLive">
            <title>Untitled Page</title>
      </head>

      <body bgcolor="#ffffff">
            <form id="FormName" action="ftpTest.php" method="get" name="FormName">
                  <input type="file" name="file" size="41"><br>
                  <br>
                  <input type="submit" name="submit" value="submit">
            </form>
            <p></p>
      </body>

</html>
0
Comment
Question by:boynatural
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
Jeff Darling earned 125 total points
ID: 18850898

I don't see how this code works at all.

First, the form isn't setup correctly.  It shold be a POST not a GET.  Also try adding enctype to the form.

like this:

<form id="FormName" action="ftpTest.php" method="POST" name="FormName" enctype="multipart/form-data">


Second, you probably don't need the ftp_chdir lines.  Try removing them and see if your file gets uploaded to the default directory for the ftp user account that you are using. (change the form as specified above)

You probably want to name the file on your server the same as it was on the submitted from the form.

$destination_file = $_FILES['file']['name'];



0
 

Author Comment

by:boynatural
ID: 18853327
I was using the form to pass the location. By adding your adjustment it worked with images. Does the form actually pass the file or just a reference to location??
0
 
LVL 12

Expert Comment

by:Jeff Darling
ID: 18854122

The form passes the filename parameter needed by the PHP process.  The PHP code is actually skipped based on the results of this test:  

if($submit)

If the form isn't submitted, then it just displays the form.

I'm not sure why it worked for you on text files.  I have no explanation on that.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
AWS ELB 5 78
Advice and best practice  SQLite3 Database using PHP 2 49
PHP PDO   How do I correctly remove record using DELETE FROM? 1 21
QQ problem 22 23
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question