Symfony UploadedFile not working - php

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

Related

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

cakephp 3 lock file property

I have an application that create many php process to read file.
Sometimes process read the same file so I need to lock file when a process read It.
The application need to read only one times a file and after delete It.
Sometimes two process read the same file and this is a big problem.
I have create many process that every one loop through a folder, read all file, and delete it.
I don't want that two process read the same file, but the problem is that the process works simultaneously.
code of every process :
public function readAllXmlAndSave()
$xml_files = glob(TMP . 'xml/*.xml');
foreach($xml_files as $fileXml){
$explStr = explode('/', $fileXml);
$filename = $explStr[count($explStr) - 1];
$path = TMP . '/xml/' . $filename;
$file = new File($path, false);
if($file->exists()){
$string = $file->read();
//some work
$file->close();
$file->delete();
}
}
}
I have create a simple script to understand how lock file works into cakephp 3 but seems that property lock doesn't work as I aspected.
code
$path = TMP . '/xml/test.xml'; //inside there is "HELLO"
$file = new File($path, false);
$string = $file->read();
echo($string);
$file->lock = true;
$string2 = $file->read();
echo($string2);
$string3 = $file->read();
echo($string3);
I aspected that only the first echo work not the other.. Why I get always:
HELLO
HELLO
HELLO
and not only the first echo?
HELLO

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?

Cakephp how to browse file?

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

sugarCRM add document from script

I am trying to add Document to Sugar object (client) from PHP script. I have a directory of files (on the same server where sugarCRM is installed) and xls with sugar objec ID and filename). PHP Script should add correct filename to specific sugar object (identified with ID). I can read XLS this is no problem, I can also get instance of sugar object (retrieve by ID), but I have no idea how can I assign the file to sugar. I was trying with Document, and upload_file.php, but those seem to be usable with uploading single file with html Form.
How can I automate this task, copy files with correct filename to cache\upload and create Document related to my Customer from PHP Script? I would prefer not to use SOAP if it's not necesarry...
Edit:
I was able to save document and revision, but something is wrong, and file can't be downloaded from browser ("incorrect call to file")
My Code so far is:
require_once('include/upload_file.php');
$upload_file = new UploadFile('uploadfile');
$document->filename = 'robots.txt';
$document->document_name = 'robots.txt';
$document->save();
$contents = file_get_contents ($document->filename);
$revision = new DocumentRevision;
$revision->document_id = $document->id;
$revision->file = base64_encode($contents);
$revision->filename = $document->filename;
$revision->revision = 1;
$revision->doc_type = 'Sugar';
$revision->file_mime_type = 'text/plain';
$revision->save();
$document->revision_id = $revision->id;
$document->save();
$destination = clean_path($upload_file->get_upload_path($document->id));
$fp = sugar_fopen($destination, 'wb');
if( !fwrite($fp, $contents) ){
die("ERROR: can't save file to $destination");
}
fclose($fp);
WORKS! I Hope this will help someone
I have corrected 3 lines from code abowe:
//$document->revision_id = $revision->id;
//$document->save();
$destination = clean_path($upload_file->get_upload_path($revision->id));

Categories