<?php
// FTP access parameters
$source_ftp_server = 'ftp://uk9.siteground.eu';
$source_ftp_user_name = 'xyz';
$source_ftp_user_pass = 'abc';
// file to move:
$local_file = '/Users/Swayam/Desktop/Test.csv';
$remote_file = '/home/c10mill2/public_html/edtopiadb/Test.csv';
$fp = fopen($local_file, 'r');
$conn_id = ftp_connect($source_ftp_server);
$login_result = ftp_login($conn_id, $source_ftp_user_name, $source_ftp_user_pass);
$ret = ftp_nb_fput($conn_id, $remote_file, $fp, FTP_BINARY);
while ($ret == FTP_MOREDATA) {
// Establish a new connection to FTP server
if(!isset($conn_id2)) {
$conn_id2 = ftp_connect($source_ftp_server);
$login_result2 = ftp_login($conn_id2, $source_ftp_user_name, $source_ftp_user_pass);
}
// Retreive size of uploaded file.
if(isset($conn_id2)) {
clearstatcache(); // <- this must be included!!
$remote_file_size = ftp_size($conn_id2, $remote_file);
}
// Calculate upload progress
$local_file_size = filesize($local_file);
if (isset($remote_file_size) && $remote_file_size > 0 ){
$i = ($remote_file_size/$local_file_size)*100;
printf("%d%% uploaded<br>", $i);
flush();
}
$ret = ftp_nb_continue($conn_id);
}
if ($ret != FTP_FINISHED) {
echo "<span style='color:red;'><b>There was an error uploading the file...</b></span><br>";
exit(1);
}
else {
echo "<br>Files successfully uploaded!<br><br>";
}
fclose($fp);
Pls help me with the code to get it working.
When i execute this code i get a error -
Warning: ftp_connect(): php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known in /Users/Swayam/Desktop/sample.php on line 14
Warning: ftp_login() expects parameter 1 to be resource, boolean given in /Users/Swayam/Desktop/sample.php on line 15
Warning: ftp_nb_fput() expects parameter 1 to be resource, boolean given in /Users/Swayam/Desktop/sample.php on line 16
<span style='color:red;'><b>There was an error uploading the file...</b></span><br>
Related
i have one php file and i am using ftp_get() function in it. when i run it from server localhost it's running fine but when i run it from schedule task it's giving error. here's my code.
<?php
$ftp_server = "IP";
$conn_id = ftp_connect ($ftp_server)
or die("Couldn't connect to $ftp_server");
$login_result = ftp_login($conn_id, "syslog", "Gms$67");
if ((!$conn_id) || (!$login_result))
die("FTP Connection Failed");
ftp_pasv($conn_id, true);
echo "Start time:".date("d-m-Y h:i:sa")."<br/>";
ftp_sync ("."); // Use "." if you are in the current directory
echo "<br/>End time:".date("d-m-Y h:i:sa")."<br/>";
ftp_close($conn_id);
// ftp_sync - Copy directory and file structure
function ftp_sync ($dir) {
global $conn_id;
if ($dir != ".") {
if (ftp_chdir($conn_id, $dir) == false) {
echo ("Change Dir Failed: $dir<BR>\r\n");
return;
}
if (!(is_dir($dir)))
mkdir($dir);
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);
}
else
{
if(ftp_get($conn_id, "files/syslog.txt", $file, FTP_BINARY))
{
$mbody= nl2br(file_get_contents("files/syslog.txt"));
//$f = fopen('files/syslog.txt', 'r+');
//$mbody = fread($f, filesize("files/syslog.txt"));
//fclose($f);
$resArray = explode("\n",$mbody);
foreach($resArray as $r) {
$ArrayResults[] = explode(" ",$r,6);
}
/*echo "<pre>";
print_r($ArrayResults);
echo "</pre>";*/
$fields="srdate,srtime,facility,severity,host,message";
foreach ($ArrayResults as $rowValues) {
//echo "vip==".$rowValues[0]."--<br/>";
if($rowValues[0]!="")
{
$serarray = explode(".",$rowValues[2]);
//echo "servi=".$serarray[1]."<br/>";
$facility=$serarray[0];
$severity=$serarray[1];
/*foreach ($rowValues as $key => $rowValue) {
$rowValues[$key] = mysql_real_escape_string($rowValues[$key]);
}*/
$rowValues1[0] = mysql_real_escape_string($rowValues[0]);
$rowValues1[1] = mysql_real_escape_string($rowValues[1]);
$rowValues1[2] = mysql_real_escape_string($facility);
$rowValues1[3] = mysql_real_escape_string($severity);
$rowValues1[4] = mysql_real_escape_string($rowValues[3]);
$rowValues1[5] = mysql_real_escape_string($rowValues[5]);
$values[] = "('" . implode('\', \'', $rowValues1) . "')";
unset($serarray);
}
}
include("config.php");
$query = "INSERT INTO syslogtbl ($fields) VALUES " . implode (', ', $values) . "";
if(mysql_query($query))
{
echo "Successfully Insert to Database.";
}
else
{
echo "<br/>Error in insert syslog".mysql_error();;
}
}
else
{
echo "Error downloading $server_file.";
}
}
}
ftp_chdir ($conn_id, "..");
chdir ("..");
}
?>
when i run from schedule task it shows not any error but also not anything change in output(it must change). then i run file from batch file and it shows error in command prompt is.
C:\script>C:\wamp\bin\php\php5.5.12\php.exe C:\wamp\www\syslog\schedule_readtxt.php
Start time:24-11-2016 12:33:41pm<br/>
Warning: ftp_get(files/syslog.txt): failed to open stream: No such file or directory in C:\wamp\www\syslog\schedule_readtxt.php on line 69
Call Stack:
0.0000 250200 1. {main}() C:\wamp\www\syslog\schedule_readtxt.php:0
0.0156 259928 2. ftp_sync() C:\wamp\www\syslog\schedule_readtxt.php:35
0.0156 261832 3. ftp_get() C:\wamp\www\syslog\schedule_readtxt.php:69
Warning: ftp_get(): Error opening files/syslog.txt in C:\wamp\www\syslog\schedul
e_readtxt.php on line 69
Call Stack:
0.0000 250200 1. {main}() C:\wamp\www\syslog\schedule_readtxt.php:0
0.0156 259928 2. ftp_sync() C:\wamp\www\syslog\schedule_readtxt.php:35
0.0156 261832 3. ftp_get() C:\wamp\www\syslog\schedule_readtxt.php:69
Notice: Undefined variable: server_file in C:\wamp\www\syslog\schedule_readtxt.p
hp on line 118
Call Stack:
0.0000 250200 1. {main}() C:\wamp\www\syslog\schedule_readtxt.php:0
0.0156 259928 2. ftp_sync() C:\wamp\www\syslog\schedule_readtxt.php:35
Error downloading .<br/>End time:24-11-2016 12:33:41pm<br/>
C:\script>pause
Press any key to continue . . .
i don't know how to run this script from schedule task. please help me.
Im trying to upload a mp3 file to my ftp server:
$data = substr($_POST['data'], strpos($_POST['data'], ",") + 1);
$decodedData = base64_decode($data);
$filename = urldecode($_POST['fname']);
$cid = ftp_connect("foo.com");
$result = ftp_login($cid, "rodrigo#foo.com","password");
if ((!$cid) || (!$result)) {
echo "connection failed"; die;
} else {
echo "connected";
}
ftp_pasv ($cid, true);
ftp_chdir($cid, "my_folder");
if (ftp_put($cid, $filename, $decodedData, FTP_BINARY)) {
//...
} else {
//...
}
I have this warning:
Warning: ftp_put(���) [function.ftp-put]: failed to open stream: Invalid argument in...
I cant find out how to send a valid argument
You will need to create a file that you can pass, the below example writes the file to memory instead of a file on your disk ... but you may want to write it to disk depending on the size.
$tmp = fopen('php://memory', 'r+');
fputs($tmp, $decodedData);
rewind($tmp);
if (ftp_fput($cid, $filename, $tmp, FTP_BINARY)) {
}
I am trying to find the WAV file duration using the following code.
if ( !file_exists($location_wmv.$name_wmv) ) {
echo "<BR> File Does not Exist<BR>";
} else {
echo "<BR> File Exist<BR>";
}
$file = $location_wmv.$name_wmv;
$fp = fopen($file, ‘r’);
$size_in_bytes = filesize($file);
fseek($fp, 20);
$rawheader = fread($fp, 16);
$header = unpack(‘vtype/vchannels/Vsamplerate/Vbytespersec/valignment/vbits’,
$rawheader);
$sec = ceil($size_in_bytes/$header['bytespersec']);
$duration_wmv = $sec;
echo "<BR> Raw Suration.". $duration_wmv . "<BR>";
$duration_wmv = gmdate("H:i:s", $demo_song_duration_sec);
echo "<BR>WAV Duration".$duration_wmv;
Here the file_exists function says that file Exist. But the fopen says failed to open stream: Undefined error: 0
The file is actually present in the folder. But still I get this error.
This:
$fp = fopen($file, ‘r’);
looks iffy. Are those backticks?
Maybe try:
$fp = fopen($file, 'r');
(simple quotes)
I use newrelic to keep track of anything on my website and I always get this error:
Error message: E_WARNING: fclose() expects parameter 1 to be resource, boolean given
Stack trace: in fclose called at /etc/snmp/bfd-stats.php (68)
This is how /etc/snmp/bfd-stats.php looks like
<?php
$a = 0;
$ptr = 0;
$any = 0;
$mx = 0;
$ns = 0;
$cname = 0;
$soa = 0;
$srv = 0;
$aaaa = 0;
$txt = 0;
$total = 0;
if(file_exists('/etc/snmp/bfd-log-pos.stat')) {
$lfh = fopen('/etc/snmp/bfd-log-pos.stat','r');
$string = fread($lfh,2087);
$res = explode(',',$string);
fclose($lfh);
}
else {
$res = array();
$res[0] = 0;
$res[1] = 0;
}
if(file_exists("/var/log/bfd_log.1")) {
$stats = stat('/var/log/bfd_log.1');
if($stats[10] > $res[0]) {
$res[0] = 0;
$res[1] = 0;
}
}
$fh = fopen('/var/log/bfd_log', 'r');
fseek($fh,$res[1]);
$blocks = 0;
if(!$fh) {
echo "Error! Couldn't open the file.";
} else {
while (!feof($fh)) {
$data = fgets($fh);
if(preg_match('/executed\sban/',$data)) {
$blocks++;
}
}
}
$lfh = fopen('/etc/snmp/bfd-log-pos.stat','w');
$timestamp = time();
$pos = ftell($fh);
fwrite($lfh,"$timestamp,$pos");
fclose($lfh);
if(!fclose($fh)) {
echo "Error! Couldn't close the file.";
}
print("bfd_blocks\n$blocks");
?>
On line 40: $fh = fopen('/var/log/bfd_log', 'r'); I looked at the directory /var/log and there is no file called bfd_log, I dont know if I have to create it by myself or it is automatically created.
Can anyone help me on fixing this error, Thanks in advance.
The error indicates that you are trying to pass a variable with a boolean value (true/false) to a function that needs a resource instead of a boolean value.
Please make sure that before you use resources from variables, the function that returns the resource has not run into trouble. Only on success perform the other functions that use this resource/variable.
$fh = fopen('/var/log/bfd_log', 'r');
// check fh before other functions use this variable
if (!$fh) {
echo "Error! Couldn't open the file.";
} else {
// perform task with resource $fh
fseek($fh, $res[1]);
[...]
$lfh = fopen('/etc/snmp/bfd-log-pos.stat', 'w');
// check before other code block is executed and use this variable
if( $lfh )
{
// perform task with resource $lfh
$pos = ftell($fh);
fwrite($lfh, "$timestamp,$pos");
fclose($lfh);
fclose($fh);
[...]
} else {
// lfh error
}
}
If you always check before using variables, you won't run into this error anymore.
I wrestled with this problem and could not find the answer until I separated my write check (put it first) from the actual file write code. So before I would open the file fopen/fwrite then do the is_writable check and then do the fclose and i would get this error.
To resolve I moved the is_writable and variable declaration before the fopen/fwrite and the error went away. Shown below (former php code position shown in comments) The first comment did help me realize this... Thank you.
$myfile = "/var/www/html/newfile.txt";
if (is_writable($myfile)) {
echo "The file is writable";
}
else {
echo "The file is not writable";
}
$txt = "$name, $email, $command, $searchtype, $keyword \n";
$myfile = fopen('/var/www/html/newfile.txt', 'w') or die("Unable to open file!");
fwrite($myfile, $txt);
// $myfile = "/var/www/html/newfile.txt";
// if (is_writable($myfile)) {
// echo "The file is writable";
// }
// else {
// echo "The file is not writable";
// }
fclose($myfile);
Try
$fh = fopen('/var/log/bfd_log', 'a+');
a+ mode will create the file if it does not exists
I have a script which connect my source FTP and destination FTP and copy all files from source to destination now i have change my server and it returns me these errors ftp connection suspenseful but didn't copy files just create files. My code is written below.
$ftp1 = new ClsFTP("$user1","$pass1", "$site1");
if(!$ftp1)
{
echo "could not connect";
exit;
}
$ftp2 = new ClsFTP("$user2","$pass2", "$site2");
$mod = "0755";
$dir_list[] = "httpdocs";
for($i=0;$i<count($dir_list); $i++)
{
echo "<br>Copying Directory: " . $dir_list[$i];
if(!$ftp1->cd($dir_list[$i]))
{
echo "1::$site1 could not open directory: $dir_list[$i]. skipping<br>\n";
continue;
}
if(!$ftp2->cd($dir_list[$i]))
{
echo "2::$site2 could not open directory: $dir_list[$i]. Trying to create it<br>\n";
if(!$ftp2->mkdir($dir_list[$i]))
{
echo "2::$site2 could not create directory: $dir_list[$i]. skipping<br>\n";
continue;
}
if(!$ftp2->cd($dir_list[$i]))
{
echo "2::$site2 could not open directory: $dir_list[$i]. skipping<br>\n";
continue;
}
}
if($dir_list[$i]=="httpdocs/tmpupload"){
}else{
$dir_rawlist = $ftp1->rawlist();
$dir_nlist = $ftp1->nlist();
$ftp1->p($dir_rawlist);
#$ftp1->p($dir_nlist);
for($k = 0; $k<count($dir_nlist); $k++)
{
if($dir_list[$i]=="httpdocs/includes" && $dir_nlist[$k]=="cert_key_pem.txt"){
continue;
}
if($dir_list[$i]=="httpdocs" && $dir_nlist[$k]=="PayPal.class.php"){
continue;
}
$size = $ftp1->check_file($dir_nlist[$k]);
if($size >0)//substr($dir_rawlist[$k], 0, 1)=='-')//its a file
{
$ftp1->get("tmp/".$dir_nlist[$k], $dir_nlist[$k]);
$handle = fopen("tmp/".$dir_nlist[$k], 'r');
$contents = fread($handle, filesize("tmp/".$dir_nlist[$k]));
fclose($handle);
$contents = str_replace($search_for, $replace_with, $contents);
$handle = fopen("tmp/".$dir_nlist[$k], 'w');
fwrite($handle, $contents);
fclose($handle);
$ftp2->put($dir_nlist[$k], "tmp/".$dir_nlist[$k]);
$ftp2->chmod($dir_nlist[$k], $mod);
}
}
}
$ftp1->cd("/");
$ftp2->cd("/");
}
These warnings thrown by script.
Warning: ftp_get(): Error opening tmp/InviteFriend2.php in /var/www/vhosts/virtualphoneline.com/httpdocs/includes/ftp.class.php on line 120
Warning: fopen(tmp/InviteFriend2.php): failed to open stream: Permission denied in /var/www/vhosts/virtualphoneline.com/httpdocs/includes/clone.php on line 228
Warning: fwrite() expects parameter 1 to be resource, boolean given in /var/www/vhosts/virtualphoneline.com/httpdocs/includes/clone.php on line 229
Warning: fclose() expects parameter 1 to be resource, boolean given in /var/www/vhosts/virtualphoneline.com/httpdocs/includes/clone.php on line 230