How to access the file in another folder file_get_contents() - php

This file is runing
my-site.com/includes/classes/build/cg.php
and it loads
my-site.com/data/data.json
with a such structure
$data = json_decode(file_get_contents("http://my-site.com/data/data.json"), true);
I want to load it with relevant path, how to do it?

After trying some combinations, realised this:
$data = json_decode(file_get_contents(__DIR__ ."/../../../data/data.json"), true);
or
$data = json_decode(file_get_contents("././././data/data.json"), true);

Related

get php file execution result

i'm new in php. and using php on xampp and also in real server. i have a php file that receives on image as String and saves it as image that im gonna use this file with a library in android that uploads image to php file.
the string is sent to php file but no file is saved as image. my problem is that i cant figure out how to get result of executing this php file. i cant get response with my upload library , if i could get echo from this file for test purpose, so i could test it or if i could get error log of execution of file in xampp. but i have no clue how to test php file that is not containing view so i cant echo any thing.
this is my php file code:
<?php
if($_POST){
$data = $_POST['imgBase64'];
$data = str_replace('data:image/png;base64,', '', $data);
$data = str_replace(' ', '+', $data);
$data = base64_decode($data);
$file = ''.rand() . '.png';
$success = file_put_contents($file, $data);
$data = base64_decode($data);
$source_img = imagecreatefromstring($data);
$rotated_img = imagerotate($source_img, 90, 0);
$file = 'localhost/serverp/server.parhamcode.ir/'. rand(). '.png';
$imageSave = imagejpeg($rotated_img, $file, 10);
imagedestroy($source_img);
}
?>
try this:
<?php
file_put_contents('./debug.log', $_POST, FILE_APPEND);
then you'll get a debug.log file under the same folder as your PHP script.
you can change $_POST to any variable you want to check.
If you want to echo something in this log file :
file_put_contents('./debug.log', "any string is ok.", FILE_APPEND);

combine many json files into one

I am trying to combine all json files into one.However i always receive an empty json file . Here is code ;
function mergejson()
{
$events = array();
// open each jsonfile in this directory
foreach(glob("*.json") as $filename) {
// get the contents of the the current file
$data[] =json_decode($filename, true);
$events= array_merge($events,$data);
}
$file_name ='merge.json';
$events =json_encode($events,true);
file_put_contents($file_name,$events);
}
The function json_decode takes a string as first argument, not a filename!
So you have to load the file content, try using file_get_contents

php created in the root directory

I use account
dev_hq
I want to create a folder in the root called medias.
I use the command
$medias = 'medias';
$path = './root/'.$medias;
if(!is_dir($path)){mkdir($path, 0777, true); }
but the directory is created in
/var/www/public/root/medias
=> I want to create folders in
/home/dev_hq/
and link
/home/dev_hq/root/medias
What are you are doing is absolutely wrong. Please try this code
$medias = 'medias';
$path = '../../root/'.$medias;
if(!is_dir($path)){mkdir($path, 0777, true); }
Hope this helps you

Move Upload File from PHP to CGI

Im trying to pass post file information to an upload.php file and have that information be sent to a CGI script. There is nothing on the net on how to go about doing this that i can find, iv spent days. I know there are a few people out there that need this, it could help us all that have legacy perl scripts.
Dataflow:
Jquery --> Upload.php --> index.cgi
My php:
<?php
if(isset($_FILES['file'])) {
if(move_uploaded_file($_FILES['file']['tmp_name'], "../index.cgi" . $_FILES['file']['name'])){
echo "success";
exit;
}
}
?>
Post call to CGI example:
foobar.com/index.cgi?act=store&data=$filename
Any suggestions would help greatly. Thank you.
From my understanding, your CGI script is receiving a parameter which is the path of the uploaded script. However you are attempting to pass the uploaded script to your CGI script using a function that is only supposed to move a file from 1 place to another without executing a script.
my suggestion is to do the following
<?php
if(isset($_FILES['file'])) {
$destination = "new/path/to/".$_FILES['file']['name'];
if(move_uploaded_file($_FILES['file']['tmp_name'], $destination)){
$data = array();
//You can add multiple post parameters here
//$data = array('param1' => 'value1', 'param2' => 'value2');
$url = "http://url/to/hello.cgi";
// You can POST a file by prefixing with an # (for <input type="file"> fields)
$data['file'] = '#'.$destination;
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($handle);
if($result) {
echo "success";
}
exit;
}
}
?>
You can execute the cgi script via a CURL POST and pass any params you want

How to download directly from one website to another

I have a piece of a PHP code:
<?php
$image_cdn = "http://ddragon.leagueoflegends.com/cdn/4.2.6/img/champion/";
$championsJson = "http://ddragon.leagueoflegends.com/cdn/4.2.6/data/en_GB/champion.json";
$ch = curl_init();`
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $championsJson);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$json = curl_exec($ch);
curl_close($ch);
$json_array = json_decode($json, true);
$champions = $json_array["data"];
foreach ($champions as $championdata) {
$image_url = $image_cdn.$championdata["image"]["full"];
$image = file_get_contents($image_url);
file_put_contents("imgfolder/".$championdata["image"]["full"], $image);
}
?>
So the idea of the code is to basically to decode a JSON and to download the images from the following website:
http://gameinfo.na.leagueoflegends.com/en/game-info/champions/
The pictures download perfectly fine and are stored into a folder I have created on my hard drive. The next step is, is it possible for me to use this same piece of code, to download/display the images onto a website I have recently created:
www.lolguides4you.co.uk
the website is basically a day old and I have literally been messing around with some HTML coding. I am new to both HTML and PHP so if someone could point me into the right direction, that would be great!
Assuming that all you want to do it insert the images into a page on your website, then this is quite simple.
However, it may be illegal for you to use an automated scraping tool to save/replicate/duplicate any of the images. Look into that before doing anything on the internet.
The first step is to upload your previous PHP script to your website. That way, rather than downloading the images to your computer and then trying to upload them to your site it allows you to just save the files straight into a directory on the website.
Next, you can create a basic PHP page anywhere on your site:
<?php
echo "Images downloaded:\n";
?>
Next you can use PHP's scandir() function to find every file in the download directory:
<?php
echo "Images downloaded:\n";
$files = scandir('imgfolder/');
foreach($files as $file) {
// Do something
}
?>
Finally, now you just show each image:
<?php
echo "Images downloaded:\n";
$files = scandir('imgfolder/');
foreach($files as $file) {
echo $file . "\n";
}
?>
You could also use Glob, which allows you to ignore any files which aren't a certain type (in case you have other files in the same directory:
<?php
echo "Images downloaded:\n";
$files = glob("imgfolder/*.jpg");
foreach($files as $file){
echo $file . "\n";
}
?>

Categories