I am trying to download a file from a remote website using cronjob and save it on my hosting. For this purpose I am using the following function but unable to achieve the desired results. When i run the function no file is downloaded.
function save_image($inPath,$outDir, $outPath) {
$in = fopen($inPath, "rb");
if (!is_dir($outDir)) { mkdir($outDir); }
$out = fopen($outPath, "wb");
while ($chunk = fread($in,8192)) {
fwrite($out, $chunk, 8192);
}
fclose($in);
fclose($out);
}
save_image($dataurl,"data/","data/$filename");
You need to download the file and save it.fopen can be used only for opening the local file.if you want to download and save the remove file,you can use php curl api.
you refer the following link for downloading and saving the remote file.
Download Remote File to Server with PHP
Related
I want to open a ZIP file by passing a remote URL (http://www.example.com/file.zip or http://localhost/wordpress/wp-content/uploads/file.zip) instead of a file location (C:\wamp\www\wordpress\wp-content\uploads\file.zip)
This constructor works fine for a file location but not for a remote url of a file. How does one open a file using a remote URL for this scenario?
public function __construct($file = false)
{
if ($file && is_file($file)) {
//$file="C:\wamp\www\wordpress\wp-content\uploads\file.zip" here
$this->open($file);
$this->fileName = basename($this->filePath = $file);
} else {
throw new \Exception($file . " not a regular file");
}
}
The safest way is to
download the file
This is super easy if allow_url_fopen is enabled: file_get_contents() accepts remote URLs. If that's not enabled, use cURL or a Wordpress HTTP helper to download it.
save it locally
Also super easy, with file_put_contents(). The /tmp folder is probably writable for you. On Windows, I don't know where the tmp folder lives.
open it like any other
As you would a local ZIP archive, with ZipArchive::open() or your nameless class
just use php fopen function
http://php.net/manual/en/function.fopen.php
$handle = fopen("http://www.example.com/", "r");
I have used this to get the contents of a webpage but not a zip file - not sure if that will work but it did well for me. $contents definitely worked for text.
// For PHP 5 and up
$handle = fopen("https://www.thesiteyouwant.com/the_target_file.ext", "r");
$contents = stream_get_contents($handle);
http://php.net/manual/en/function.stream-get-contents.php
There is a free ftp space xxx.yyy.zzz , i upload the code (named getweb.php) into it with filezilla.
<?php
$word=file_get_contents('http://www.webkaka.com/');
$filename='c:\\file.txt';
$fh=fopen($filename,"w");
echo fwrite($fh,$word);
fclose($fh);
?>
when i inpu xxx.yyy.zzz\getweb.php, i found that there is a file named c:\file.txt in my ftp space,i had to download it with filezilla.
I can do that with python ,how can capture the web pages directly into my local disk in php code ?
,
You must install PHP on your local computer if you want PHP code to be able to create files locally. You could try a ready-to-run XAMP package if you don't want to install native applications.
If you're using php 5, you can use file_put_contents:
file_put_contents('/path/to/file.txt', $word);
Code to download a file using PHP's built-in FTP streams
// the file your trying to get
$file ="ftp://ftp_user:ftp_pass#domain.com/file.txt";
// get the file
$contents = file_get_contents($file);
Code to download a file using PHP's built-in HTTP streams
// the file your trying to get
$file ="http://domain.com/file.txt";
// get the file
$contents = file_get_contents($file);
Code to UPLOAD (put) file on remote FTP
// get the file from LOCAL HARD DRIVE
$contents = file_get_contents('C:/local/path/to/file.txt');
// the file your trying to UPLOAD
$file ="ftp://ftp_user:ftp_pass#domain.com/file.ext";
// write USING FTP
$opts = array('ftp' => array('overwrite' => true));
$context = stream_context_create($opts);
file_put_contents($file, $contents, NULL, $context);
OR COMBINE A FEW OF ABOVE: Download using FTP, upload using FTP
// the file your trying to get
$file ="ftp://ftp_user:ftp_pass#domain-ONE.com/file.txt";
// get the file USING FTP
$contents = file_get_contents($file);
// the file your trying to UPLOAD
$file ="ftp://ftp_user:ftp_pass#domain-TWO.com/file.ext";
// write USING FTP
$opts = array('ftp' => array('overwrite' => true));
$context = stream_context_create($opts);
file_put_contents($file, $contents, NULL, $context);
Download using HTTP, upload using FTP
// the file your trying to get
$file ="http://domain-ONE.com/file.txt";
// get the file USING HTTP
$contents = file_get_contents($file);
// the file your trying to UPLOAD
$file ="ftp://ftp_user:ftp_pass#domain-TWO.com/file.ext";
// write USING FTP
$opts = array('ftp' => array('overwrite' => true));
$context = stream_context_create($opts);
file_put_contents($file, $contents, NULL, $context);
This code can be ran remotely on a webserver, locally on a webserver, locally using php-cli, or remotely using php-cli over SSH.
Let me know your specific demands and i'll adjust my answer
I have a vb.net app that webrequests a PHP file which does this:
<?php
$msg = $_GET['w'];
$logfile= 'savedrv.idps';
$fp = fopen($logfile, "w");
fwrite($fp, $msg);
fclose($fp);
?>
I want to make a PHP file that will open the new file created "savedrv.idps" so I can read it in vb.net. This is what I tried:
<?php
$logfile= 'reg.idps';
$fp = fopen($logfile, "r");
fclose($fp);
?>
How can I accomplish this?
Most likely your IIS settings for this Virtual Directory forbid a file with this extension to be browsed to. I got 404.7 error attempting to open a file in IE when I browsed to the URL: http://localhost/mysite/myvbfile.vb Using your browser, try to open the same URL that your VB program is attempting to access. I anticipate that you will get the 404.7 error in the browser window too.
You have two approaches here:
Have your PHP script write the file to a location outside of IIS where your VB.Net program can access.
Modify the Request Filtering in IIS for your site so that this file can be browsed.
(screenshot) http://support.citrix.com/article/html/images/1CTX132655-1.gif
We have a bunch of linuix and windows servers.
On my windows desktop I can see all the shares.
Using PHP I'm attempting to write a file to a directory on a windows share using the UNC path.
//ServerShare/directory/file.txt
Using fwrite says it successfully wrote the file but the file never exists on the server.
Using opendir function says the directory isn't accessible.
This is the source pretty simple.
$file_name = "\\SERVER2\Share\CPI\data.txt";
if (!$handle = fopen($file_name, 'w')) {
echo "Cannot open file ($file_name)";
exit;
}
// Write $somecontent to our opened file.
$somecontent = "this is a test";
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($file_name)";
fclose($handle);
Any thoughts on what types of permissions need to be set to let a linux box write files to a windows box?
You should be mounting the fileshare to a local directory:
mount -f smbfs //user#server2/Share/CPI/Data.txt /media/share
Then access /media/share/Share/CPI/Data.txt from your PHP script.
PHP needs to authenticate to the share, even if it is public, and using fopen or opendir does not do this.
I have the following code:
$cachefile = "http://www.DOMAIN.com/users/{$user}.php";
$fp = fopen($cachefile, 'w'); // open the cache file "cache/home.html" for writing
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file
fclose($fp); // close the file
ob_end_clean(); // Send the output to the browser
chmod($cachefile, 0644);
The file path is not valid, as it produces this error: Warning: chmod() [function.chmod]: No such file or directory in ...
However, this code does:
$cachefile = "{$user}.php";
$fp = fopen($cachefile, 'w'); // open the cache file "cache/home.html" for writing
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file
fclose($fp); // close the file
ob_end_clean(); // Send the output to the browser
chmod($cachefile, 0644);
The file--using the second block of code--is created in the same folder as the script that created it. However, I would like to save it to the first block's location. I look on php.net for fopen and I didn't see that I was doing anything wrong, but obviously I am.
EDIT (comment response):
I also tried $cachefile = $_SERVER["DOCUMENT_ROOT"] . "/users/{$user}.php" to no avail.
You cannot write to a remote file using fwrite(). If you're writing to a file that is on your own server, use a relative path.
You need to use the full path to the file on the server, not the web address:
$cachefile = "/var/www/path/to/users/{$user}.php";
You cannot open remote files, you can only edit files on the local filesystem, if the file is on the file system, then you have to use the relative path,
Also instead of using fopen, you could just use file_get_contents() and file_put_contents().