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
Related
<?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>
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 am trying to code a file shredder in PHP, and I get these errors:
Warning: fopen(calculate): failed to open stream: Permission denied in C:\XAMPP\htdocs\shred.php on line 9
Warning: fwrite() expects parameter 1 to be resource, string given in C:\XAMPP\htdocs\shred.php on line 11
<?PHP
$files = glob("*");
foreach ($files as $files) {
$size = filesize($files);
$bytes = "1";
$writes = "1";
while ($writes <= "3") {
$data = fopen($files, "w");
while ($bytes <= $size) {
fwrite($files, "0");
$bytes = $bytes + 1;
}
fclose($data);
$writes = $writes + 1;
}
// unlink($files);
}
?>
I have no idea what to do at this point. The files aren't read only.
You should use $data as the first parameter of fwrite.
Make sure you have the appropriate permissions because that is the common problem in using
fopen()
you can see the the same errors with this one.
I am a new PHP developer and I just started working with files in PHP.
I have the following code to count number of txt files in the directory and store their names in an array and then using a loop display the total lines in each of the files!
here is the code, help me where I have gone wrong!
$dir = opendir('directory/');
$num_files = 0;
$dir_files[] = array();
while (false !== ($file = readdir($dir))){
if (!in_array($file, array('.', '..','Thumbs.db')) and !is_dir($file)){
$num_files++;
echo $file;
array_push($dir_files,$file);
echo "<br />";
}
}
echo "--------------------------------------<br />";
echo "Number of files in this directory: ".$num_files."<br />";
echo "--------------------------------------<br />";
foreach($dir_files as $dir_file=>$value){
$file='directory/'.$value;
$linecount = 0;
$handle = fopen($file, "r");
while(!feof($handle)){
$line = fgets($handle);
$linecount++;
}
fclose($handle);
echo "File $file has $linecount lines!";
}
I get the following errors:
Notice: Array to string conversion in D:\xampp\htdocs\PHP_practice\read_lines_of_files.php on line 19
Warning: fopen(directory/Array): failed to open stream: No such file or directory in D:\xampp\htdocs\PHP_practice\read_lines_of_files.php on line 21
Warning: feof() expects parameter 1 to be resource, boolean given in D:\xampp\htdocs\PHP_practice\read_lines_of_files.php on line 22
Your code is toooooooo lengthy. Try this : This will do whole functionality for you, let me know if any issues.
foreach(glob('directory/*.txt',GLOB_BRACE) as $value){
$file =$value;
$linecount = 0;
$handle = fopen($file, "r");
while(!feof($handle)){
$line = fgets($handle);
$linecount++;
}
fclose($handle);
echo "File $file has $linecount lines!";
}
change:
$dir_files[] = array();
to
$dir_files = array();
And:
fopen() returns a file pointer resource on success, or FALSE on error.As it is throwing an error opening the file, feof() is receiving FALSE instead of a file pointer resource: so you get the error "expects parameter 1 to be resource, boolean given in"...