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 :(";
}
}
}
Related
I'm working on RESTFul API Project in Larave 5.7, and I'm using postman to test my APIs, I'm trying to upload multiple images or videos but nothing work with me!
I tried this way, however, it never gets inside the foreach loop!
And I also tried to use allFiles instead of file but it gives me only one file (maybe the last selected one)!!
foreach ($files as $file) {
$extension = $file->getClientOriginalExtension();
$check = in_array($extension,$allowedfileExtension);
if($check) {
foreach($request->fileName as $mediaFiles) {
// save here
}
} else {
return response()->json(['invalid_file_format'], 422);
}
return response()->json(['file_uploaded'], 200);
}
I was uploading the files in a wrong way with Postman!
This is the correct way :D
It's worked :D
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
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");
I would like to use PHPExcel to save some charts that already exist in an excel file as image files (Save as Image).
Is this possible and can anyone point me to some sample code as Google results only bring back code to generate new charts from numeric data.
You should look at the example 35chartrender.php in the /Tests directory, it does exactly what you want. Basically it all comes down to $chart->render($jpegFileName);. I don't think there is any way to render a chart as anything else than a jpeg, though you could always ask the expert at http://phpexcel.codeplex.com/.
If these are actual images rather than MS Excel charts, then take a look at section 4.6.37 of the developer documentation, entitled "Reading Images from a worksheet":
$i = 0;
foreach ($objPHPExcel->getActiveSheet()->getDrawingCollection() as $drawing) {
if ($drawing instanceof PHPExcel_Worksheet_MemoryDrawing) {
ob_start();
call_user_func(
$drawing->getRenderingFunction(),
$drawing->getImageResource()
);
$imageContents = ob_get_contents();
ob_end_clean();
switch ($drawing->getMimeType()) {
case PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_PNG :
$extension = 'png'; break;
case PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_GIF:
$extension = 'gif'; break;
case PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_JPEG :
$extension = 'jpg'; break;
}
} else {
$zipReader = fopen($drawing->getPath(),'r');
$imageContents = '';
while (!feof($zipReader)) {
$imageContents .= fread($zipReader,1024);
}
fclose($zipReader);
$extension = $drawing->getExtension();
}
$myFileName = '00_Image_'.++$i.'.'.$extension;
file_put_contents($myFileName,$imageContents);
}
If they are actual MS Excel charts, then (as Technoh has suggested) the chart render() method as demonstrated in example 35chartrender.php will use jpgraph to create an image file from that chart definition.
I would like to be able to unpack a .zip file using ASP classic.
I have had a bit of a poke around on google for something that will allow me to do this, and there seems to be a fair amount libraries out there. However all of these as far as I can tell require me to install a third party DLL.
The server that this script will be deployed on (or more accurately the IT department that control said server) will not allow me to use these to extend ASP's functionality and do what I have been asked to do (totally paradoxical!).
Is there any class library's out there that I might just be able to throw in as an include?
thanks for your time
Not sure is you can make it work with ASP, but in this project you will find a way to unZip in VB using a DLL, but you don't need to register the DLL, just put it somewhere where the class can find it.
I've used it in a VB 6 compiled app, but maybe you can adapt it to ASP. Not sure.
This is the code you will need: UnZip.cls
Hope it helps.
I have solved my problem... it's pretty messy, far from ideal, and dependant on server set up, but for the sake of anyone that has a similar problem and server in future here is how I solved it.
Basically I used PHP's ZIP library which seems to be installed on the server that I'm working on and made an unzip.php file:
<?PHP
$infile = $_REQUEST['infile'];
$outfile = $_REQUEST['outfile'];
$input_folder = "uploads";
$output_folder = "templates";
echo "false";
$zip = zip_open($input_folder."/".$infile);
if ($zip) {
while ($zip_entry = zip_read($zip)) {
$fp = fopen($output_folder."/".$outfile."/".zip_entry_name($zip_entry), "w");
if (zip_entry_open($zip, $zip_entry, "r")) {
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
fwrite($fp,"$buf");
zip_entry_close($zip_entry);
fclose($fp);
} else {
echo "false";
exit;
}
}
echo "true";
zip_close($zip);
} else {
echo "false";
exit;
}
?>
Then where I wanted to call this in my ASP script I HTTPXML'd to the PHP file location on same server with my variables as part of the querystring.
Response.Buffer = True
Dim objXMLHTTP, xml
Set xml = Server.CreateObject("Microsoft.XMLHTTP")
xml.Open "GET", "http://" & strdomain & "/unzip.php?infile="& filename &"&outfile=" & out_foldername, False
xml.Send
if xml.responseText = "true" then
SaveFiles = SaveFiles & "(unzip successful!)"
else
SaveFiles = SaveFiles & "(unzip failed!)"
end if
Set xml = Nothing
next
where
filename = The name of the file that you want to unzip
out_folder = The name of the folder that you want put your unzipped files into
strdomain = Request.ServerVariables("HTTP_HOST")
SaveFiles = my return variable.
I'm sure there must be a better way of doing this, but for the time being in my situation this seems to work ok (and hopefully no one will ever know!).