I am new to php and I am trying to create a file upload system that will automatically parse the xml file using simplexml. I have created a php script that will open the directory and try to parse the files. For some reason, it will only parse one of the files. I am not sure if this is the best way to aproach this task.
<?php
$dir = "path/to/xmlfiles"
chdir($dir);
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
$xml = simplexml_load_file($file);
$nombre = $xml ->xpath("//NOMBRE");
$rpu = $xml ->xpath("//RPU");
echo (string) $nombre[0];
echo (string) $rpu[0];
echo $file;
}
closedir($dh);
}
}
?>
For this script, I am able to echo the results just fine, the only problem is that it will only echo one of the xml file resutls.
Hopefully someone with more experience could give me a tip on how to achieve this.
For extra points, I am also trying to insert an entry to a Mysql database for each parsed file.
;) Thank you in advance for all your help.
readdir() reads directory entries as they're stored on disk (i.e., it doesn't sort entries) so it's very likely that . (current directory) will be the first one. That will make simplexml_load_file() fail and $xml will become false so $xml->xpath() will crash the script with a fatal error.
PHP should be reporting all this. If you cannot see it, it's very likely that you haven't configured PHP to display errors.
You need to filter out entries (the bare minimum would be to check they are actual files and not directories) and add some error checking here and there.
An alternative approach:
foreach (glob("$dir/*.xml") as $file) {
}
Related
Am creating pdf files with TCPDF.Every single user gets a different folder.
This is a path name: $_SERVER['DOCUMENT_ROOT'].'/bandymas/pdfDocuments/'.$_SESSION["userSession"].There are no problem with file creation.
Now i need to see a list of created files and make them available for open.
The problem is, my page is crashing and I can't see the list.
$dir='/'.$_SERVER['DOCUMENT_ROOT'].'/bandymas/pdfDocuments/'.$_SESSION['userSession'].'/';
if(is_dir($dir)){
if($dh=opendir($dir)){
echo "My documents list:";
while(($fileName=readdir($dir)) !==false){
echo " view","\n";
}
close($dh);
}
}
The issue here is that line: while(($fileName=readdir($dir)) !==false)
A simple look into the php documentation of that function points out the reason why things fail:
string readdir ([ resource $dir_handle ] ) requries a directory handle as argument, not a file system path. So the line should be: while(($fileName=readdir($dh)) !==false). $dh is the variable holding your directory handle you got returned a few lines above when opening the folder.
This is a very common and typical issues with scripts getting implemented. We all make such mistakes. Nothing to worry about. But what you should learn from this is: monitor your http servers error log file. Such issues are pointed out in there, you can actually read in there what issue you are dealing with and typically also in which precise line in what file that issue occurs. You cannot seriously develop php without monitoring that error log file.
Use Code Below Working Just Fine.
$dir='/'.$_SERVER['DOCUMENT_ROOT'].'/bandymas/pdfDocuments/'.$_SESSION['userSession'].'/';
if (is_dir($dir)){
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
}
closedir($dh);
}
}
You used close($dh) instead of closedir($dh)
I am writing a script that will go through all my .js files and minify them into one .php file to be included on the site. I just run this script after I have edited some js and want to upload it to the live site.
The issue: I can not load the content of jquery-2.1.4.min.js using file_get_contents. I have tried changing the name of the file to jquery.js and that did not help. I do not have any complex javascript in the other files (just random strings) but they open fine.
With the code:
if (!file_get_contents($filename)) {
die ("dammit");
}
I get the response of "dammit". All other files are fine though, so I know the file name and path are correct. One of the weird things is that there are no errors coming up (I have used error_reporting (-1); to make sure they will).
Is anyone else able to get the file contents of jquery? Any ideas what would cause this and if it will be a problem with other javascript or css?
As requested, here is the full code:
$buffer = $jsStartBuffer;
//get a list of files in the folder (only .js files)
$fileArray = array();
if (is_dir($jsMakeFile["SourcePath"])){
if ($dh = opendir($jsMakeFile["SourcePath"])){
while (($file = readdir($dh)) !== false){
$file_parts = pathinfo($jsMakeFile["SourcePath"].$file);
if ($file_parts['extension'] == "js") {
$fileArray[] = $file;
}
}
}
}
print_r($fileArray);
foreach ($fileArray as $nextRawFile) {
$buffer .= file_get_contents($jsMakeFile["SourcePath"].$nextRawFile);
if (!file_get_contents($jsMakeFile["SourcePath"].$nextRawFile)) {
die ("dammit");
}
echo $jsMakeFile["SourcePath"].$nextRawFile;
}
$buffer .= $jsEndBuffer;
echo $buffer;
$buffer = \JShrink\Minifier::minify($buffer);
file_put_contents($jsMakeFile["finalFile"]["path"].$jsMakeFile["finalFile"]["name"], $buffer);
When I put other .js files in there it is fine (I even tried lightbox.min.js and it worked fine!) I have tried a few different versions of jquery.min and they all seem to fail.
OK, solution found. It is to do with the actual file created by jquery.
The way I solved it was:
- Go to the query site, and instead of downloading the required file, open it in a new tab/window
- Copy all the content in this window
- Create a new file where required and name as required
- Paste the content into this file and save it
This new file will now be able to be read by file_get_contents. I would imagine this solution would help if you are trying to work with jquery (and other) files in php in any way and having issues.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to run 600 XML files through a script I've made that extracts specific pieces of information and saves each one in JSON format. All 600 XML files are inside a folder ready to be run through the PHP file, I'm now looking for a fast way to do it.
Essentially this is the process the PHP file goes through:
PHP reads single XML file via URL -> locally saves important info in variables -> saves important info into JSON file
Is there a way I can somehow run all 600 XML files through my PHP file?
Thanks
Open the directory containing the XML files and then process them, here are some of the most common way todo that.
opendir()
<?php
$dir = "/etc/php5/";
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
}
closedir($dh);
}
}
?>
You can also use glob()
<?php
foreach (glob("*.txt") as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}
?>
Inside the foreach loop of whichever you choose you can use file_get_contents() or fread() then you can do your conversion to json.
<?php
// get contents of a file into a string
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
?>
Hope it helps
Just go ahead and try! You'll probably run into a timeout error. If you do, try configuring the max timeout settings. http://php.net/manual/en/function.set-time-limit.php
Joel,
Sounds to me like what you need to is to use readdir
http://php.net/manual/en/function.readdir.php
This will allow you to get a list of files in a directory to iterate over.
$dir = opendir('/path/to/files');
while($file = readdir($dir)) {
if ($file !== '.' && $file !== '..' && !is_dir($file)) {
$parthParts = pathinfo($file);
if ($pathParts['extension'] === 'xml') {
runscripton($file);
}
}
}
closedir($dir);
First, write a function that gets an XML file name, and after processing, returns the results in php array or JSON (Based on how you need your code to be).
To write this function, you need to parse XML (http://php.net/manual/en/book.xml.php).
To work with JSON in PHP: http://php.net/manual/en/book.json.php
Then, write your main code. Your main code should enumerate all XML files in the folder, and then call your function for each file, and gather/generate JSON using information returned by the function.
You might need readdir to gather all of XML files in the folder. (http://php.net/manual/en/book.xml.php)
Don't forget to increase time limit as long as there are lots of XML files and the process might take long so a timeout error would occur. (http://php.net/manual/en/function.set-time-limit.php)
When I use simplexml_load_file to individual file, it works fine. However, since I have so many of them, when I tried to run my script to a batch of files.
In case this is relavant, I have two kind of log files to load into my database. One starts with . The other starts with . (But looking at the error, the error occurs even w/ the same structure.)
<?php
$dir_path = ".";
if ($dir_handler = opendir($dir_path)) {
while (($sub_dir = readdir($dir_handler)) !== false) { //reading all sub dir
if (is_dir($sub_dir)) {
if (substr($sub_dir,0,6) == "201209") { //filter only desired sub dir
$sub_dir_handler = opendir($sub_dir);
while($file = readdir($sub_dir_handler)) { //reading files in each
//qualified sub dir
if (($file != ".") && ($file != "..")) { //except . and ..
$xml = simplexml_load_file($file); // got error on
//the second file
if ($xml->getname() != "hash") { // tried to distinct
// structure type but error
I guess simplexml_load_file return false (error)
You can see those using libxml_get_errors, cf http://php.net/manual/en/simplexml.examples-errors.php
Edit:
Considering your 2nd comment, looks like your file is not accessible to SimpleXML...
Found the silly cause.
because I keep all files in the subdirs, I "need" to concat the subdir to the filename to locate the path. The error, thus, cause by cannot open file. It also explains why I can open it individually when I hard code the filename.
Since there is now no error, the libxml_get_error cannot be applied to my script.
:)
Since I want to skip the second type of xml schema which causes the error I use
if (!libxml_get_errors())
to continue my work. Many many thank to you.
I'm not a developer, but I'm the default developer at work now. : ) Over the last few weeks I've found a lot of my answers here and at other sites, but this latest problem has me confused beyond belief. I KNOW it's a simple answer, but I'm not asking Google the right questions.
First... I have to use text files, as I don't have access to a database (things are locked down TIGHT where I work).
Anyway, I need to look into a directory for text files stored there, open each file and display a small amount of text, while making sure the text I display is sorted by the file name.
I'm CLOSE, I know it... I finally managed to figure out sorting, and I know how to read into a directory and display the contents of the files, but I'm having a heck of a time merging those two concepts together.
Can anyone provide a bit of help? With the script as it is now, I echo the sorted file names with no problem. My line of code that I thought would read the contents of a file and then display it is only echoing the line breaks, but not the contents of the files. This is the code I've got so far - it's just test code so I can get the functionality working.
<?php
$dirFiles = array();
if ($handle = opendir('./event-titles')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$dirFiles[] = $file;
}
}
closedir($handle);
}
sort($dirFiles);
foreach($dirFiles as $file)
{
$fileContents = file_get_contents($file);//////// This is what's not working
echo $file."<br>".$fileContents."<br/><br/>";
}
?>
Help? : )
Dave
$files = scandir('./event-titles') will return an array of filenames in filename-sorted order. You can then do
foreach($files as $file)
{
$fileContents = file_get_contents('./event-titles/'.$file);
echo $file."<br/>".$fileContents."<br/><br/>";
}
Note that I use the directory name in the file_get_contents call, as the filename by itself will cause file_get_contents to look in the current directory, not the directory you were specifying in scandir.