I need PHP code to download file from one server to another. It's defined by variable $HasPrevod. That's the file which I need to download. Also there's simple form where I put link and the output is that variable $HasPrevod, so I had to put it in a href to get link easily to download.
Download. and download it manually and upload via Filezilla on my server. Is there solution which will download content from that variable and put it on my server in some folder. I tried with cURL but nothing happened because I'm newbie and I've made mistake for shure, and I don't know much about PHP and cURL.
So it should be something like this $HasPrevod file download -> my server / folder, and echo file name.
$prevod = $_POST['prevod'];
$url = file_get_contents("$prevod");
$PrevodLink = preg_match('!http://[a-z0-9\S\ \-\_\.\[\]\[\]\/]+\.(?:srt)!Ui', $url, $match1);
$HasPrevod = $match1['0'];
<form action="prevod.php" method="post">
<input name="prevod" type="text"/>
<input type="submit" value="Search"/>
</form>
Thats the code I'm using to put link and get path to the file which is located on another website not my own, then I have to download manually and upload on my server via Filezilla.
Download
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);
$destination = "prevodi/". $HasPrevod;
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);
file_put_contents('path/to/file.ext', file_get_contents($HasPrevod));
If you have enough permission, try with exec('wget http://www.example.com/myFile.zip');
If not, try with file_put_contents() method. At last if that now works, try to var_dump() some states of code to debug it.
Related
So I found this page: Load external XML and save it using PHP to help me out, but it doesn't seem to work for me.
I'm trying to do the same thing by loading an external xml file and saving it (with no changes to the xml file) into my website directories as a batch system. I have already dynamically created all the directories needed.
ex:
/xml/en/281
Now what I'm trying to do is load the company's xml files (https://thiscompany.com/xml/en/281/18511095_en.xml) and save it in my own directory as the same name, 18511095_en.xml in the 281 directory.
I've been researching and I am getting lots of simplexml_load_file and DOMDocument examples but I'm not getting the results I needed.
For all sake and purposes here is the code: (I'm changing the url of the actual xml file because my client doesn't want it out there.
EDITED from the responses below
$url = "https://thiscompany.com/xml/en/281/18511095_en.xml";
$timeout = 10;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
$response = curl_exec($curl);
file_put_contents(__DIR__ . "/xml/18511095_en.xml", $response);
I'm assuming the problem is with the saveXML path. The xml directory is at the root of my website. Do I need to include, http://www.....com?
xml, en, and 281 all have 0777 permissions.
Any help would be appreciated.
You need to provide the path on your filesystem, not a URL.
e.g. "/var/www/www.example.com/htdocs/xml/en/281/18511095_en.xml"
Here's a simple way to copy a remote file into the same directory as your PHP file:
$url = "http://www.test.com/xmlfile.xml";
$contents = file_get_contents($url);
file_put_contents(dirname(__FILE__) . "/xmlfile.xml", $contents);
You can also accomplish this without having to use simplexml_load_string which will use more memory on your server. Instead try the following:
$xml = file_get_contents("https://thiscompany.com/xml/en/281/18511095_en.xml");
file_put_contents("/var/www/my/full/data/path/filename.xml", $xml);
$xml = simplexml_load_string($response);
$xml->saveXML("/xml/en/281/18511095_en.xml");
First of all, you should use a relative path instead of an absolute one. In this example you are trying to save the file under the /xml folder on the root filesystem and there's most likely no such directory and you don't have permissions to write to that directory (assuming it exists). Use a relative path.
Secondly, you don't need to parse the XML file, you can save it directly. Here's a working example:
$response = curl_exec($curl);
file_put_contents(__DIR__ . "/xml/en/281/xmlfile.xml", $response);
Further reading: Absloute path vs relative path in Linux/Unix
I have files like pdf in a directory and i have created a search form where in the user will input the filecode and it will search the source directory if it matches a filename it will allow the user to download the file, if not it will show "No Results" Here's what i did, but I am not sure what code should I use to download a file from my source directory, I just saw the code for downloading a file somewhere and substitute the source file with the variable i use when a user search
<html>
<head>
<title>Search Contacts</title>
</head>
<body>
<h3>Search Client File</h3>
<form method="post" action="#" id="searchform">
Type the File Code:<br><br>
<input type="text" name="fcode">
<br>
<input type="submit" name="submit" value="Search">
</form>
<?php
$filecode=$_POST["fcode"];
if (!empty($filecode))
{
$ch = curl_init();
$source = "/sdir/$filecode.pdf";
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);
$destination = "/tdir/afile.pdf";
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);
}
else
{
echo "No Results";
}
?>
</body>
</html>
I think your issue is relating to the line $file="<sourcefile?>" as this doesn't appear (at least to me) to make sense. However, you will get a better idea of your issues if you make sure that PHP has error logging turned on and then look up your PHP error log file.
something like (At the top of your PHP page):
Sorry this code is older:
ini_set("log_errors", 1);
ini_set("error_log", "/tmp/php-error.log");
see: https://stackoverflow.com/a/3531852/3536236
Newer way of doing it:
ini_set("error_log", "/tmp/php-error.log");
error_reporting(E_ALL);
This will mean that you can then download and view the log file "/tmp/php-error.log" and this will explain why PHP failed, if it did indeed fail.
I'm sorry I can't give you a more exact answer but this will show you exactly what's wrong rather than a somewhat vague "it doesn't work".
How to download a file
http://www.media-division.com/the-right-way-to-handle-file-downloads-in-php/
https://stackoverflow.com/a/3320743/3536236
I think after following these two links you should have it working.
I want to open and write a txt file from another server, but I don't know how to do it? Can anyone help me?
<?PHP
$fname=$_POST["fname"];
$groupid=$_POST["groupid"];
$myfile = fopen("Ji.txt", "a+") or die("Unable to open file!");
fwrite($myfile, $fname."|".$groupid."\r\n");
fclose($myfile)
?>
I want to replace Ji.txt with http://xxx.xxx.xx.x/ddd.txt
You can read the files over HTTP by using fopen or cURL.
You can't write to files over HTTP unless the server you are writing to is set up to understand an appropriate request. You could configure it to support PUT requests (make sure you have some kind of authn/authz system in place!) and make one using cURL.
Alternatively, you could use some other protocol to make the file available between servers (such as NFS).
Alternatively you can do it with the FTP functions
<?php
// Connect to remote FTP server
$conn = ftp_connect("ftp.example.com")or die("Cant connect to ftp server");
$login = ftp_login($conn, "username", "password");
// Open local (temporary) file handle
$fh = fopen("Ji.txt", "a+");
// Get remote file and save it to the previous file handle
if(ftp_fget($conn, $fh, "Ji.txt", FTP_ASCII))
{
// Local file has now been updated with the content of the remote Ji.txt
$fname = $_POST['fname'];
$groupid = $_POST['groupid'];
fwrite($fh, $fname.'|'.$groupid.'\r\n');
if(ftp_fput($conn, "Ji.txt", $fh, FTP_ASCII))
echo 'File saved to remote server';
else
echo 'Error saving to remote server';
}
else
echo 'Error downloading remote file';
ftp_close($conn);
fclose($fh);
?>
Read more about ftp_fput etc here: http://php.net/manual/en/function.ftp-fput.php
As long as allow_url_fopen is set to True you can pass an URL to fopen() and read the data. Writing however, is impossible. However, you can write a wrapper around the file that accepts both GET & POSTS requests. A GET returns the contents of the file and a POST (or PUT) saves content to the files. The downside of this is that you must add security and it's a lot more complex to save the data.
Assuming you have access to the other server and the text file is available at a web address (ie you can see it in a browser) then you can use fopen to grab it, make your edits or whatever and then post back to a script that will handle the post data and save the file using curl. Example code below copied from this answer
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => '#/path/to/file.txt'));
curl_setopt($ch, CURLOPT_URL, 'http://server2/upload.php');
curl_exec($ch);
curl_close($ch);
Posting server should be something like this
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('ourfilename' =>'yourpath/test.txt'));
curl_setopt($ch, CURLOPT_URL, 'http://urlreceivepost/getfile.php');
curl_exec($ch);
curl_close($ch);
Then getfile.php at your above url
if ($_POST) {
$file= $_POST['ourfilename'];
$data = file_get_contents($file);
$new = 'test.txt';
file_put_contents($new, $data);
var_dump($new);}
My plan is to create php 'template' files which will create various reports for the users of the site on demand. What I want to do is get the output of the php file and save it as a new file on the server. The curl and file creation works, but ...
Problems and questions :
When using curl I no longer have access to any of the $_SESSION variables which are set for the user logged in. I need to be able to access these in my output.php file to actually create the correct content I want in the file. Is there a way to pass/allow access to the $_SESSION variables? If not, is there a way I can post data along with curl... such as posting the user ID or something along those lines?
Is the mode required when using recursive = true for mkdir?
Lastly... my original plan was to store these 'template' files outside of the public_html directory so there was no direct access to the files... only through my scripts although it appears curl can only save the output of the php file when given a url. With that said, it appears the files must be accessible via the web. Is there a way to give curl a filepath such as '/home/test/template/output.php instead and still process the file and save the actual output of it?
I am up for any suggestions that work EXCEPT file_get_contents as I have fopen disabled. Would using output_buffer instead be better in this situation?
example call to create a report :
get_archive('http://www.test.com/template/output.php', '/home/test/public_html/reports/', 'newreport.html');
function get_archive($file, $local_path, $newfilename)
{
// if location does not exist create it
if(!file_exists($local_path))
{
mkdir($local_path, 0755, true);
}
$out = fopen($local_path.$newfilename,"wb");
if ($out == false){
exit;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_URL, $file);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
}
simple example template file (output.php) : This would obviously be much more extensive, but you should get the idea.
<?php
// These files can be included only if INCLUDE_CHECK is defined
require '/home/test/public_html/custom/functions/connect.php';
require '/home/test/public_html/custom/functions/functions.php';
require '/home/test/public_html/custom/functions/session.php';
?>
<!DOCTYPE html>
<html>
<head></head>
<body>
<div class="row">
<div class="col-md-12">
<?php
//create report contents
echo $_SESSION['user']['account_id']; // returns nothing
?>
</div>
</div>
</body>
</html>
Hi i want to include an external .php file inside my php file.
this .php file has some variable declaration needed inside my page.
i tried using file_get_contents but it just echoes the lines inside.
tried:
function getter($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo getter('www.somehting.com/phpfile.php');
-still not working
also tried:
$code = file_get_contents($url);
eval($code);
is there anyway for me to do this without changing the config.ini?
I have access to .htaccess on the page where the file should be included.
the file is on a different server "rackspace".
Thank You.
You cannot include an external php file, unless the .php file is outputing the php source. Otherwise you will just get the interpreted php file from the external source.
The only thing you can do is output the .phps (source) so that you can access the actually code. Like I said above, you would be accessing the processed / interpreted php file, which would do you no good.
If you have access to the other server, you can try outputing the file.phps instead of file.php
If you load a file on an external server by web address, Apache will show the page after the php has been finsished.
So it will return
Hello world!
Instead of
<?php
echo 'Hello world!';
?>
You could, however, try to connect to the external server with ftp connection and retreive the file you want. This will require you to have administrator access to the server its hosting.