I want to convert an excel file to a text file. I have tried this code but it doesn't work:
$destination = 'products.xls';
$section = file_get_contents($destination);
$File = "file.txt";
$File = str_replace(';','#',$File);
$Handle = fopen($File, 'w');
$Data = $section;
fwrite($Handle, $Data);
print "Data Written"; die;
When I open the text file it shows me special characters and when I convert csv to text it shows me the right result. Please provide any helpful links.
You cannot read the Excel file that way. Excel file is binary file that follows the XLS format while file_get_contents is for pure text file. You need a library (like https://github.com/PHPOffice/PHPExcel) to read the content then write to text.
Related
Hi i have the following code to decode and save image
$photo = $_REQUEST['image'];
$binary = base64_decode ($photo);
header ('Content-Type: bitmap; charset=utf-8');
$file = fopen ('uploads/'.time().'.png', 'wb');
fwrite($file, $binary);
fclose ($file);
This works fine but i will like to get the name of the image after it is written and store that name as a variable to use in the rest of the code where it will be saved in mysql database. I can write to mysql i just need to get the file name. Thanks
You are already naming the file yourself. Just store it in a variable before you write the file.
$filename = time().'.png';
$file = fopen ('uploads/'.$filename, 'wb');
I want to upload image file into alfresco using cmis api using PHP..
I can create simple text document in alfresco using following code
$obs = $client->createDocument($myfolder->id, $repo_new_file,$prop, "testssss", "text/plain");
I tried following code to upload image
$obs = $client->createDocument($myfolder->id, $repo_new_file,$prop, null, "image/jpeg");
But can't create image file into alfresco
Can anyone help me to solve this issue ??
I got the solution of this problem
Just store base64 content into image.. Use below code
$filename="A.jpg";
$handle = fopen($filename, "r");
if(!$handle)return FALSE;
$contents = fread($handle, filesize($filename));
if(!$mimetype)$type=mime_content_type($filename);
else $type=$mimetype;
fclose($handle);
$base64_content=base64_encode($contents);
$obs = $client->createDocument($myfolder->id, $repo_new_file,$prop, base64_decode($base64_content), "image/jpg");
In PHP im creating a tool to open txt file with an integer in it, increment the number, save the number to a variable and then save and close the file. this is the code i have for it and it doesnt seem to work when i have it on my test server. Can anyone clue me in as to why this isnt working properly?
//opens pclnumber.txt to $handle, saves number to $number, Increments number in text file, saves and closes file
$handle = fopen("pclnumber.txt", "w+");
$number = fread($handle);
fwrite($handle, $number+1);
fclose($handle);
over all you must set reading permission on your file, and than you can use the follow code:
$filename = "pclnumber.txt";
//read file content
$handle = fopen($filename, "r");
$number = fread($handle, sizeof($filename));
fclose($handle);
//update file content
$handleWrite = fopen($filename, "w+");
fwrite($handleWrite, $number+1);
fclose($handleWrite);
Bye,
Marco
I have a base64-encoded string containing a pdf. How to create a .pdf file from this encoded string using php?
Something like Content-type:application/pdf in header function?
this is the base64 encoded string
Try this piece of code
$pdf_base64 = "base64pdf.txt";
//Get File content from txt file
$pdf_base64_handler = fopen($pdf_base64,'r');
$pdf_content = fread ($pdf_base64_handler,filesize($pdf_base64));
fclose ($pdf_base64_handler);
//Decode pdf content
$pdf_decoded = base64_decode ($pdf_content);
//Write data back to pdf file
$pdf = fopen ('test.pdf','w');
fwrite ($pdf,$pdf_decoded);
//close output file
fclose ($pdf);
echo 'Done';
With this you can create and open the file... but maybe this is not the answer to the question
$data = base64_decode($this->Get(self::Body));
file_put_contents('file.pdf',$data);
The answer is echo the content with a header :
$data = base64_decode($this->Get(self::Body));
header('Content-Type: application/pdf');
echo $data;
I am trying to get the image header from a png file. as the binary content show as neeble how can i show it as hexadecimal? i tried to base_convert() but it is not returning the desired output as i am getting the poutput in hexadecimel editor. any help will be appreciated.I am trying to do it with php
Try this for size:
$filename = "C:\\wamp\\www\\projects\\cog.png";
$handle = fopen($filename, "rb");
$binContents = fread($handle, filesize($filename));
fclose($handle);
$hexContents = bin2hex($binContents);
echo $hexContents;