Cakephp how to browse file? - php

my question is how to get the content of a file from a input file
becausethe only thing im getting is the name of the file not the
full path of the file.
$handle = file_get_contents($this->data['btnBrowse']);
$absolute = basename($this->data['btnBrowse']);
var_dump($handle);
var_dump($absolute);

This should work:
$file = new File($dir);
$contents = $file->read();
$file->close();

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

how find file name in a server source using php

i have a link.when clicking on that link a pdf file will be downloaded. am trying to save that file to a folder. with static name it getting saved,but i want to save with its actual name,is that possible? if anybody knows please help me.
sample url:- http://www.intercoat.de/index.php?option=com_jdownloads&Itemid=206&task=finish&cid=341&catid=178&lang=en
am attaching my code below. i have an array of links like above sample link
so am fetching it from an array
foreach($li as $lm)
{
$i++;
$file_info = new finfo(FILEINFO_MIME_TYPE);
$mime_type = $file_info->buffer(file_get_contents($lm));
if($mime_type=='application/pdf')
{
echo $lm.$i.'<br>';
$filecontent = file_get_contents($lm);
file_put_contents('./uploads/myfile'.$i.'.pdf', $filecontent);
}
}
now all files not saving with its exact name,it saves as myfile1,myfile2 ..etc
cant take base name because the file name is not in url.that urls are just source of file. anybody knows please help
try this
foreach($li as $lm)
{
$i++;
$file_info = new finfo(FILEINFO_MIME_TYPE);
$mime_type = $file_info->buffer(file_get_contents($lm));
if($mime_type=='application/pdf')
{
echo $lm.$i.'<br>';
$filecontent = file_get_contents($lm);
$actual_name = basename($lm);
file_put_contents('./uploads/'.$actual_name', $filecontent);
}
}

How to save file in subfolder using php

I have a script with a mysql query which saves a file called invoice.xml every day automatically by running a cron job. In case no data is found a no_orders.txt is saved.
I would like this file not be saved to the same folder as the script.php file is in but to a subfolder called invoices.
The renaming of the old invoice.xml is done with the following code
// rename old file
$nowshort = date("Y-m-d");
if(file_exists('invoice.xml')) {
rename('invoice.xml','invoice_'.$nowshort.'.xml');
}
The saving is done with the following code:
if($xml1 !='') {
$File = "invoice.xml";
$Handle = fopen($File, 'w');
fwrite($Handle, $xml1);
print "Data Written - ".$nowMysql;
fclose($Handle);
#print $xml;
die();
} else {
print "No new orders - ".$nowMysql;
$File = "no_orders_".$nowshort.".txt";
$Handle = fopen($File, 'w');
fclose($Handle);
die();
}
Could I please get assistance how to save this file to a subfolder. Also the renaming of the existing file would need to be within the subfolder then. I have already tried with possibilities like ../invoice/invoice.xml but unfortunately without any success.
Thank you
Just give the path of file 'invoice.xml' to $File.
Otherwise create some $Dir object which will point to Folder named 'invoice', then use accordingly
Use __DIR__ magic constant to retrieve your script.php directory, then you can append /invoice/invoice.xml .
Example if path to your script php something like this:
/var/www/path/to/script.php
$currentDir = __DIR__; //this wil return /var/www/path/to
$invoicePath = $currentDir.'/invoice/invoice.xml';

Open a file then edit and save file using codeigniter

I want to edit a .ini using codeigniter.
For Now i can open file edit that file but when i save it the file is saved as a POINTER : RESOURCE ID #5.
I want to create a new file and i am setting a custom name to that file.
But my code create the file but the file is empty and data i want to write is written in POINTER : RESOURCE ID #5.
What should i be doing?
This is my code:
$this->data['parameters'] = parse_ini_file($path.$filename,true);
while (current($this->data['parameters']) )
{
$param_set = current($this->data['parameters']);
$param_type = key($this->data['parameters']);
foreach ($param_set as $key =>$value)
{
$this->data['parameters'][$param_type][$key] = $this->input->post($key);
}
next($this->data['parameters']);
}
$this->load->helper('file');
$this->load->library('ini');
$name = $this->session->userdata('username');
$ext = $this->session->userdata('extension');
/*Want to create a new file*/
$custom_file = fopen('uploads/'.$name.'_'.$ext.'.ini', 'w');
/* $file returns a file pointer which instead should be the file i just created above*/
$file = $path.$custom_file;
$ini = new INI($file);
$ini->write($file, $this->data['parameters']);
So how to write in the file i created instead of pointer file?

Symfony UploadedFile not working

I am trying to create a temporary file , Fill it with data and then create a UpladedFile for this temp file.
Here goes my code.
$encoded_data = "This is a huge string";
$filename = "tempMaxFile";//$meta_data["uri"];
$handle = fopen($_SERVER['DOCUMENT_ROOT'].$filename, "a+");
file_put_contents($_SERVER['DOCUMENT_ROOT'].$filename, $encoded_data);
$file = new UploadedFile($_SERVER['DOCUMENT_ROOT'].$filename,$filename);
var_dump($file->getClientSize());
die;
But it prints null where it should be printing the size of the file.
And I can see the file in my folder with the data in it!
If you look at the code for UploadedFile you will see that it does not calculate this size, it expects you to pass it to the constructor

Categories