file_put_contents return URL after execution - php

I have a set of functions like this:
<?php
//Get the base-64 string from data
$filteredData=substr($_POST['img_val'], strpos($_POST['img_val'], ",")+1);
//Decode the string
$unencodedData=base64_decode($filteredData);
//Save the image
file_put_contents('img.png', $unencodedData);
?>
This saves a file called img.png to the server. Now I need the last file_put_contents function to return the path/absolute URL of the file it just created. I can't seem to find an option for this in the php documentation.
Is there an option for this or alternatively any other method for returning the path/absolute URL?
Thanks

Use:
$abs_path = __DIR__.'/img.png';
file_put_contents($abs_path, $unencodedData);
echo $abs_path;

If you're writing into the same folder as the script, as in your example:
dirname($_SERVER['PHP_SELF']) . '/' . 'img.png'

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);

pdf file delete not working in php codeigniter using unlink

i want to delete my pdf file from server. my controller function looks like
function delete_pdf()
{
$id = (isset($_GET['id']) && $_GET['id']!='')?$_GET['id']:'1';
$user_email = $this->session->userdata('user_email');
$file = site_url('pdf files/'.$user_email.'/pdf #'. $id.'.pdf');
unlink($file);
}
when i echo $file;, it gives url http://localhost/my_site/pdf files/developer_team#gmail.com/pdf #4.pdf but the function not working to delete the pdf file.
I would appreciate for any help where i can delete my pdf files from server. thank you.
we can't delete file using URL. we  need absolute path. try this-:
$file = FCPATH.'pdf files/'.$user_email.'/pdf #'. $id.'.pdf';
try to remove space in pdf #4.pdf in your url
http://localhost/my_site/pdf files/developer_team#gmail.com/pdf #4.pdf
You need the absolute path to the file, I mean something like this
/Users/me/..../my_sites/pdf
The path depends of where is your controller. I don't know how codeigniter works.
EDIT
$file = dirname(__FILE__). DIRECTORY_SEPARATOR .'..'. DIRECTORY_SEPARATOR .'..'. DIRECTORY_SEPARATOR .'pdf files/'.$user_email.'/pdf #'. $id.'.pdf';
It will give you this :
C:\xampp\htdocs\my_site\application\controllers\..\..\pdf files\developer_team#gmail.com\pdf #4.pdf

PHP Input & file_get_contents

Can someone explain me why when i POST RAW Data for example "test.txt" in the below script
<?php
echo file_get_contents("php://input");
?>
it only prints the text "test.txt" instead of the file contents of that file?
Thank you
Your code reads the contents of the raw POST data and echoes it back.
Whereas what you want is this:
// retrieve the requested filename
$fileName = file_get_contents("php://input");
// echo the contents of the requested file
echo file_get_contents($fileName);
Depending on what you're trying to, you may wish to sanitize the $fileName input (not shown: too broad) and restrict access to a specific local directory:
$path = $myLocalDirectory . DIRECTORY_SEPARATOR . $fileName;
if (file_exists($path) {
echo file_get_conents($path);
}
Try like this ..
$input = "abc.txt";
echo file_get_contents($input);
It gives the content of the text file abc.txt

PHP Data-URI to file

I have a data URI I am getting from javascript and trying to save via php. I use the following code which gives a apparently corrupt image file:
$data = $_POST['logoImage'];
$uri = substr($data,strpos($data,",")+1);
file_put_contents($_POST['logoFilename'], base64_decode($uri));
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs 9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAxklEQVQYlYWQMW7CUBBE33yITYUUmwbOkBtEcgUlTa7COXIVV5RUkXKC5AxU EdyZVD4kyKxkwIrr9vd0c7Oih aopinLNsF6Qkg2XW4XJ7LGFsAAcTV6lF5/jLdbALA9XDAXYfthFQVx OrmqKYK88/7rbbMFksALieTnzu9wDYTj6f70PKsp2kwAiSvjXNcvkWpAfNZkzWa/5a9yT7fdoX7rrB7hYh2fXo9HdjPYQZu3MIU8bYIlW20y0RUlXG2Kpv/vfwLxhTaSQwWqwhAAAAAElFTkSuQmCC
Below the code is the actual image as a Data-URI. 'logoImage' is the string above, and $uri is the string minus 'image/jpeg;base64,'.
A quick look at the PHP manual yields the following:
If you want to save data that is derived from a Javascript
canvas.toDataURL() function, you have to convert blanks into plusses.
If you do not do that, the decoded data is corrupted:
$encodedData = str_replace(' ','+',$encodedData);
$decodedData = base64_decode($encodedData);
The data URI you have in your example is not a valid PNG image. This will never work and is unrelated to the code, it's related to the data.
Does not apply but might be of interest:
file_put_contents($_POST['logoFilename'], file_get_contents($data));
The idea behind: PHP itself can read the contents of data URIs (data://) so you don't need to decode it on your own.
Note that the official data URI scheme (ref: The "data" URL scheme RFC 2397) does not include a double slash ("//") after the colon (":"). PHP supports with or without the two slashes.
# RFC 2397 conform
$binary = file_get_contents($uri);
# with two slashes
$uriPhp = 'data://' . substr($uri, 5);
$binary = file_get_contents($uriPhp);
The all code that works :
$imgData = str_replace(' ','+',$_POST['image']);
$imgData = substr($imgData,strpos($imgData,",")+1);
$imgData = base64_decode($imgData);
// Path where the image is going to be saved
$filePath = $_SERVER['DOCUMENT_ROOT']. '/ima/temp2.png';
// Write $imgData into the image file
$file = fopen($filePath, 'w');
fwrite($file, $imgData);
fclose($file);
I have another way to do this with PHP.
$img = str_replace(' ','+',$img);
$i = explode(',', $img);
$imgData = array_pop($i);
$newName = 'digital_file/'. rand(10, 16) . '.' . str_replace('/', '.', mime_content_type($img) );
// data:image/png;base64
$imgData = base64_decode($imgData);
Now you can use file_put_contents($newName) to create the image file.
Produces a file with a random numerical name (e.g. "123123.image.png"). And of course it has correct mime type.

using file_get_contents twice with a variable

i am trying to use this bit of code to first retrieve a URL that is stored in a txt file on my server and save it as a variable, then run file_get_contents a second time using the URL i just retrieved and saved as a variable.
the code works for the first file_get_contents and echoes the URL that is stored, but fails to then use that URL in the second file_get_contents to echo the contents of the URL.
<?php
$files = file_get_contents('http://example.com/txtfile.txt');
echo $files;
$file = file_get_contents($files);
echo $file;
?>
Well the direct solution to your problem is:
<?php
$files = file_get_contents('http://example.com/txtfile.txt');
echo $files;
$files = trim($files);
$file = file_get_contents($files);
echo $file;
?>
But this is huge security risk. Running file_get_contents to open a variable file is risky.

Categories