Solved

ftp_put works with Text based files but not Images

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
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 how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

820 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