I have a problem with copying files to local server. I've got only the last file, plashka_3.png.
Warning: copy(http://rus-yurist.ru/images/plashka_1.png )
[function.copy]: failed to open stream: HTTP request failed!
Warning: copy(http://rus-yurist.ru/images/plashka_2.png )
[function.copy]: failed to open stream: HTTP request failed!
$txt="http://rus-yurist.ru/images/plashka_1.png
http://rus-yurist.ru/images/plashka_2.png
http://rus-yurist.ru/images/plashka_3.png";
$a1=explode("\n",$txt);
$a1=array_unique($a1);
foreach($a1 as $url) {
$ch = curl_init($url);
$path_parts = pathinfo($url);
$path = 'docs/'.$path_parts['basename'];
copy($url, $path);
// file_put_contents($path, file_get_contents($url)); /* not works too, only last file
}
I always use file_put_contents
file_put_contents("Tmpfile.zip", fopen("http://someurl/file.zip", 'r'));
I should trim my $url variable! Thanks to jeroen!
Related
I am trying to auto sync my remote database with my local machine by using php file_get_contents and connecting to remote server with ftp_connect frequent intervals. I am using windows Task Scheduler from my local machine for that.
My php script works perfectly from the browser. But when Task Scheduler Triggers it, the command prompt window comes up showing error messages.
My script is
<?php
set_time_limit(0);
include('functions.php');
$root=$_SERVER['DOCUMENT_ROOT'];
$sitearr=array("indianchristians","mc","evaa","laveena","aita","kaazar","landshop");
$date=Date('Y-m-d H:i:s');
$vals="";
$msg="";
foreach($sitearr as $sa)
{
include('connnew.php');
if($sa=="landshop")
{
$remurl="dwggxtwa_landshoppe.sql";
$localurl="$root/$folder/Myadmin/download/sqlbackup/";
$backupserv=file_get_contents($url."/Myadmin/download/sqlbackup/backupserver.php");
}
else{
$remurl="$db.sql";
$localurl="$root/$folder/admin/download/sqlbackup/";
$backupserv=file_get_contents($url."/admin/download/sqlbackup/backupserver.php");
}
$savedb=$db.".sql";
$localdb=$localurl.$savedb;
// echo $localdb."<br>";
$conn = ftp_connect($ftp_server);
$login_result = ftp_login($conn, $ftp_user_name, $ftp_user_pass);
ftp_pasv($conn, true);
if($sa=="landshop"){
ftp_chdir($conn, '/public_html/Myadmin/download/sqlbackup/');
}else{
ftp_chdir($conn, '/admin/download/sqlbackup/');
}
if (ftp_get($conn, $localdb, $remurl, FTP_BINARY))
{
// echo "$sa saved locally<br>";
$query=$maindb->query("SHOW TABLES") or die("SHOW TABLES ERROR ".$maindb->error);
while($st=$query->fetch_array()){
$drop=$maindb->query("DROP TABLE $db.$st[0]");
//if($drop){echo "<br>$db.$st[0] DROPPED !<br>";}else{echo "<br>$db.$st[0] NOT DROPPED !
<br>";}
}
$templine = '';
// Read in entire file
$lines = file($localdb);
// Loop through each line
foreach ($lines as $line)
{
// Skip it if it's a comment
if (substr($line, 0, 2) == '--' || $line == '')
continue;
// Add this line to the current segment
$templine .= $line;
// If it has a semicolon at the end, it's the end of the query
if (substr(trim($line), -1, 1) == ';')
{
// Perform the query
if(!empty($templine)){
$maindb->query($templine) or die('Error performing query
\'<strong>' . $templine . '\': ' . $maindb->error . '<br /><br />');
}
// Reset temp variable to empty
$templine = '';
}
}
ftp_close($conn);
$vals.="('$date','$sa'),";
$msg .="Updated $sa<br>";
}
}
if(!empty($vals)){
$vals=rtrim($vals,",");
$date=Date('Y-m-d H:i:s');
$link = mysqli_connect('127.0.0.1','admin','admin') or die ("Error connecting to
mysql : " . mysqli_connect_errno($link).'\r\n');
$connect=new mysqli('127.0.0.1','admin','admin','admin') or die("Database error
:".$connect->error);
$checktab=$connect->query("SELECT * FROM backuptimeline");
if(!$checktab)
{
$createsql = "CREATE TABLE backuptimeline
(
id int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
Date datetime,
site varchar(500)
)";
$createtab=$connect->query($createsql) or die($connect->error);
}
$butlsql="INSERT INTO backuptimeline (Date,site) VALUES $vals";
// echo $butlsql."<br>";
$insbu=$connect->query($butlsql)or die($connect->error);
if($insbu){
//sendEmailAlerts("anitgopinath#gmail.com","anitgopinath#gmail.com","DB
$sa Backed Up","All DB $sa backed up at $date");
$msg.="<br>Timeline Updated<br>";
}
else{
$msg.= "<br>Timeline Not Updated<br>";
}
echo $msg."<br>";
sendEmailAlerts("anitgopinath#gmail.com","anitgopinath#gmail.com","DBs Back Up
Results",$msg);
}else{
echo $msg."<br>";
sendEmailAlerts("anitgopinath#gmail.com","anitgopinath#gmail.com","DBs Back Up
Failed",'DBs Back Up Failed');
}
?>
Task Scheduler entries;
Program / Script in Action field:
"C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\binaries\php\php_runningversion\php.exe"
Arguments field
"C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\Admin\download\autobackup_task_scheduler.php"
Some error Messages
Warning: ftp_get(/matrimony/admin/download/sqlbackup/rotiraa1_mc.sql): failed to open stream: No such file or directory in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\Admin\download\autobackup_task_scheduler.php on line 35 PHP Warning: ftp_get(): Error opening /matrimony/admin/download/sqlbackup/rotiraa1_mc.sql in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\Admin\download\autobackup_task_scheduler.php on line 35
Warning: ftp_get(): Error opening
/matrimony/admin/download/sqlbackup/rotiraa1_mc.sql in C:\Program
Files
(x86)\EasyPHP-DevServer-14.1VC9\data\localweb\Admin\download\autobackup_task_scheduler.php
on line 35 PHP Warning: file_get_contents(): SSL operation failed
with code 1. OpenSSL Error messages: error:1407742E:SSL
routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version in
C:\Program Files
(x86)\EasyPHP-DevServer-14.1VC9\data\localweb\Admin\download\autobackup_task_scheduler.php
on line 21
Warning: file_get_contents(): SSL operation failed with code 1.
OpenSSL Error messages: error:1407742E:SSL
routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version in
C:\Program Files
(x86)\EasyPHP-DevServer-14.1VC9\data\localweb\Admin\download\autobackup_task_scheduler.php
on line 21 PHP Warning: file_get_contents(): Failed to enable crypto
in C:\Program Files
(x86)\EasyPHP-DevServer-14.1VC9\data\localweb\Admin\download\autobackup_task_scheduler.php
on line 21
Warning: file_get_contents(): Failed to enable crypto in C:\Program
Files
(x86)\EasyPHP-DevServer-14.1VC9\data\localweb\Admin\download\autobackup_task_scheduler.php
on line 21 PHP Warning:
file_get_contents(https://www.evaa-ethnix.com/admin/download/sqlbackup/backupserver.php):
failed to open stream: operation failed in C:\Program Files
(x86)\EasyPHP-DevServer-14.1VC9\data\localweb\Admin\download\autobackup_task_scheduler.php
on line 21
Warning:
file_get_contents(https://www.evaa-ethnix.com/admin/download/sqlbackup/backupserver.php):
f
I have tried using a batch file (.bat) too. I put it in system32 folder and tried. Still same errors appear.
I am not able identify whether it is a path issue or something else. I have been breaking my for a long time on this.
Any help will be greatly appreciated.
Finally after much googling and brainstorming I found I have two issues:
PHP file_get_contents was creating the SSL error. So I replaced it with Curl as follows;
function file_get_contents_curl( $url ) {
$ch = curl_init();
curl_setopt( $ch, CURLOPT_AUTOREFERER, TRUE );
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, TRUE );
$data = curl_exec( $ch );
curl_close( $ch );
return $data;
}
The path local url path ftp_get() began with a variable $root which had
$root=$_SERVER['DOCUMENT_ROOT'];
This was not seen by the Windows Task Scheduler. So I simply replaced $root with the complete path
C:/Program Files (x86)/EasyPHP-DevServer-14.1VC9/data/localweb/$folder
All my problems disappeared ! My system is auto syncing my remote database to my local database on set schedules. I am a happy man now ! :)
I am making a web application that grabs one page from the internet using cUrl and updates another page accordingly. I have been doing this by saving the HTML from cUrl and then parsing it on the other page. The issue is: I can't figure out what permissions I should use for the text file. I don't have it saved in my /public/ html folder, since I don't want any of the website's users to be able to see it. I only want them to be able to see the way it's parsed on the site.
Here is the cUrl code:
$perfidlist = "" ;
$sourcefile = "../templates/textfilefromsite.txt";
$trackerfile = "../templates/trackerfile.txt";
//CURL REQUEST 1 OF 2
$ch = curl_init("http://www.website.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
<more cUrl options omitted>
ob_start();
$curl2 = curl_exec($ch);
ob_end_clean();
curl_close($ch);
//WRITING FILES
$out = fopen($sourcefile, "r");
$oldfiletext = fread($out, filesize($sourcefile));
fclose($out);
$runcode = 1 ;
And the part where I save the text file:
/*only writing a file if the site has changed*/
if (strcmp($oldfiletext, $curl2) !==0)
{
$out = fopen($sourcefile, "w");
fwrite($out, $curl2);
fclose($out);
$tracker = fopen($trackerfile, "a+");
fwrite($tracker, date('Y/m/d H:i:s')."\n");
fclose($tracker);
$runcode = 1 ;
}
I am receiving an error at that last '$out = fopen($sourcefile, "w");' part that says:
Warning: fopen(../templates/textfilefromsite.txt): failed to open stream: Permission denied in /usr/share/nginx/templates/homedir.php on line 72
Any ideas?
The issue was with file/folder permissions. I ended up changing the permissions for the file to '666' meaning '-rw-rw-rw-' and it worked.
I'm trying:
-to connect to the server
-to list all the files
-to read the files and echo the content.
The connection work well:
$conn_id = ssh2_connect('xxx.com', 22);
$login_result = ssh2_auth_password($conn_id, 'xxxxxx', 'xxxxxxxxx');
$sftp = ssh2_sftp($conn_id);
$dir = "ssh2.sftp://$sftp/home";
$ftp_contents = scanFilesystem($dir);
Then I loop over the directory and get all the filenames, work like a charm:
foreach($ftp_contents as $key => $currentFilename){
echo($currentFilename);
}
Then I try to get the content of the file.... And it doesn't work.
$stream = fopen("ssh2.sftp://$sftp/home/".$currentFilename, 'r');
echo($stream);
There is the output error:
Warning: fopen(ssh2.sftp://Resource id #4/home/myfile.xml) [function.fopen]: failed to open stream: operation failed in /home/xxxxxx/public_html/xxx.php on line 38
The line 38 is the end of the foreach.
I try with:
fopen("ssh2.sftp://$sftp/home/".$currentFilename, 'r');
fopen("ssh2.sftp://$sftp/".$currentFilename, 'r');
fopen("/home/".$currentFilename, 'r');
fopen("home/".$currentFilename, 'r');
Nothing work, always the same type of error, can some one help me please?
Thanks.
update: I try with : $stream = file_get_contents("ssh2.sftp://$sftp/home/$data"); does'nt work either ...
Still got the error :
Warning: file_get_contents() [function.file-get-contents]: Unable to open ssh2.sftp://Resource id #3/xxx.xml on remote host in /home/xxxxxx/public_html/xxxx.php on line 40
I don't have a clue ... can some one help me?
I had the same problem. Resolved by converting the $sftp resource to an integer before using it as a url, which is a string.
Should work like that:
$sftp = (int)$sftp;
$stream = fopen("ssh2.sftp://$sftp/home/".$currentFilename, 'r');
Warning: file_get_contents(http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast) [function.file-get-contents]: failed to open stream: HTTP request failed! in /home/content/93/11389393/html/white/bet_gg.php on line 286
$grabPrice = file_get_contents('http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast');
$data = json_decode($grabPrice);
$current_price = $data->data->last_local->value;
Check if allow_url_fopen is enabled in PHP
If not - use cURL to grab some page by URL - works great
check if allow_url_fopen is enabled
phpinfo();
or
if( ini_get('allow_url_fopen') ) {
echo "Enable";
} else{
echo "Not enable";
}
if no, in your php.ini change
allow_url_fopen =0;
to
allow_url_fopen =1;
In this code :
$path = "C:\NucServ\www\vv\static\arrays\news.php";
$fp = fopen($path, "w");
if(fwrite($fp=fopen($path,"w"),$text))
{
echo "ok";
}
fclose($fp);
I have this error message:
failed to open stream: Invalid argument
What is wrong in my code?
Your backslashes is converted into special chars by PHP. For instance, ...arrays\news.php gets turned into
...arrays
ews.php
You should escape them like this:
$path = "C:\\NucServ\\www\\vv\\static\\arrays\\news.php";
Or use singles, like this:
$path = 'C:\NucServ\www\vv\static\arrays\news.php';
Also, your if is messed up. You shouldn't fopen the file again. Just use your $fp which you already have.
path error:
$path = 'C:/NucServ/www/vv/static/arrays/news.php';
file lock:
user file_get_contents replace fopen