how to implement pagination while fetching data from file server in php - php

I wrote a php script which fetches the data from file server,it lists all the uploaded file on server.
Now problem is there is large no. of file on my server ,i want limited results per page how can i implement pagination for this.
My php code is :
$ftp_server = "ftp.abc.com";
$ftp_user = "uploads#abc.com";
$ftp_password = "xyz";
$conn = ftp_connect($ftp_server) or die("could not connect to ftp server");
if (ftp_login($conn, $ftp_user, $ftp_password)) {
$contents = ftp_nlist($conn, $path);
foreach($contents as $value) {
$i++;
echo "$i <a href='#form' id='url_title' class='links'>$value</a><br><br> <br>";
}
}

Related

How to upload files from database on FTP remote server? [duplicate]

This question already has answers here:
PHP ftp_put fails
(2 answers)
Closed 1 year ago.
I use PHP to upload images from a host into FTP remote server.
I have already stored the names of the files that need to be uploaded to the FTP remote server, stored on the database.
After connecting to DB and a query for select information of images, I passed variables with $_SESSION to another PHP file for upload them on FTP remote server like this:
conn_auth.php for select target images:
<?php
if (file_exists(__DIR__.'/conn_auth/db_conn.php')){
include __DIR__.'/conn_auth/db_conn.php';
}else{
die('Connection is Failed ...!');
}
$conn = OpenConn();
$sql = "SELECT * FROM users where status = 'Active'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$user_id = $row["id"];
//search for user_id in userAthentication Table in DB
$sql = "SELECT * FROM userauthentication where user_id = $user_id";
$result = $conn->query($sql);
$user_auth = $result->fetch_assoc();
$user = array(
'id'=>$row['id'],
'name'=>$row['name'],
'family'=>$row['family'],
'auth_image'=>'',
'file_name'=>$user_auth["image"],
'phone'=>$row["phone"]
);
$url = "/home/root/app_name_1/uploads/".$user["file_name"];
if(!file_exists($url)){
$url = "/home/root/app_name_2/uploads/".$user["file_name"];
}
$user['auth_image'] = $url;
session_start();
$_SESSION["userInfo"] = $user;
$_SESSION["userAuth"] = $user_auth;
if (file_exists(__DIR__.'/conn_auth/get_auth.php')){
include __DIR__.'/conn_auth/get_auth.php';
}else{
die("I can't Connect to the Server");
}
}
} else {
echo "0 results";
}
CloseConn($conn);
On this file, I try to connect to FTP remote server and upload images with the ftp_put() method, but it does not work:
<?php
include_once __DIR__.'../conn_auth.php';
session_start();
$ftp_server="*.***.***.**";
$ftp_user_name="****";
$ftp_user_pass="**********";
$file = $_SESSION["userInfo"]['auth_image']; //to be uploaded
$folder_name = $_SESSION["userInfo"]['phone'];
$file_name = $_SESSION["userInfo"]["file_name"];
$remote_file = "/HDD/pics/$folder_name";
$conn_id = ftp_connect($ftp_server,21);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_mkdir($conn_id,'auth_pics').ftp_chdir($conn_id,'auth_pics');
if (ftp_mkdir($conn_id,"$folder_name")){
ftp_chdir($conn_id,$folder_name);
}else{
echo "I Can't Create Folder For : ".$folder_name;
die();
}
// upload a file
$upload = ftp_put($conn_id, $remote_file, $file, FTP_BINARY);
if ($upload) {
echo "successfully uploaded : $file\n";
} else {
echo "There was a problem while uploading...\n";
}
// close the connection
ftp_close($conn_id);
?>
Mostly your FTP server doesn't support active mode connections, so you may try to switch to the passive mode using the ftp_pasv, but make sure to do this after you logged into the server (after ftp_login).
If you face some troubles while using the native API functions, you may want to try out an FTP client library, like this, that I've built.
Hope you find this answer useful for you.

Saving a file from one server to another

I have a web form that I will be hosting on a centOS web server. The code that I have written, will take the data that has been entered, and save it to an excel spreadsheet to a specific location. i also have a file server, this is where I would like to have the file saved to. this is the first time that i have done a server to server file save/copy/transfer. on the web server, would i have to install an ftp client to accomplish this? When testing the program locally on my computer, this is how i currently have the destination path set up.
file_put_contents('C:\Users\username\Box Sync\Driver Check In\Shortage'.
$month. '\Shortage'.date('m-d-y').".csv", $result, FILE_APPEND);
with the code being hosted on the web server, how can i change the destination path to point to my file server?
Here is a sample script of how to upload file from your localhost to any FTP server.
Source: PHP Manual
<?php
$host = '*****';
$usr = '*****';
$pwd = '**********';
$local_file = './orderXML/order200.xml';
$ftp_path = 'order200.xml';
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
ftp_pasv($resource, true);
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
// perform file upload
ftp_chdir($conn_id, '/public_html/abc/');
$upload = ftp_put($conn_id, $ftp_path, $local_file, FTP_ASCII);
if($upload) { $ftpsucc=1; } else { $ftpsucc=0; }
// check upload status:
print (!$upload) ? 'Cannot upload' : 'Upload complete';
print "\n";
// close the FTP stream
ftp_close($conn_id);
?>

PHP ftp_nlist() returning bool(false) even with passive mode and different users

I'm currently making a script that will backup files that I have on my server but am having a major issue. About 30 minutes ago I was able to connect using the same script, list my files, and download them. But now when I try to connect, it connects and that is it, I can't ftp_nlist the files because when I do it just returns bool(false). I read on some other posts that I should try setting passive mode to true with ftp_pasv but it did not change anything. I've also tried other users which do the same exact thing. I tried using the credentials on filezilla and it connected and allowed me to download with no issues. Any help would be greatly appreciated, I'll post the code below.
PHP:
Connect to FTP
$ftp_server = "Hidden for security";
$ftp_user = "Hidden for security";
$ftp_pass = "Hidden for security";
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
ftp_pasv($conn_id, true);
Get filenames on FTP
$contents = ftp_nlist($conn_id, ".");
$rm = array(0, 1, 2, 3,);
foreach($rm as $value) {
unset($contents[$value]);
}
$contents = array_values($contents);
Get filenames on localhost
$dir = scandir("images/");
$rmval = array(0, 1, 2);
foreach ($rmval as $val) {
unset($dir[$val]);
}
$dir = array_values($dir);
Try to login
if (ftp_login($conn_id, $ftp_user, $ftp_pass)) {
//echo "Connected";
$counter = 0;
var_dump($contents);
/*foreach ($ftp_content as $file) {
ftp_get($conn_id, "images/" . $ftp_content[$counter], $ftp_content[$counter], FTP_BINARY);
$counter++;
}*/
} else {
echo "Couldn't Connect";
}
Close FTP connection
ftp_close($conn_id);
The issue was that I was trying to list my files before establishing a connection to the FTP server. Adding $login_result = ftp_login($conn_id, $ftp_user, $ftp_pass) after $conn_id fixed my issue. Now I'm able to evaluate if it's true or false and run different code depending on the result.

can't download file from ftp with ftp_get (no error)

I'm trying to download file from ftp, but whatever i tried i couldn't download any file from ftp and it is not giving any error.
My code fetching file names from database and compares these file names with existing ftp file(names). If file name exist on ftp i want to download this file to my local-destination folder.
I can read ftp file names but i can't download them. Hope you can help me.
Here is my code
error_reporting(E_ALL);
$ftp_server = "xx.xx.y12.34"; //server
$bt = ftp_connect($ftp_server); // connect to ftp
$username = "myusername"; $pass = "mypass"; //username and password
$connection = ftp_login($bt, $username, $pass); // login ftp with username and password
ftp_pasv($bt, true);
if ((!$bt) || (!$connection)) {
echo "failed";
exit;
}else {
$path = "/1_Swatch Files/LG Swatches/";
$destination_folder = "/home/faruk/lasenza/media/color_samples/";
// mysql connection codes..
$results = $readConnection->fetchAll($query); // fetch file names from database
$contents_on_server = ftp_nlist($bt, $path);
foreach($results as $result){
$check_file_exist = $result["CLR_CODE"].".gif";
if(in_array($check_file_exist, $contents_on_server)) {
$server_file = $result["CLR_CODE"].".gif";
if (ftp_get($bt, $destination_folder , $server_file, FTP_BINARY)) {
echo 'Succeeded';
}else{
echo "There was a problem";
}
break;
}
}
}

PHP File Transfer Script .. ALMOST DONE!

Can someone figure out what this is not working? I kept trying different things, double checking syntax etc but I get the error message I have setup for when a file does not transfer. This is my first PHP script.
I appreciate any help I will do research in the meantime.
Using NetBeans
<?php
$user = "username";
$password = "password";
$server_file = 'mag_hounddog.mp3';
$local_file = 'C:\users\kazuiman\desktop\mag_hounddog.mp3';
$ftp_server = "ftp.mysite.com";
// setup a basic connection to the ftp server
$connectionID = ftp_connect($ftp_server) or die("Connection was Not Successful");
// login with username and password for your account
$loginResult = ftp_login($connectionID, $user, $password);
echo ftp_pwd($connectionID);
// changes the directory to the tvstation you can hard code one in here.
if (ftp_chdir($connectionID, "test1nbiiwcdp_apps/tv2beta/streams/_definst_/")) {
echo "Current Directory is Now..." . ftp_pwd($connectionID) . "\n";
} else {
echo "Error Changing Directory .. perhaps it doesn't exist?";
}
// Now to download the files and put them onto your local machine
if (ftp_get($connectionID, $local_file, $remote_file, FTP_BINARY)) {
echo "Succesfully written $server_file to $local_file\n";
} else {
echo "There was a problem with the FTP transfer please check script\n";
}
// close the connection
ftp_close($connectionID)
?>
You have never defined the variable $remote_file.
In your preamble it is called $server_file.
Is this a copy-paste-error?

Categories