I am using silm3 as my REST API framework and followed this to write my file upload script from API.
I am currently running my web app in a.xyz.com and my REST API is in b.xyz.com . Now I want to upload a picture into a.xyz.com/uploaded/ from b.xyz.com/upload.
$newfile->moveTo("/path/to/$uploadFileName");
is used to save the file in current subdomain. But I failed to upload it into another subdomain.
I search in the web, but didn't find any clue how to do it.
After moving the file on b.xyz.com in the correct position, do the following:
// Server B
// Move file to correct position on B
$newfile->moveTo("/path/to/$uploadFileName");
// Send file to A
$request = curl_init('a.xyz.com/upload');
curl_setopt($request, CURLOPT_POST, true);
curl_setopt($request, CURLOPT_POSTFIELDS, [
'file' => new CURLFile("/path/to/$uploadFileName")
]);
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($request);
curl_close($request);
a.xyz.com should then accept the file and proceed to storing it. If using Slim, in the same way as b.xyz.com does
...
$newfile->moveTo("/path/to/$uploadFileName");
or using PHP's core functions
// Server A
if ( !empty($_FILES["file"]) && !empty($_FILES["file"]["tmp_name"]) ) {
// Move file to correct position on A
$uploadFileName = basename($_FILES["file"]["name"]);
move_uploaded_file($_FILES["file"]["tmp_name"], "/path/to/$uploadFileName");
}
Normally domains or subdomains are restricted to their own webspace, so you can't read or write other domain's files. The setting is calles open_basedir.
You have to set it in your vhost configuration but there are also other ways, like setting it with a .htacces file if your hoster allows it:
How can I relax PHP's open_basedir restriction?
Related
I have two servers so i want when i upload file/image from first server ( using move uploaded file function),
file should be upload to second server.for this i used following code
$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);
but i want to know that what code should be there in second servers "upload.php".And how can i mention path ?
first off, don't try to upload files with the # method, it was deprecated in PHP5.5, disabled-by-default in PHP5.6, and completely removed in PHP7.0. use CURLFile to upload files. to mention a path, just add the path as another post variable in the array. as for the receiver, read the handling file uploads in php article on php.net. finally, consider if you want to let anyone who finds the url to be able to upload any file or not. (probably not, you *probably* want to set a password on uploads), eg
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'file' => new CURLFile('path/to/file.jpg'),
'path' => '/where/ever/you/want.jpg',
'password' => 'd540cyLp419rdwelv8c-'
));
on the sender and
if (! hash_equals(hash('sha256', 'd540cyLp419rdwelv8c-', true), hash('sha256', (string) ($_POST['password'] ?? ''), true))) {
http_response_code(403); // forbidden
die("wrong/missing password.");
}
$upload_path = $_FILES["file"]["tmp_name"];
$requested_file_path = $_POST['path'];
on the receiver, again, see the aforementioned "handling file uploads in php" article to learn more.
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
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.
What is the best way using PHP and Curl to POST an entire folder to another server.
you can:
post all files in the directory consequently
zip the directory and post the archive
$srcdir = '/source/directory/';
$dh = opendir($srcdir);
$c = curl_init();
curl_setopt($c, ....); // set necesarry curl options to specify target url, etc...
while($file = readdir($dh)) {
if (!is_file($srcdir . $file)) {
continue; // skip non-files, like directories
}
curl_setopt($c, CURLOPT_POSTFIELDS, "file=#{$srcdir}{$file}");
curl_exec($c);
}
closedir($dh);
That'd be the basics. You'd want some error handling in there, to make sure the source file is readable, to make sure the upload succeeds, etc.. The full set of CURLOPT constants are documented here.