PHPExcel different behavior on different systems - phpexcel

I have PHPExcel on localhost and on server.
When I try to read xlsx file on localhost - all ok, but when I try to read same file on server - all cells with cyrillic words are empty.
All systems have same PHPExcel and PHP versions.
What could be the problem?
Thanks!

The problem with this file is that it was created by an application that doesn't recognise case-sensitivity in filenames.
The rels table indicates that the Shared Strings table (where all the text string values for the workbook are stored) is called sharedStrings.xml, but the actual file in the zip is called SharedStrings.xml. A file generated by MS Excel itself uses the correct case in the filename, so I'm guessing that this file was created using some third-party tool or library. MS Excel is clearly more forgiving about case-sensitivity in filenames, allowing it to read the zip regardless.
I can probably fix this by using
$zip->getFromIndex(
$zip->locateName('sharedStrings.xml', ZIPARCHIVE::FL_NOCASE);
);
rather than
$zip->getFromName('sharedStrings.xml');
but it will take me a couple of days to implement the fix
EDIT
Somewhere around line 310 of the /PHPExcel/Reader/Excel2007.php file is the getFromZipArchive() method that can be changed to read:
private function getFromZipArchive($archive, $fileName = '')
{
// Root-relative paths
if (strpos($fileName, '//') !== false) {
$fileName = substr($fileName, strpos($fileName, '//') + 1);
}
$fileName = PHPExcel_Shared_File::realpath($fileName);
// Apache POI fixes
$contents = $archive->getFromIndex(
$archive->locateName($fileName, ZIPARCHIVE::FL_NOCASE)
);
if ($contents === false) {
$contents = $archive->getFromIndex(
$archive->locateName(substr($fileName, 1), ZIPARCHIVE::FL_NOCASE)
);
}
return $contents;
}
and will then be able to access the Shared String file case-insensitively

Related

How image uploading file name replace for CKFinder with asp.net?

I'm using file browser with CKFinder. But I have a problem.
During uploading filename is not saving Turkish format.
I want to load the file by changing the name.
Can any Replace method be applied?
For example;
File name: türkçe karakter sıkıntısı.jpg,
Modified name: turkce-karakter-sikintisi.jpg
Thank you.
Good luck with.
Read CKFinder. Rename uploaded files.
i.e. you need to update FileUploadCommandHandler.cs where you can modify sFileName in a way you like. To remove accent characters you can use code from How do I remove diacritics (accents) from a string in .NET?, to replace spaces you can use simple replace()
Example:
sFileName = System.IO.Path.GetFileName( oFile.FileName );
if ( Connector.CheckFileName( sFileName ) && !Config.Current.CheckIsHiddenFile( sFileName ) )
{
//custom logic
sFileName = RemoveDiacritics(sFileName).Replace(" ", "-");
//other logic
// Replace dots in the name with underscores (only one dot can be there... security issue).
...

Edit xml File and save it from c#

I have an xml file which contain images url . i am verifying the url whether url is responsive or not. If url is not responsive then i am removing that url from xml. and saving all changes . but i am getting error like
'The process cannot access the file 'E:\1.xml' because it is being used by another process'
xmlTR = new XmlTextReader(#"E:\1.xml");
PrimaryXmlDoc.Load(xmlTR);
foreach (XmlNode node in PrimaryXmlDoc.SelectNodes("/fp-hotel/Images/Url"))
{
if (CheckUrlExists(node.InnerText))
{
}
else
{
XmlElement _xmlElement = PrimaryXmlDoc.DocumentElement;
node.ParentNode.RemoveChild(node);
}
}
PrimaryXmlDoc.Save(#"E:\1.xml");
I assume that you have to Close XmlTextReader before using it second time. If you don't do that, the previous instance will keep your file open and you won't be able to open it again.
EDIT: And that's what happens here is probably that you want to save file before closing it.
Add line:
xmlTR.Close();
before
PrimaryXmlDoc.Save(#"E:\1.xml");

BitmapFactory.decodeStream returns null reading local file

A quick summary of my problem: I am trying to read a local image file. If I include the file in my assets directory and treat it as an asset everything works fine. However, if I try to read it from the external sd card, BitmapFactory.decode stream returns null.
I've had the same results with both .jpeg and .png files.
This is how I obtain an InputStream from the asset file:
InputStream isa = context.getAssets().open("Boulder.jpg");
This is how I obtain an InputStream from the asset file:
File f = new File( "/mnt/extSdCard/Maps/Colorado/Boulder.jpg" );
InputStream isf = new BufferedInputStream( new FileInputStream( f.toString() ) );
In either case the InputStream is passed to this code: (If this code looks familiar, it was borrowed from an internet example)
this.decoder = BitmapRegionDecoder.newInstance(inputStream, false);
tmpOptions.inJustDecodeBounds = true;
Bitmap temp = BitmapFactory.decodeStream(inputStream, null, tmpOptions);
setSceneSize(tmpOptions.outWidth, tmpOptions.outHeight);
tmpOptions.inJustDecodeBounds = false;
tmpOptions.inSampleSize = (1<<downShift);
sampleBitmap = BitmapFactory.decodeStream(inputStream, null, tmpOptions);
Again, using the asset file derived InputStream, everything works fine. Conversely, with the sdCard file, outWidth and outHeight are both -1 after the first call and sampleBitmap is null after the second.
I'm really wrapped around the axle on with this problem and have spent a lot of time browsing this forum and experimenting with suggested solutions all to no avail. I also wrote some test code which opened both versions of the file, creating InputStreams as shown above, then read the contents of each stream and compared each byte and found them to be identical.
I've tried the code on different devices with the same results. I'm currently testing on a Galaxy Note II (Android 4.1.1) with the following version info in the AndroidManifest:
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="16"/>
I need the ability to dynamically add files to the system so including everything as an asset is not an option. Any help would be greatly appreciated.
You can simple use
BitmapFactory.decodeFile(file.getAbsolutePath(), tmpOptions);
rather than
File f = new File( "/mnt/extSdCard/Maps/Colorado/Boulder.jpg" );
InputStream isf = new BufferedInputStream( new FileInputStream( f.toString() ) );
BitmapFactory.decodeStream(inputStream, null, tmpOptions)

File field is showing that my previously uploaded files are 0 bytes

Got a bit of a weird issue here. I recently started doing maintenance on a website that I did not originally build. The Drupal site has a bunch of nodes with an audio file field, and a jQuery player that plays them. On a lot of the nodes the player does not load, and I've realized this is because the file is reported as being 0 bytes when I edit the node. I'm not sure why this is. At first I was thinking it might be a file permissions thing, but I don't think thats the case as the permissions look fine to me. To fix it all I had to do was re-upload the file. Problem is that there are hundreds of these files and I'd like to fix it just by making one update if that is possible.
Here is a working version of Terry's pseudocode for Drupal 7.
$fids = db_query('SELECT fid FROM {file_managed} WHERE filesize = 0')->fetchCol();
foreach(file_load_multiple($fids) as $file) {
// Get full path to file.
$target = drupal_realpath($file->uri);
// If the file does not exist try the next file.
if (!file_exists($target)) {
echo "File $file->uri does not exist." .PHP_EOL;
continue;
}
// Find and store size of file
$file->filesize = filesize($target);
// Size of file is
if ($file->filesize > 0) {
file_save($file);
}
else {
echo "Size of $file->uri is still zero." . PHP_EOL;
}
}
Run it with drush:
drush php-script fix-file-sizes.php
We had the same problem: a lot of 0 byte files in the database.
What we did looked something like this:
function update_my_filesizes() {
$fileIDs = db_query('SELECT fid FROM {file_managed) WHERE filesize = 0')->fetchCol();
foreach(file_load_multiple($fids) as $file) {
// determine size of file
$filesize = SIZE_OF_THE_FILE; (pseudocode, we had to use filesize())
$file->filesize = $filesize;
if($file->filesize > 0) {
file_save($file);
} else {
echo "Filesize for file <filename here> is still 0 :(";
}
}
}

Is there way to read a text file from an assembly by using Reflection in C#?

I have a text file inside the assembly say MyAssembly. I am trying to access that text file from the code like this :
Stream stream = Assembly.GetAssembly(typeof(MyClass)).GetFile("data");
where data is data.txt file containing some data and I have added that .txt as Embedded Resources. I have dome reading of the images from the Assebly as embedded resources with code like this :
protected Stream GetLogoImageStream()
{
Assembly current = Assembly.GetExecutingAssembly();
string imageFileNameFormat = "{0}.{1}";
string imageName = "myLogo.GIF";
string assemblyName = current.ManifestModule.Name;
int extensionIndex = assemblyName.LastIndexOf(".dll", StringComparison.CurrentCultureIgnoreCase);
string file = string.Format(imageFileNameFormat, assemblyName.Remove(extensionIndex, 4), imageName);
Stream thisImageStream = current.GetManifestResourceStream(file);
return thisImageStream;
}
However, this approach did not work while reading the .txt file from an the executing assembly. I would really appreciate if anybody can point me to the approach to read .txt file from an assembly. Please dont ask me why I am not reading the file from the drive or the network share. Just say that the requirement is to read the .txt file from the Assembly.
Thank you so much
GetManifestResourceStream is indeed the correct way to read the data. However, when it returns null, that usually means you have specified the wrong name. Specifying the correct name is not as simple as it seems. The rules are:
The VB.NET compiler generates a resource name of <root namespace>.<physical filename>.
The C# compiler generates a resource name of <default namespace>.<folder location>.<physical filename>, where <folder location> is the relative folder path of the file within the project, using dots as path separators.
You can call the Assembly.GetManifestResourceNames method in the debugger to check the actual names generated by the compiler.
Your approach should work. GetManifestResourceStream returns null, if the resource is not found. Try checking the run-time value of your file variable with the actual name of the resource stored in the assembly (you could check it using Reflector).
I really appreciate for everybody's help on this question. I was able to read the file with the code like this :
Assembly a = Assembly.GetExecutingAssembly();
string[] nameList = a.GetManifestResourceNames();
string manifestanme = string.Empty;
if (nameList != null && nameList.Length > 0)
{
foreach (string name in nameList)
{
if (name.IndexOf("c.txt") != -1)
{
manifestanme = name;
break;
}
}
}
Stream stream = a.GetManifestResourceStream(manifestanme);
Thanks and +1 for Christian Hayter for this method : a.GetManifestResourceNames();

Resources