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);
Related
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);
?>
I have a bunch of movies that I'm trying to transfer from my CentOS server onto my Windows PC. But when I run them through this script they end up being corrupt. Is there something wrong with the script?
Thanks
$allFiles = glob("/var/www/html/ftp_pending/*");
// 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);
foreach($allFiles as $singleFile)
{
// check if a file exist
$path = "/"; //the path where the file is located
$file = substr( $singleFile, strrpos( $singleFile, '/' )+1 );
$check_file_exist = $path.$file; //combine string for easy use
// Returns an array of filenames from the specified directory on success or
// FALSE on error.
$contents_on_server = ftp_nlist($conn_id, $path);
// Test if file is in the ftp_nlist array
if (in_array($check_file_exist, $contents_on_server))
{
echo "$file is already on FTP Server, no need to re-upload <br />";
}
else
{
$localfile = '/var/www/html/'.$file.'';
$remote_file = $file;
// upload a file
if (ftp_put($conn_id, $remote_file, $localfile, FTP_ASCII))
{
echo "successfully uploaded $file\n";
}
else
{
echo "There was a problem while uploading $file\n";
}
};
}
// remember to always close your ftp connection
ftp_close($conn_id);
You're trying to upload something other than a text-based file while using
(ftp_put($conn_id, $remote_file, $localfile, FTP_ASCII))
You should be using FTP_BINARY instead of FTP_ASCII since movies (and images) are binary files.
i understand that the ftp_put method uploads a file from the local server computer to the ftp server but i have problems using it where when i try to execute a simple script like this:
<?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);
?>
the operation is successfully done except for that the file uploaded to the my ftp server is always with zero byte size!
also i tried to enable passive mode but it still uploads an empty file.
Try enabling track_errors and access $php_errormsg
ini_set('track_errors', 1);
// put operation
// if error
var_dump($php_errormsg);
I had the same issue. When I change "FTP_ASCII" to "FTP_BINARY" it solved my problem and files uploaded as expected.
I had ran into this as well. Bit late but thought I'd post my solution:
$file_name = "localfile.txt";
Get content from your existing file
$content = file_get_contents('http://www.somewhere.com/'.$file_name);
...or make temp file content
$content = "This is content";
upload file
// connect
$conn_id = ftp_connect($host);
$login = ftp_login($conn_id, $username, $password);
ftp_pasv($conn_id, true);
// create
$tmp = fopen(tempnam(sys_get_temp_dir(), $file), "w+");
fwrite($tmp, $content);
rewind($tmp);
// upload
$upload = ftp_fput($conn_id, "serverfile.txt", $tmp, FTP_ASCII);
// close
ftp_close($conn_id);
PHP Warning: imagejpeg(): Unable to open '.Project/events/timepass.jpg' for writing: No such file or directory in ./Project/upload/thumbnal.php on line 35
code..
<?php
// open the directory
$pathToImages="./Project/upload/original/";
$dir = opendir($pathToImages);
// loop through it, looking for any/all JPG files:
while (false !== ($fname = readdir( $dir )))
{
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( strtolower($info['extension']) == 'jpg')
{
// echo "Creating thumbnail for {$fname} <br />";
// load image and get image size
$image_size=getimagesize( "{$pathToImages}{$fname}");
$image_width=$image_size[0];
$image_height=$image_size[1];
$new_size=($image_width+$image_height)/($image_width*($image_height/80));
$new_width=$image_width*$new_size;
$new_height=$image_height*$new_size;
$new_image=imagecreatetruecolor($new_width,$new_height);
$old_image=imagecreatefromjpeg("{$pathToImages}{$fname}");
imagecopyresized($new_image,$old_image,0,0,0,0,$new_width,$new_height,$image_width,$image_height);
$pathToThumbs="./Project/events/$fname";
imagejpeg($new_image,$pathToThumbs);
// save thumbnail into a file
}
}
// close the directory
closedir( $dir );
?>
I am getting this error when i transferred my data from localhost to live server FTP.I searched on google some have recommanded for changing attributes of directory to 777.i did tat bt no use same warning.Please tell where should i make changes to make these code work.
You transfer the new image to a remote server?
Where is the path to the server? Like "--IP TO SERVER--/Project/events/"
The problem is, the path you have wrote can not be found on your local machine.
UPDATE
To upload the image to the FTP server have a look at this example:
<?php
$file = 'somefile.txt';
$ftp_server = "ftp.example.com";
// Connection
$conn_id = ftp_connect($ftp_server);
// Login with user and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// File upload
// $remote_file is the filename on the server
// $file the filename on your local machine
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "success\n";
} else {
echo "error\n";
}
// Close connection
ftp_close($conn_id);
?>
In general when you have such problems, make sure that the path exists and the user with which you try to upload the image has the necessary privileges to that path.
I am trying to move an uploaded file onto a remote server, this isn't working;
move_uploaded_file($tmp_name, "uploads/$code1/$code.$fileex");
$ftp_server = "IP";
$ftp_user_name = "username";
$ftp_user_pass = "password";
$file = $tmp_name;
$remote_file = "/public_html/test/uploads/";
// 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";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
I get this erorr;
Warning: ftp_put() [function.ftp-put]: Can't open that file: Is a directory in /home/file/public_html/uploaded.php on line 52
Your $remote_file variable is pointing to a directory when it should point to a file. Try changing $remote_file to $remote_file = "/public_html/test/uploads/".$file;
You should probably wrap the portion that uploads the file in an if statement that checks to see if you are actually connected properly to the FTP
Also, when uploading a file, you need file 1 and file 2. Right now you've supplied file 2 and a directory.
http://php.net/manual/en/function.ftp-put.php
The file you are trying to move to is the directory "/public_html/test/uploads/", you need to append the filename and extension onto the directory.
Add the following line at the end of the /etc/vsftpd.conf file
Add pasv_promiscuous=YES it