PHP read remote file through FTP connection





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I have simple code to load data from txt file to mysql, it works perfect but my last one problem is that my file is on ftp server not in local folder.
Below is my code how rewrite this line to connect with my file on ftp (test.txt) with using credentials?



$open=fopen('test.txt','r');

$conn = mysqli_connect('localhost','user','password','db_name');

if(!$conn)
{
die(mysqli_error());
}

$query = mysqli_query($conn, "DELETE FROM test");

$open = fopen('test.txt','r');

fgets($open);
fgets($open);
while (!feof($open))
{
$getTextLine = fgets($open);
$explodeLine = explode("|",$getTextLine);

list($Login,$Inday,$Start_przerwy,$Koniec_przerwy,$Czas_przerwy,$Odcinek) = $explodeLine;

$qry = "insert into test (Login,Inday,Start_przerwy,Koniec_przerwy,Czas_przerwy,Odcinek) values('".$Login."','".$Inday."','".$Start_przerwy."','".$Koniec_przerwy."','".$Czas_przerwy."','".$Odcinek."')";
mysqli_query($conn,$qry);
}
fclose($open);
echo "done";









share|improve this question




















  • 1





    You might want to consider writing a shell script and then having PHP execute the shell script. You could even pass variables to it.

    – Difster
    Nov 16 '18 at 18:10






  • 1





    This is open to (sql) injection; use a prepared statement if you don't want your database to suddenly disappear one day.

    – Funk Forty Niner
    Nov 16 '18 at 18:26




















1















I have simple code to load data from txt file to mysql, it works perfect but my last one problem is that my file is on ftp server not in local folder.
Below is my code how rewrite this line to connect with my file on ftp (test.txt) with using credentials?



$open=fopen('test.txt','r');

$conn = mysqli_connect('localhost','user','password','db_name');

if(!$conn)
{
die(mysqli_error());
}

$query = mysqli_query($conn, "DELETE FROM test");

$open = fopen('test.txt','r');

fgets($open);
fgets($open);
while (!feof($open))
{
$getTextLine = fgets($open);
$explodeLine = explode("|",$getTextLine);

list($Login,$Inday,$Start_przerwy,$Koniec_przerwy,$Czas_przerwy,$Odcinek) = $explodeLine;

$qry = "insert into test (Login,Inday,Start_przerwy,Koniec_przerwy,Czas_przerwy,Odcinek) values('".$Login."','".$Inday."','".$Start_przerwy."','".$Koniec_przerwy."','".$Czas_przerwy."','".$Odcinek."')";
mysqli_query($conn,$qry);
}
fclose($open);
echo "done";









share|improve this question




















  • 1





    You might want to consider writing a shell script and then having PHP execute the shell script. You could even pass variables to it.

    – Difster
    Nov 16 '18 at 18:10






  • 1





    This is open to (sql) injection; use a prepared statement if you don't want your database to suddenly disappear one day.

    – Funk Forty Niner
    Nov 16 '18 at 18:26
















1












1








1








I have simple code to load data from txt file to mysql, it works perfect but my last one problem is that my file is on ftp server not in local folder.
Below is my code how rewrite this line to connect with my file on ftp (test.txt) with using credentials?



$open=fopen('test.txt','r');

$conn = mysqli_connect('localhost','user','password','db_name');

if(!$conn)
{
die(mysqli_error());
}

$query = mysqli_query($conn, "DELETE FROM test");

$open = fopen('test.txt','r');

fgets($open);
fgets($open);
while (!feof($open))
{
$getTextLine = fgets($open);
$explodeLine = explode("|",$getTextLine);

list($Login,$Inday,$Start_przerwy,$Koniec_przerwy,$Czas_przerwy,$Odcinek) = $explodeLine;

$qry = "insert into test (Login,Inday,Start_przerwy,Koniec_przerwy,Czas_przerwy,Odcinek) values('".$Login."','".$Inday."','".$Start_przerwy."','".$Koniec_przerwy."','".$Czas_przerwy."','".$Odcinek."')";
mysqli_query($conn,$qry);
}
fclose($open);
echo "done";









share|improve this question
















I have simple code to load data from txt file to mysql, it works perfect but my last one problem is that my file is on ftp server not in local folder.
Below is my code how rewrite this line to connect with my file on ftp (test.txt) with using credentials?



$open=fopen('test.txt','r');

$conn = mysqli_connect('localhost','user','password','db_name');

if(!$conn)
{
die(mysqli_error());
}

$query = mysqli_query($conn, "DELETE FROM test");

$open = fopen('test.txt','r');

fgets($open);
fgets($open);
while (!feof($open))
{
$getTextLine = fgets($open);
$explodeLine = explode("|",$getTextLine);

list($Login,$Inday,$Start_przerwy,$Koniec_przerwy,$Czas_przerwy,$Odcinek) = $explodeLine;

$qry = "insert into test (Login,Inday,Start_przerwy,Koniec_przerwy,Czas_przerwy,Odcinek) values('".$Login."','".$Inday."','".$Start_przerwy."','".$Koniec_przerwy."','".$Czas_przerwy."','".$Odcinek."')";
mysqli_query($conn,$qry);
}
fclose($open);
echo "done";






php ftp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 18:26









Funk Forty Niner

1




1










asked Nov 16 '18 at 18:06









markmark

82




82








  • 1





    You might want to consider writing a shell script and then having PHP execute the shell script. You could even pass variables to it.

    – Difster
    Nov 16 '18 at 18:10






  • 1





    This is open to (sql) injection; use a prepared statement if you don't want your database to suddenly disappear one day.

    – Funk Forty Niner
    Nov 16 '18 at 18:26
















  • 1





    You might want to consider writing a shell script and then having PHP execute the shell script. You could even pass variables to it.

    – Difster
    Nov 16 '18 at 18:10






  • 1





    This is open to (sql) injection; use a prepared statement if you don't want your database to suddenly disappear one day.

    – Funk Forty Niner
    Nov 16 '18 at 18:26










1




1





You might want to consider writing a shell script and then having PHP execute the shell script. You could even pass variables to it.

– Difster
Nov 16 '18 at 18:10





You might want to consider writing a shell script and then having PHP execute the shell script. You could even pass variables to it.

– Difster
Nov 16 '18 at 18:10




1




1





This is open to (sql) injection; use a prepared statement if you don't want your database to suddenly disappear one day.

– Funk Forty Niner
Nov 16 '18 at 18:26







This is open to (sql) injection; use a prepared statement if you don't want your database to suddenly disappear one day.

– Funk Forty Niner
Nov 16 '18 at 18:26














1 Answer
1






active

oldest

votes


















1














Simply the same but define protocol ftp:// and username, password



$file = "ftp://username:pa‌​ssword@hostname/path/to/test.txt";
$open = fopen($file, "r");


or:



$file = "ftp://username:pa‌​ssword@hostname/path/to/test.txt";
$lines = explode("n", file_get_contents($file));

foreach($lines AS $line) {
$line = trim($line);
// body of while
}





no need for ftp_* methods, keep it simple (:






share|improve this answer


























  • works fine! how can I add to first option

    – mark
    Nov 16 '18 at 18:25












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343212%2fphp-read-remote-file-through-ftp-connection%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Simply the same but define protocol ftp:// and username, password



$file = "ftp://username:pa‌​ssword@hostname/path/to/test.txt";
$open = fopen($file, "r");


or:



$file = "ftp://username:pa‌​ssword@hostname/path/to/test.txt";
$lines = explode("n", file_get_contents($file));

foreach($lines AS $line) {
$line = trim($line);
// body of while
}





no need for ftp_* methods, keep it simple (:






share|improve this answer


























  • works fine! how can I add to first option

    – mark
    Nov 16 '18 at 18:25
















1














Simply the same but define protocol ftp:// and username, password



$file = "ftp://username:pa‌​ssword@hostname/path/to/test.txt";
$open = fopen($file, "r");


or:



$file = "ftp://username:pa‌​ssword@hostname/path/to/test.txt";
$lines = explode("n", file_get_contents($file));

foreach($lines AS $line) {
$line = trim($line);
// body of while
}





no need for ftp_* methods, keep it simple (:






share|improve this answer


























  • works fine! how can I add to first option

    – mark
    Nov 16 '18 at 18:25














1












1








1







Simply the same but define protocol ftp:// and username, password



$file = "ftp://username:pa‌​ssword@hostname/path/to/test.txt";
$open = fopen($file, "r");


or:



$file = "ftp://username:pa‌​ssword@hostname/path/to/test.txt";
$lines = explode("n", file_get_contents($file));

foreach($lines AS $line) {
$line = trim($line);
// body of while
}





no need for ftp_* methods, keep it simple (:






share|improve this answer















Simply the same but define protocol ftp:// and username, password



$file = "ftp://username:pa‌​ssword@hostname/path/to/test.txt";
$open = fopen($file, "r");


or:



$file = "ftp://username:pa‌​ssword@hostname/path/to/test.txt";
$lines = explode("n", file_get_contents($file));

foreach($lines AS $line) {
$line = trim($line);
// body of while
}





no need for ftp_* methods, keep it simple (:







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 18:19

























answered Nov 16 '18 at 18:13









num8ernum8er

12.1k22240




12.1k22240













  • works fine! how can I add to first option

    – mark
    Nov 16 '18 at 18:25



















  • works fine! how can I add to first option

    – mark
    Nov 16 '18 at 18:25

















works fine! how can I add to first option

– mark
Nov 16 '18 at 18:25





works fine! how can I add to first option

– mark
Nov 16 '18 at 18:25




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343212%2fphp-read-remote-file-through-ftp-connection%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values