how to get file size on ftp server using php? - php

i need a list of all the files on an FTP server. I can get a list of files using php , but no idea how to get the file size. How do I get the file sizes? Thanks.

You can call
ftp_size
As in
int ftp_size ( resource $ftp_stream , string $remote_file )
To get the size of files over FTP.
For example:
$file = 'somefile.txt';
// 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);
// get the size of $file
$res = ftp_size($conn_id, $file);
if ($res != -1) {
echo "size of $file is $res bytes";
} else {
echo "couldn't get the size";
}
// close the connection
ftp_close($conn_id);
However only some FTP servers support this.
Source: http://php.net/manual/en/function.ftp-size.php

Related

files uploaded by ftp_put are becoming shortcuts on the server [duplicate]

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.

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);

PHP Read a ftp file line by line from localhost

I have a text file stored on a ftp account. I have the hostname, username and the password. I dont know what is the name of the file, but i know that on root there is only 1 file. I have to read that file line by line and store that content into a php variable.
This is what i have tryed:
$ftp_server = "myhostname";
$conn_id = ftp_connect($ftp_server);
$ftp_user_name = "myusename";
$ftp_user_pass = "mypassword";
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if ((!$conn_id) || (!$login_result)) { }
else {
ftp_pasv( $conn_id, true );
$contents = ftp_nlist($conn_id, ".");
$filename = $contents[0]; //name of the file
$handle = fopen($filename, "r");
$file_content = fread($handle, filesize($filename));
print_r($file_content);
}
I run this script from my localhost, but it seems it trys to read a file stored in the same folder where is this php file insted of reading from the ftp server.
You need to download a file from FTP with ftp_get first and then fopen and fread on local machine.
http://php.net/manual/en/function.ftp-get.php

imagejpeg(): Unable to open

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.

How can I open a file from another ftp and write?

I tried to open a file in another ftp and write but I coulnd how can I do that?
$ftpstream = #ftp_connect('****');
//Login to the FTP server
$login = #ftp_login($ftpstream, '****', '***');
if($login) {
echo "Conectado";
$fp = fopen('file.txt', 'w');
fwrite($fp, '1');
fwrite($fp, '23');
fclose($fp);
}
The code you have posted is wrong. It opens up an FTP stream (ftp_connect), but then writes a file to the local file system (fopen). Your ftp stream won't allow you to write to it with fwrite- you need to use the commands to transfer entire files.
You can do what you want with fopen if you use an ftp:// scheme.
For example:
$fp = fopen("ftp://user:password#example.com/file.txt","w");
fwrite($fp, '1');
fwrite($fp, '23');
fclose($fp);
Alternativley, you can write the file to the local file system and use an ftp stream to transfer it:
$file = 'somefile.txt';
// create your file in the local file system here.
$remote_file = 'readme.txt';
// 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);
As always, be clear on what you want to do.
PHP FTP reference is here
PHP scheme & wrapper reference (for use with fopen) is here

Categories