473,394 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Issues with ftp_put() Upload

I'm kinda new to PHP, and I'm trying to make a simple FTP upload script. It successfully connects to the server and logs in, but it can't upload. The transfer mode I use normally is ASCII (like I have it in the script), and for now, I have the mode set to passive (although I'll give a choice later on when I actually implement the script). (I starred out the password here, it is in the file, though.)

Here's the entire page (not much, just the script itself):

[PHP]<?php

$source_file='C:\cmd.txt';
$destination_file='cmd.txt';

$name='TestGuy';
$type='Text Document';

// set up basic connection
$conn_id = ftp_connect('ftp.pehjota.com');

// login with username and password
$login_result = ftp_login($conn_id,'pehjotauploads','******');

// check connection
if (!$conn_id){
echo "Connection failed!<br />";
exit;
} else {
echo "Connected.<br />";
}
if (!$login_result){
echo "Login failed!<br />";
exit;
} else {
echo "Logged in.<br />";
}

// upload the file
$passive=ftp_pasv($conn_id,true);
$upload=ftp_put($conn_id,$destination_file,$source _file,FTP_ASCII);

// check upload status
if (!passive){
echo 'Failed to enter passive mode.<br />';
}
else {
echo 'Entered passive mode.<br />';
}
if (!$upload) {
echo 'Upload failed! Could not upload "'.$source_file.'" to "http://www.pehjota.com/uploads/'.$destination_file.'"<br />';
//mail('random@pehjota.com','New Submission',$name.' has tried to submit a '.$type.'. The file is located at: http://www.pehjota.com/uploads/'.$destination_file,'From: name@email.com');
} else {
echo 'Uploaded "'.$source_file.'" to <a href="http://www.pehjota.com/uploads/'.$destination_file.'">"http://www.pehjota.com/uploads/'.$destination_file.'"</a><br />';
mail('random@pehjota.com','New Submission',$name.' has submitted a '.$type.'. The file is located at: http://www.pehjota.com/uploads/'.$destination_file);
}

// close the FTP stream
$close=ftp_close($conn_id);

if (close){
echo 'FTP connection successfully closed.';
}
?>[/PHP]

I also tried adjusting chmod permissions for the folder to which this would upload (to 777) just in case, but that did nothing.
Apr 23 '07 #1
1 6056
i've been wondering this too..
but now after some view try and error, i can solve this problem...
first you need to specify your local file that you want to upload...
and must with full path like c:/abc/abc.file
don't use $_FILES[s]['filenameform'] because this will be same as moving file from server to some folder with ftp. thats not what we want...

you can use application/x-www-form-urlencoded in enctype of the head of the form. and
use $_post['filenameform'] to get the path from your local hard drive. you can relax if you use IE because this browser post them with full path,
like c:\\abc\\abc.file. but in firefox, the browser only pass the filename only, like abc.file..this will not upload anything...
so solution for this thing is using javascript, so after you pick a file from file form, some hidden form will catch the path..

pasv mode is used when "It may be needed if the client is behind firewall." thats php manual says, i dont know what it means, wether the
browser is firewalled or the php is firewalled???


this is the source code

=====
=====

<div align="left"><pre><?
if($_POST['button']=="Submit"){
$localfile = //"H:/666/666Satan Vol. 04.zip"; working...
//$_FILES['f']['tmp_name']; not working...
$_POST['f_hidden']; // working
print_r ($_FILES); //debugging purpose
print_r ($_POST); //debugging purpose

$ftp_server = "localhost";
$remote_file = rand().$_POST['f'];
// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
$ftp_user_name="newuser";
$ftp_user_pass="wampp";
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
//ftp_pasv($conn_id, true); //you can comment or uncomment it if you using firewall

echo "login result : ".$login_result."\n";
echo "local file ".$localfile."\n";
echo "remote file ".$remote_file."\n";

echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
// change directory if needed,
if (ftp_chdir($conn_id, "/trans/files/")) {
echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
} else echo "can't change dir";

if (ftp_put(
$conn_id,
$remote_file,
$localfile,
FTP_BINARY )) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}

// close the connection
ftp_close($conn_id);

}
?></pre></div>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<script>
// this js function will fill f_hidden form value with full path of a local file
function changehidden(){
document.uploadForm.f_hidden.value=document.upload Form.f.value;
}
</script>
<body class="twoColHybRt">

<div id="container">
<div id="sidebar1">
<h3>&nbsp;</h3>
<!-- end #sidebar1 --></div>
<div id="mainContent">
<h1> Main Content </h1>
<form action="<?=$_SERVER['PHP_SELF']?>" enctype="application/x-www-form-urlencoded" method="post" name="uploadForm" id="uploadForm">
<input type="file" name="f" id="f" onchange="changehidden()" on />
<input type="hidden" name="f_hidden" value="" />
<input type="submit" name="button" id="button" value="Submit" />
</form>

</div>
</div>
</body>
</html>


====
====

sorry that i dont use CODE tag, because if you copy it straightly you will copy the line numbers to
Attached Files
File Type: zip ftpput.zip (1.2 KB, 254 views)
Jul 24 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Joseph Markovich | last post by:
I'm trying to write a script for my users to be able to upload large files, and now I am unsure what exactly I am doing wrong. My script is based on the one in the PHP manual. <?php if(isset(...
1
by: balzano_1 | last post by:
Hi, im trying to upload a file from a client (windows) to an ftp server (linux), i deciede to use some of php`s functions to connect and authenticate etc.. i only wish to put a file on the server,...
2
by: Udo Pecher | last post by:
I apply the following code test.php $upload =ftp_put($conn_id,"test.jpg","$dat",FTP-BINARY); where $dat is a local filename which I like to move via FTP. "test.jpg" ist the filename on the...
5
by: tripwater | last post by:
Hello, I am having problems with the ftp_put() function. I can login via shell prompt. Does anyone know why the ftp_put() function may not work? I went to the php site and followed the...
1
by: excession | last post by:
I've got a script which uploads a few files to a remote server. The problem is the ftp_put command hands and the script eventually times out. What is even more strange is sometime it works fine. ...
5
by: Alex | last post by:
I'm trying to make simple ftp client, used the following article for base: http://www.devarticles.com/c/a/PHP/Building-An-FTP-Client-With-PHP/3/ All works, I can login, see list of files, but...
9
by: craig.overton | last post by:
All, I am currently developing an FTP class in VB.NET. It's kid tested, mother approved when trying to access an FTP Server on a Windows box meaning I can connect, run commands, upload and...
3
by: empiresolutions | last post by:
ftp_put() is not working. this is what i have. $host = "www.site.com"; $ftp_user_name = "username"; $ftp_user_pass = "password"; // declair files $remote_file = "ITS_BLI_0".$_POST.".csv";...
0
by: sushil | last post by:
Hello Frnds, I am getting problem in uploading file from php script. The error is : Code - $upload = ftp_put($conn_id,$filemax,$src,FTP_BINARY); Output - Resource id...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.