Solved

ftp_put works with Text based files but not Images

Posted on 2007-04-03
3
270 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now