How to upload multiple files of local folder to ftp folder? - php

Error in uploading files of local folder to ftp folder
I tried with ftp_put method and it is not working for large files
function UploadMasterFiles() //uploading files from local folder to ftp
{
try
{
$connection = ftp_connect($ip, $port, $timeout);
$login_result = ftp_login($connection, $un, $pw);
$dir = getcwd()."\\files";
// Open a directory, and read its contents
if($connection)
{
if (is_dir($dir))
{
if ($dh = opendir($dir))
{
while (($file = readdir($dh)) !== false)
{
if($file !== "." && $file !== "..")
{
if (ftp_put($connection,"FOLD\\SPLITFILES\\".$file,$dir."\\".$file,FTP_ASCII))
{
//echo success;
}
}
else{}
}
closedir($dh);
}
}
}
else
{
//echo "not connected";
}
}
catch(Exception $e)
{
echo "\n Exception Caught", $e->getMessage();
}
}
I have files in my local folder named "files" and want to transfer to my ftp server folder "SPLITFILES". This is the task I want to do.
Now, I am getting error at "if (ftp_put($connection,"FOLD\files\".$file,$dir."\".$file,FTP_ASCII))" this line. warning message is "ftp_put Transfer complete". but files not get transferred. But if files are of less size then this is working. I am unable to pick where the problem is. Please guide me where I am wrong.

use this
ftp_put($connection, "FOLD\SPLITFILES\".$file, $dir."\".$file, FTP_BINARY)
Reply if error still exists.

Related

How does one move files to a specific folder after an FTP download using PHP?

Is there a way to move files to a specific folder or declare a specific folder to download files by using FTP methods in PHP?
In short, I am downloading 10,000+ files and I want them to go into a certain folder that I already created. I am using an FTP connection to download the files from my script and I am looping through every file in the FTP server. They all download (and it takes a hell of a long time) - I just need to declare a specific path or move the files to the folder.
Here is the code:
function ftp_sync($dir, $conn_id){
if($dir !== '.'){
if(ftp_chdir($conn_id, $dir) === FALSE){
echo 'Change directory failed: ' . $dir . PHP_EOL;
return;
}
chdir($dir);
}
$contents = ftp_nlist($conn_id, '.');
foreach($contents as $file){
if($file == '.' || $file == '..'){
continue;
}
if(#ftp_chdir($conn_id, $file)){
ftp_chdir($conn_id, "..");
ftp_sync($file, $conn_id);
} else {
ftp_get($conn_id, $file, $file, FTP_BINARY);
//TODO: Download the files into a specific directory
}
}
ftp_chdir($conn_id, '..');
chdir('..');
}
$ftp_server = 'server';
$user = 'user';
$password = 'password';
$document_root = '/';
$sync_path = 'Web_Images';
$conn_id = ftp_connect($ftp_server);
if ($conn_id) {
$login_result = ftp_login($conn_id, $user, $password);
ftp_pasv($conn_id, true);
if ($login_result) {
ftp_chdir($conn_id, $document_root);
ftp_sync($sync_path, $conn_id);
ftp_close($conn_id);
} else {
echo 'login to server failed!' . PHP_EOL;
}
} else {
echo 'connection to server failed!';
}
echo 'done.' . PHP_EOL;
The ftp_get($conn_id, $file, $file, FTP_BINARY); should be able to place your remote file anywhere you want as a default, you just have to indicate that spot in the local parameter:
# Where ever you want to download local files to
$dir = __DIR__.'/my/specific/path/';
# See if directory exists, create if not
if(!is_dir($dir))
mkdir($dir,0755,true);
# Saves the file(s) into the $dir folder with the same name as the remote file
ftp_get($conn_id, $dir.$file, $file, FTP_BINARY);

How to overwrite existing folder or file in php ftp upload?

how to overwrite folder/file if exist via php ftp using ftp_put .
the default is not overwriting the files.
function ftp_putAll($conn_id, $folder, $remotedir) { // Called from moveFolder function at line 161 //
$d = dir($folder);
while($file = $d->read()) { // do this for each file in the directory
if ($file != "." && $file != "..") { // to prevent an infinite loop
if (is_dir($folder."/".$file)) { // do the following if it is a directory
if (!#ftp_chdir($conn_id, $remotedir."/".$file)) {
ftp_mkdir($conn_id, $remotedir."/".$file); // create directories that do not yet exist
}
$stream_options = array('ftp' => array('overwrite' => true));
$this->ftp_putAll($conn_id, $folder."/".$file, $remotedir."/".$file); // recursive part
} else {
if(ftp_put($conn_id, $remotedir."/".$file, $folder."/".$file, FTP_ASCII)) {
$upload = ftp_put($conn_id, $remotedir."/".$file, $folder."/".$file, FTP_ASCII);
}
else {
}
}
It would depend on the implementation of the FTP server. If the file overwrite is not allowed, first delete the file before uploading.
function ftp_putAll($conn_id, $src_dir, $dst_dir){
$d = dir($src_dir);
while($file = $d->read()) { // do this for each file in the directory
if ($file != "." && $file != "..") { // to prevent an infinite loop
if (is_dir($src_dir."/".$file)) { // do the following if it is a directory
if (!#ftp_chdir($conn_id, $dst_dir."/".$file)) {
ftp_mkdir($conn_id, $dst_dir."/".$file); // create directories that do not yet exist
}
ftp_putAll($conn_id, $src_dir."/".$file, $dst_dir."/".$file); // recursive part
} else {
#ftp_put( $conn_id, $dst_dir."/".$file, $src_dir."/".$file, FTP_BINARY);
}
}
}
$d->close();
}
can you please try this above code.
Following are function parameter
connection id ,
source path ,
destination path

how to delete a file from folder in php

I have a folder 'items' in which there are 3 files item1.txt, item2.txt and item3.txt.
I want to delete item2.txt file from folder. I am using the below code but it not deleting a file from folder. Can any body help me in that.
<?php
$data="item2.txt";
$dir = "items";
$dirHandle = opendir($dir);
while ($file = readdir($dirHandle)) {
if($file==$data) {
unlink($file);
}
}
closedir($dirHandle);
?>
Initially the folder should have 777 permissions
$data = "item2.txt";
$dir = "items";
while ($file = readdir($dirHandle)) {
if ($file==$data) {
unlink($dir.'/'.$file);
}
}
or try
$path = $_SERVER['DOCUMENT_ROOT'].'items/item2.txt';
unlink($path);
No need of while loop here for just deleting a file, you have to pass path of that file to unlink() function, as shown below.
$file_to_delete = 'items/item2.txt';
unlink($file_to_delete);
Please read details of unlink() function
http://php.net/manual/en/function.unlink.php
There is one bug in your code, you haven't given the correct path
<?php
$data="item2.txt";
$dir = "items";
$dirHandle = opendir($dir);
while ($file = readdir($dirHandle)) {
if($file==$data) {
unlink($dir."/".$file);//give correct path,
}
}
closedir($dirHandle);
?>
unlink
if($file==$data) {
unlink( $dir .'/'. $file);
}
It's very simple:
$file='a.txt';
if(unlink($file))
{
echo "file named $file has been deleted successfully";
}
else
{
echo "file is not deleted";
}
//if file is in other folder then do as follows
unlink("foldername/".$file);
try renaming it to the trash or a temp folder that the server have access **UNLESS IT'S sensitive data.
rename($old, $new) or die("Unable to rename $old to $new.");

file upload in multiple directory

This is a script that Upload a file in all directory. But when i run it, its only upload One time and then fail to upload . whats wrong in this code ?
function read_directory($p_pathname)
{
$d = dir ($p_pathname);
$target = $p_pathname;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target.$_FILES['uploaded']['name']))
{
echo $target. "Done<br>";
}
else
{
echo $target."Sorry<br>";
}
while (($file = $d->read()) !== false)
{
if (($file != ".") and ($file != ".."))
{
$filetype = filetype ("{$d->path}/{$file}");
if ($filetype == "dir")
{
read_directory ("{$d->path}/{$file}");
}
else
{
// echo "\tFILE: {$d->path}/{$file}\n";
}
}
}
$d->close;
}
Use copy() instead of move_uploaded_file(). move_uploaded_file() deletes the source file when it's done, so you can't use it multiple times on the same file. copy() leaves the original file alone, so you can do it as many times as you want.
When the script exits, PHP automatically removes the temp file that was uploaded if it doesn't get moved by the script.
in the first time your moving the file... not copying ... so only next time that file not in the temp directory, so you can't move again..

How can I open image file with fopen function in FTP Server in php

$conn = ftp_connect("ftp.mywebsite.com") or die("Could not connect");
ftp_login($conn,"admin","password");
$self = $_SERVER['PHP_SELF'];
$file = $_FILES["file"]["name"];
if ($_FILES['file']['error'] !== UPLOAD_ERR_OK)
{
die("Upload failed");
}
else
{
$fp = fopen($file, 'r')or die("Unable to open $file for reading");
if(ftp_fput($conn, $file,$fp, FTP_BINARY))
{
echo "success";
}
else
{
echo "error";
}
}
How can I open image file with fopen function in FTP Server in php. (failed to open stream: No such file or directory )
There is no reason in using fopen function on FTP Server.
Assuming from your code, your real task is just to safe uploaded file on the same server.
you have to use move_uploaded_file() then
If you really need to save uploaded file not locally but on FTP server, ftp_put $_FILE['file']['tmp_name'] then.

Categories