I'm trying to access the mime.types file from a model in codeIgniter.
function get_mime_type($filename, $mimePath = '../../assets'){
$fileext = substr(strrchr($filename, '.'), 1);
if (empty($fileext)) return (false);
$regex = "/^([\w\+\-\.\/]+)\s+(\w+\s)*($fileext\s)/i";
$lines = file("$mimePath/mime.types");
foreach($lines as $line){
if (substr($line, 0, 1) == '#') continue; // skip comments
$line = rtrim($line) . " ";
if (!preg_match($regex, $line, $matches)) continue; // no match to the extension
return ($matches[1]);
}
return (false); // no match at all
}
I've already tried the following when I call it:
$this->get_mime_type($filename, $mimePath = '../../assets');
$this->get_mime_type($filename, $mimePath = '/zenoir/assets');
$this->get_mime_type($filename, $mimePath = 'http://localhost:8080/zenoir/assets/mime.types');
But no luck. The mime.types file is in the assets folder and the model is inzenoir/application/models
The error was:
A PHP Error was encountered
Severity: Warning
Message: file(../../assets/mime.types) [function.file]: failed to open stream: No such file or directory
Filename: models/files.php
Line Number: 46
Why are you getting into so much trouble for the mime types, use mime_content_type() instead.
echo mime_content_type($mimePath.$filename);
And the problem with your code is the path issue. Use BASEPATH constant and traverse from there instead. You will omit all these sort of path issue.
Related
I am trying to unarchive a zip file that was inside another zip file to get to the xml file that is in the second zip file.
The big challenge with this file is that the files inside the zip file will always be different and therefore unknow. So I created a function to put the list of the archived archives into a text list. Then use this list to unarchive each file and extract the information that is needed from the xml file that is inside the second zip file.
Here is my code so far.
//Set the date
$day = date("mdY");
echo $day."<br>";
//URL to download file from for updating the prescription table
//$url = "ftp://public.nlm.nih.gov/nlmdata/.dailymed/dm_spl_daily_update_".$day.".zip";
$url = "ftp://public.nlm.nih.gov/nlmdata/.dailymed/dm_spl_daily_update_09152016.zip";
//Saving the file on the server.
file_put_contents("Prescription_update.zip", fopen($url, 'r'));
//unzip the downloaded file
$zip = new ZipArchive;
if($zip->open('Prescription_update.zip')){
$path = getcwd() . "/update/";
$path = str_replace("\\","/",$path);
//echo $path;
$zip->extractTo($path);
$zip->close();
print 'ok<br>';
} else {
print 'Failed';
}
// integer starts at 0 before counting
$i = 0;
$dir = '/update/prescription/';
if ($handle = opendir($dir)) {
while (($file = readdir($handle)) !== false){
if (!in_array($file, array('.', '..')) && !is_dir($dir.$file))
$i++;
}
}
// prints out how many were in the directory need this for the loop later
echo "There were $i files<br>";
$dh = opendir($dir);
$files = array();
while (false !== ($filename = readdir($dh))) {
$files[] = $filename." \r\n";
}
//Created a list of files in the update/prescription folder
file_put_contents("list.txt", $files);
/*
* Creating a loop here to ready the names and extract the
* XML file from the zipped files that are in the update/prescription folder
*
*/
$ii = 2;
$fileName = new SplFileObject('list.txt');
$fileName->seek($ii);
echo $fileName."<br>"; //return the first file name from list.txt
$zip = new ZipArchive;
$zipObj = getcwd()."/update/prescription/".$fileName;
$zipObj = str_replace("\\","/", $zipObj);
echo $zipObj."<br>";
if($zip->open($zipObj)){
$path = getcwd() . "/update/prescription/tmp/";
$path = str_replace("\\","/",$path);
mkdir($path);
echo $path;
$zip->extractTo($path);
$zip->close();
print 'ok<br>';
} else {
print 'Failed';
}
Can't figure out why the second ZipArchive::extractTo: is throwing the error. I thought it may have been a path problem. I so I did the second string replacement hoping that would clear it up but it did not. So, throwing up my hands and asking for a second set of eyes on this one.
UPDATE ERROR LOG ENTRY
[18-Sep-2016 02:24:24 America/Chicago] PHP 1. {main}() C:\emr-wamp\www\interface\weno\update_prescription_drug_table.php:0
[18-Sep-2016 02:24:24 America/Chicago] PHP 2. ZipArchive->extractTo() C:\emr-wamp\www\interface\weno\update_prescription_drug_table.php:76
[18-Sep-2016 02:24:24 America/Chicago] PHP Warning: ZipArchive::close(): Invalid or unitialized Zip object in C:\emr-wamp\www\interface\weno\update_prescription_drug_table.php on line 77
[18-Sep-2016 02:24:24 America/Chicago] PHP Stack trace:
[18-Sep-2016 02:24:24 America/Chicago] PHP 1. {main}() C:\emr-wamp\www\interface\weno\update_prescription_drug_table.php:0
[18-Sep-2016 02:24:24 America/Chicago] PHP 2. ZipArchive->close() C:\emr-wamp\www\interface\weno\update_prescription_drug_table.php:77
I was going to vote to delete this question but I thought it would be better to leave it over the long run.
The answer is
How to get PHP ZipArchive to work with variable
It seems that the whole name cannot be subbed as a variable. But if you sub part of the name it is allowed.
$zipObj = getcwd()."/update/prescription/".$fileName;
It has to be subbed like
$z->open("update/".$r.".zip"){
//do something here
}
Whenever i add an extension to Magento following error occurs:
Fatal error: Call-time pass-by-reference has been removed in
C:\xampp1\htdocs\magento\app\code\community\Bricks\Xmlbrowser\Helper\Data.php
on line 39
I am using Magento 1.8.00-alpha1
public function searchFiles($dir, $str, $filesList = array()) {
$dir = rtrim($dir, '\\\/').DS;
$files = scandir($dir);
foreach ($files as $file) {
if (is_dir($dir.$file)) {
if (($file != '.') && ($file != '..')) {
$this->searchFiles($dir.$file, $str, &$filesList);
}
}
else {
$needToSearch = true;
if ($this->_file_types && is_array($this->_file_types)) {
$needToSearch = in_array(strtolower(pathinfo($file, PATHINFO_EXTENSION)), $this->_file_types);
}
if ($needToSearch) {
$fileContent = file_get_contents($dir.$file);
if ($needToSearch) {
$fileContent = file_get_contents($dir.$file);
$result = $this->_case_sensitive? strpos($fileContent, $str) : stripos($fileContent, $str);
if ($result !== FALSE) {
$filesList[] = $dir.$file;
}
}
}
}
return $filesList;
}
}
First, change
$this->searchFiles($dir.$file, $str, &$filesList); // This line is 39
to
$this->searchFiles($dir.$file, $str, $filesList); // This line is 39
Second, change
public function searchFiles($dir, $str, $filesList = array()) {
to
public function searchFiles($dir, $str, &$filesList = array()) {
Third, change the 6th line within the method searchFiles()
$this->searchFiles($dir.$file, $str, &$filesList);
to
$this->searchFiles($dir.$file, $str, $filesList);
This should fix the specific fatal error that you're asking about here.
But...
...even if fixed, most probably just the next fatal error will popup.
Like I aready wrote in the comments, my guess is you're running a PHP 5.4 system, but use Magento 3rd party extensions, which are not PHP 5.4 compatible.
If PHP 5.4 is a must on your target system, I'd recommend to contact the authors of the extensions and ask for 5.4 compatible versions of their extensions, or patches to make them 5.4 compatible. You can of course also try to fix them all by yourself, or hire a developer to do it.
If PHP 5.4 is not mandatory, I'd probably check whether downgrading the system to PHP 5.3.latest won't be the better/cheaper/less time consuming option.
I'm trying to create php script to scan directory and delete file in this directory. I have problem with my scanning file extension is not working right
<?php
if ($handle = opendir(''))
{
echo " Directory handle: $handle \n";
echo "Entries: \n";
while (false !== ($entry = readdir($handle)))
{
echo" $entry :\n";
$file_parts = pathinfo($entry);
switch ($file_parts['extension'])
{
case 'dmg':
echo "dmg";
break;
default:
echo "no file";
break;
}
}
closedir($handle);
}
?>
Notice: Undefined index: extension in /Applications/MAMP/htdocs/dir.php on line 13
This error would be caused if for example the path doesn't have an extension or some error occured.
If you want to retreive the filetype of the files it seems like a roundabout way of doing so when you can just do
switch(filetype($dir.$file)){
case 'dmg' ://And so on
}
Here $dir would be /Users/username/Downloads
Undefined Index means it doesn't exists in the array.
From docs:
If the options parameter is not passed, an associative array containing the
following elements is returned: dirname, basename, extension (if any), and filename.
Your files do have extension?
Also, put '.' (dot) in your dir instead of blank space.
You can use var_dump( $file_parts ); to see what it is.
Thanks in advance.
Getting this warning when using below code:
Warning: file_get_contents(test.php) [function.file-get-contents]: failed to open stream: No such file or directory in /path/index.php on line so-n-so.
Here's the code I am using,
<?php
// Scan directory for files
$dir = "path/";
$files = scandir($dir);
// Iterate through the list of files
foreach($files as $file)
{
// Determine info about the file
$parts = pathinfo($file);
// If the file extension == php
if ( $parts['extension'] === "php" )
{
// Read the contents of the file
$contents = file_get_contents($file);
// Find first occurrence of opening template tag
$from = strpos($contents, "{{{{{");
// Find first occurrence of ending template tag
$to = strpos($contents,"}}}}}");
// Pull out the unique name from between the template tags
$uniqueName = substr($contents, $from+5, $to);
// Print out the unique name
echo $uniqueName ."<br/>";
}
}
?>
The error message says that the file isn't found.
This is because scandir() returns only the basename of the files from your directory. It doesn't include the directory name. You could use glob() instead:
$files = glob("$dir/*.php");
This returns the path in the result list, and would also make your extension check redundant.
I would suggest that you need to exclude . and .. from the list of files picked up by scandir() DOCs.
// Iterate through the list of files
foreach($files as $file) {
if('.' == $file or '..' == $file) {
continue;
}
...
Also you need to put the path before your file name:
$contents = file_get_contents($path . $file);
This is my code to go through all special folder "folderContainer" which inside other drives:
$folderNames = '';
// go through all drives.Just want to find out which has folderContainer
for($i='D';$i<='Z';$i++){
// get the path
$path = realpath($i.':\\folderContainer\\');
$folders = new DirectoryIterator($path);
foreach($folders as $folder){
if($folder->isDot()) continue;
// get folder's name which match my pattern
if ($folder->isDir() && preg_match('/^[A-Z]{1}[0-9]{1,9}$/i', $folders)){
// store the folder name
$folderNames .= ' '.$folder.' ';
}
}
}
And got these error messages:
Fatal error: Uncaught exception 'RuntimeException' with message 'Directory name must not be empty.'
But my code work correct when I set the $path to "D:\\folderContainer\\" without a loop. So, how can I to make the code to go through all folderContainer which inside other drives .
Thank you very much!!
realpath returns null if the path doesn't exist. And probably you want to skip not existing folders
//...
$path = realpath($i.':\\folderContainer\\');
if (empty($path))
continue;
//...