ftp_put to upload files to a server problems - php

I have an HTML form that is intended to allow file uploads to a server. I am programming an FTP client which is going well so far except that the files will not upload to the server. My form is as follows:
<form action='upload.php' id='upload'>
<input type='file' name='file' />
<input name='file_name' placeholder='File Name' />
<input type='submit' value='upload' />
</form>
And here is my php:
<?php
$ftp_connection = ftp_connect($_COOKIE['domain']);
if(#ftp_login($ftp_connection, $_COOKIE['username'], $_COOKIE['password'])) {
ftp_put($ftp_connection, $_REQUEST['file_name'], $_REQUEST['file']);
}
ftp_close($ftp_connection);
?>
Also note that all of those cookies work perfectly fine, as I use them to login to the FTP GUI.

Your form is missing enctype="multipart/form-data" which is required when uploading files.
Plus a POST method is also required.
Modify your <form... to read as:
<form action='upload.php' id='upload' enctype='multipart/form-data' method='post'>
<form> defaults to GET when omitted.
Also have a look at:
http://php.net/manual/en/book.ftp.php
Example from that page:
<?php
$ftp_server="";
$ftp_user_name="";
$ftp_user_pass="";
$file = "";//tobe uploaded
$remote_file = "";
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
exit;
} else {
echo "There was a problem while uploading $file\n";
exit;
}
// close the connection
ftp_close($conn_id);
?>
and
http://php.net/manual/en/function.ftp-put.php
Example from that page:
<?php
ftp_chdir($conn, '/www/site/');
ftp_put($conn,'file.html', 'c:/wamp/www/site/file.html', FTP_BINARY );
?>
and
<?PHP
$destination_path = "src/bin/";
//where you want to throw the file on the webserver (relative to your login dir)
$destination_file = $destination_path."img.jpg";
//This will create a full path with the file on the end for you to use, I like splitting the variables like this in case I need to use on on their own or if I'm dynamically creating new folders.
$file = $myFile['tmp_name'];
//Converts the array into a new string containing the path name on the server where your file is.
$upload = ftp_put($conn_id, $destination_file, $file, FTP_BINARY);// upload the file
if (!$upload) {// check upload status
echo "FTP upload of $destination_file has failed!";
} else {
echo "Uploaded $file to $conn_id as $destination_file";
}
?>

Related

Ho to use ftp_put for file upload in PHP?

I try to upload a file using ftp_put after submitting a form. However my code does not work as expected:
$ftpHost = '192.168.180.36';
$ftpUsername = 'userdownload';
$ftpPassword = 'Toms!';
$filepath = "C://xampp/htdocs/Helpdesk/fmt/download";
$connId = ftp_connect($ftpHost,21) or die("Couldn't connect to $ftpHost");
$ftpLogin = ftp_login($connId, $ftpUsername, $ftpPassword);
$file_name = $_FILES['file']['name'][$i];
$tmp_name = $_FILES['file']['tmp_name'][$i];
// try to upload file
if(ftp_put($connId, $file_name, $filepath.'/'.$file_name, FTP_BINARY)){
echo "File transfer successful - $file_name";
}else{
echo "There was an error while uploading $file_name";
}
And I get this error message:
There was an error while uploading $file_name
There are two thing i noticed in your code. the first one is the mode of transfer depends upon your file type.Files that are in the ascii/text file extension list are transferred as ascii, all other files are transferred as binary.The second one is may be there is a problem in your file which you received may be it is black first check that. You can try this code it is working for me.
<?php
// connect and login to FTP server
$ftp_server = "ftp.example.com";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$file = "localfile.txt";
// upload file
if (ftp_put($ftp_conn, "serverfile.txt", $file, FTP_ASCII))
{
echo "Successfully uploaded $file.";
}
else
{
echo "Error uploading $file.";
}
// close connection
ftp_close($ftp_conn);
?>

How to upload file on server using php

Im using php code to upload a file.
The problem is i can only code it to upload file on my local host.
How do I code the target directory that can upload file in server using info such as localhost, name, password etc.
You are looking for a remote file transfer such as ftp.
Taken from the PHP manual:
<?php
$ftp_server="";
$ftp_user_name="";
$ftp_user_pass="";
$file = "";//tobe uploaded
$remote_file = "";
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
exit;
} else {
echo "There was a problem while uploading $file\n";
exit;
}
// close the connection
ftp_close($conn_id);
?>

Upload mp3 file into folder is not working

i'm trying to upload mp3 format file in to a folder using PHP. But this following coding is not working.Please can some one help my?
define("UPLOAD_DIR", "mp3_upload_folder/");
if (!empty($_FILES["myFile"]))
{
$myFile = $_FILES["myFile"];
if ($myFile["error"] !== UPLOAD_ERR_OK)
{
echo "<p>An error occurred.</p>";
exit;
}
// verify the file is a GIF, JPEG, or PNG
$fileType = exif_imagetype($_FILES["myFile"]["tmp_name"]);
// ensure a safe filename
$name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);
// don't overwrite an existing file
$parts = pathinfo($name);
$sanjiv=$_POST['user_id'];
$name= $sanjiv.".".$parts["extension"];
// preserve file from temporary directory
$success = move_uploaded_file($myFile["tmp_name"],
UPLOAD_DIR . $name);
if (!$success) {
echo "<p>Unable to save file.</p>";
exit;
}
This works, and make sure that it has permissions to write to the mp3_upload_folder. I think it's still short of a complete solution, but I am lacking a full view of the problem.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
define("UPLOAD_DIR", "./mp3_upload_folder/");
if (!empty($_FILES["myFile"])) {
$myFile = $_FILES["myFile"];
if ($myFile["error"] !== UPLOAD_ERR_OK) {
echo "<p>An error occurred.</p>";
exit;
}
// ensure a safe filename
$name = $myFile["name"]; //preg_replace("/[^A-Z0-9\.\-]/i", "_", $myFile["name"]); //this is pointless if you're not using it
// don't overwrite an existing file
$parts = pathinfo($name);
$name= $_POST['user_id'].".".$parts["extension"];
// preserve file from temporary directory
$success = move_uploaded_file($myFile["tmp_name"], UPLOAD_DIR .$name);
if (!$success) {
echo "<p>Unable to save file.</p>";
exit;
}
}
?>
<form method="post" enctype="multipart/form-data">
<input type="text" value="testUserId" name ="user_id"/>
<input type="file" name="myFile"/>
<input type="submit"/>
</form>
</body>
</html>
Also make sure that you edit your server settings as well as your php settings for uploading file sizes(in the php.ini):
Post_max_size
Upload_max_size (probably your offender default is 2M)
I'm not sure what server you are using, but information can be found easily enough by googling the server type and "max upload size"

PHP FTP Upload function

i have this function in PHP:
function UploadFileToFTP($local_path, $remote_path, $file, $filename) {
global $settings;
$remote_path = 'public_html/'.$remote_path;
$ftp_server = $settings["IntegraFTP_H"];
$ftp_user_name = $settings["IntegraFTP_U"];
$ftp_user_pass = $settings["IntegraFTP_P"];
//first save the file locally
file_put_contents($local_path.$filename, $file);
//login
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, true);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
}
//change directory
ftp_chdir($conn_id, $remote_path);
$upload = ftp_put($conn_id, $filename, $local_path.$filename, FTP_BINARY);
// check upload status
if(!$upload) {
echo "FTP upload has failed!";
}
// close the FTP stream
ftp_close($conn_id);
}
i call it here:
UploadFileToFTP('p/website/uploaded_media/', 'media/', $_FILES["file"]["tmp_name"], $filename);
the selected file is being moved into the local directory and also being uploaded to FTP however the file is becoming corrupt because it is not being uploaded correctly.
how can i get the file uploading properly?
Depending on what kind of file you're moving you may need to switch from FTP_BINARY to FTP_ASCII
http://forums.devshed.com/ftp-help-113/ftp_ascii-ftp_binary-59975.html
When uploading a file to PHP it stores the uploaded file in a temporary location, the location is stored in $_FILES["file"]["tmp_name"].
You are then passing that value into your UploadToFTP function as the variable $file.
Then you try to save a copy of the uploaded file:
//first save the file locally
file_put_contents($local_path.$filename, $file);
What this will do is write the string contained within $file (i.e. the path of the temp file) to your new location - but you want to write the content of the file.
Instead of using file_put_contents use move_uploaded_file:
move_uploaded_file($file, $local_path.$filename);

Warning: ftp_put() expects parameter 3 to be a valid path, array given

I'm trying to upload a single file to my ftp directory but unable to upload the file. I keep getting the following error
Warning: ftp_put() expects parameter 3 to be a valid path, array given
<form action="" class="tsc_form_contact_light frame tbar" method='post' enctype="multipart/form-data">
<label for="name">ArtWork Name <font color='red'>(required)</font></label>
<input type="upload" name="artname" class="form-input" required />
<label for="name">ArtWork Image</label>
<input type="file" id="uploadfile" name="uploadfile">
</form>
$path_of_storage = '/public_html/newinvoice/orderimage/';
$newftpdir = $_SESSION['SESS_ORDER_ID'];
$conn_id = ftp_connect($ftpserver);
ftp_pasv($conn_id, true);
$login_result = ftp_login($conn_id, $ftplogin, $ftppass);
//ftp_mksubdirs($conn_id,$path_of_storage,$newftpdir);
$source_file = $_FILES['uploadfile']['tmp_name'];
$destination_file = "$path_of_storage".$source_file;
$destination = "$path_of_storage";
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
exit;
} else {
echo "Connected";
}
// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
$name = $_FILES["uploadfile"]["name"];
move_uploaded_file($source_file, "$destination/$name");
// check upload status
if (!$upload) {
echo "FTP upload has failed! $destination_file";
} else {
echo "Uploaded $source_file as $destination_file";
}
// close the FTP stream
ftp_close($conn_id);
$_FILES contains array of all the uploaded file along with the attributes like name, type, tmp_path etc
Use $_FILES['uploadfile']['tmp_name'] for the file path
Refer to this link for more details

Categories