Solved

My output has a "?" on the name and the file doesn't open anymore

Posted on 2016-08-29
15
36 Views
Last Modified: 2016-08-30
<?php
error_reporting(E_ALL);
ini_set('display_errors' ,1);

//expression to be found in file name
$find = '.5010.';

//directory name
//we will store renamed files here
$dirname = '5010';
if(!is_dir($dirname))
    mkdir($dirname, 0777);

//read all files from a directory
//skip directories
$directory_with_files = './';
$dh  = opendir($directory_with_files);
$files = array();
while (false !== ($filename = readdir($dh)))
{
    if(in_array($filename, array('.', '..')) || is_dir($filename))
        continue;

    $files[] = $filename;
}

//iterate collected files
foreach($files as $file)
{
    //check if file name is matching $find
    if(stripos($file, $find) !== false)
    {

        //open file
        $handle = fopen($file, "r");
        if ($handle)
        {
            //read file, line by line
            while (($line = fgets($handle)) !== false)
            {

                //find REF line
                        $refid = 'REF*2U*';
                        if(stripos($line, $refid) !== false)
                              {
                              //glue refernce numbers
                              //check if reference number is not empty
                              
                              $refnumber = str_replace(array($refid, '~'), array('', ''), $line);
                        if($refnumber != '')
                              {
                        $refnumber = '_'. $refnumber .'_';

                        $filerenamed = str_replace($find, $refnumber, $file);
                              copy($file, $dirname . '/' . $filerenamed);
                                    }

                        echo $refnumber . "\n";
                              }
            }

            //close file
            fclose($handle);
        }
    }
}
0
Comment
Question by:Jasmine Ikhreishi
  • 8
  • 3
  • 3
15 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41774661
Please use the code snippet feature when you post code at E-E, thanks.

Please show us the file name that "doesn't open any more."  Then we can probably show you a tested and working example.
0
 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41774715
For example; My file is 4867586.5010.476564.ed

After the code executes and reads the file, the output should be: 4867586_SMIL01_476564.ed but instead its: 4867586_SMIL01

And when I checked it out on putty the file name was: 4867586_SMIL01?_476564.ed
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41774734
OK, thanks.  That's helpful.  I'll see if I can find what is causing the issue.  Back after I make a few tests!
1
 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41774747
Alright, also I have the zend version of php so it's not the lastest update. Thank you!
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41774774
I wonder if there is something else in play here.  I can't see where "SMIL01" could be coming from.  Any ideas?
0
 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41774783
ISA*00*          *00*          *ZZ*133052274      *ZZ*362931550      *160822*1638*^*00501*000025142*0*P*:~
GS*HP*132274160822*1638*1*X*00X221A1~
ST*835*0001~
BPR*H*0*C*NON*****20160824~
TRN*1*1QG630664*1411289245*000087726~
REF*F2*1083~
DTM*405*20160819~
N1*PR*UNITED *XV*87726~
N3*990~
N4*MINKA*MN*559664~
REF*2U*SMIL01~
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41774789
OK, what is that?  Is it the contents of the file?  If so, it's helpful to attach the file  or if it's just a short text file, post it in the code snippet.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41774844
Also, with a small bit of tinkering, I was able to run the script.  I put the original file here:
https://iconoun.com/demo/storage/4867586.5010.476564.ed

I put the "converted file" into the "5010" directory.  Here is the "view source" of the directory listing:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
 <head>
  <title>Index of /demo/5010</title>
 </head>
 <body>
<h1>Index of /demo/5010</h1>
<ul><li><a href="/demo/"> Parent Directory</a></li>
<li><a href="4867586_SMIL01%20%0a_476564.ed"> 4867586_SMIL01 
_476564.ed</a></li>
</ul>
</body></html>

Open in new window

As you can see we got what looks like an extraneous %20%0a injected into the file name.  My guess is that you want to trim() one of the data elements that is used to create the file name.
1
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41774878
This worked (I think).  Have a look at the lines that say REMOVE UNWANTED WHITESPACE CHARACTERS.

If you're new to PHP and want to learn the language, this article can help.  It has links to many good learning resources and more importantly, can help you avoid the many outdated and bad examples that litter the internet!
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html


<?php // demo/temp_jasmine.php
/**
 * https://www.experts-exchange.com/questions/28966264/My-output-has-a-on-the-name-and-the-file-doesn't-open-anymore.html
 *
 For example; My file is 4867586.5010.476564.ed
 After the code executes and reads the file, the output should be: 4867586_SMIL01_476564.ed but instead its: 4867586_SMIL01
 And when I checked it out on putty the file name was: 4867586_SMIL01?_476564.ed
 */
error_reporting(E_ALL);

//expression to be found in file name
$find = '.5010.';

//directory name
//we will store renamed files here
$dirname = '5010';
if(!is_dir($dirname)) mkdir($dirname, 0777);

//read all files from a directory
//skip directories
$directory_with_files = './';
$directory_with_files = 'storage'; // USE THIS DIRECTORY FOR TESTS

$dh  = opendir($directory_with_files);
$files = array();
while (false !== ($filename = readdir($dh)))
{
    if(in_array($filename, array('.', '..')) || is_dir($filename))
        continue;

    $files[] = $filename;
}

//iterate collected files
foreach($files as $file)
{
    //check if file name is matching $find
    if(stripos($file, $find) !== false)
    {
        $handle = fopen($directory_with_files . DIRECTORY_SEPARATOR . $file, "r");
        if ($handle)
        {
            while (($line = fgets($handle)) !== false)
            {
                $line = trim($line); // REMOVE UNWANTED WHITESPACE CHARACTERS
                //find REF line
                $refid = 'REF*2U*';
                if(stripos($line, $refid) !== false)
                {
                    $refnumber = str_replace(array($refid, '~'), array('', ''), $line);
                    $refnumber = trim($refnumber); // REMOVE UNWANTED WHITESPACE CHARACTERS
                    var_dump($refnumber);
                    if($refnumber != '')
                    {
                        $refnumber = '_'. $refnumber .'_';
                        $filerenamed = str_replace($find, $refnumber, $file);
                        copy($directory_with_files . DIRECTORY_SEPARATOR . $file, $dirname . DIRECTORY_SEPARATOR . $filerenamed);
                    }
                    echo $refnumber . "\n";
                } // END IF
            } // END WHILE READING FILE
            fclose($handle);
        }
    } // END IF FILENAME MATCH
}

Open in new window

0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41775232
Here is a slightly more condensed version of your code - tested it on your sample file it seems to work.
<?php
error_reporting(E_ALL);

// Some setup stuff
$find = '.5010.';
$dirname = 't1517/5010';

// Get list of files using glob with wildcard
$files = glob("t1517/*{$find}*");

//iterate collected files
foreach($files as $file) {
  // Read all the lines in the file into an array
  $lines = file($file);
  
  // Iterate over the array of lines
  foreach($lines as $line) {
   
    // check if line contains search string
    // using preg_match to check and extract 
    // reference at the same time
    if (preg_match("/REF\*2U\*(.*?)\~/", $line, $match)) {
    
    // glob uses full paths so extract the filename from
    // the path
      $newfile = basename(str_replace($find, "_{$match[1]}_", $file));
    
    // Some debug code
      echo "f: {$file}, NF: {$newfile}<br/>";
    
    // And finally do the copy.
      copy($file, "{$dirname}/{$newfile}");
    }
  }
}

Open in new window

EDIT
the folder t1517 is my test folder - modify to match your environment
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41775395
@Julian: with file() you might  want to set both FILE_IGNORE_NEW_LINES and FILE_SKIP_EMPTY_LINES.  I say might  because I already posted a working example, and in the course of my testing I found extraneous whitespace to be the source of the file name problem.  But this hasn't been verified by the question author yet, and we don't know for sure whether all lines need to be trimmed, or whether only the information in the line with the REF*2U.
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41775432
I am cheating a bit because I am using information from his other thread. From what I understand he wants to use the REF*2U prefix to find the reference to use to rename the file. So while the file might have whitespace - as long as there is a line that matches the RegEx - it should work. I agree about the FILE_IGNORE_NEW_LINES / FILE_SKIP_EMPTY_LINES - might make the code a bit more efficient - but I was trying to keep it simple. I used file() as a shortcurt - but the solution can be adapted to use the opendir / while loop just as easily - the principle is the same.

For me the key piece here was the preg_match - stripos will work but preg_match checks for presence of the search string and extracts the bit you are looking for in the same call.

glob() also makes it easier allowing for a wildcard match - although there are some watch-it's with glob as well so the OP would do well to read up on the function.

I think Zephyr_hex' makes a good point in his  comment in the other thread. We might be chasing our tails a bit here trying to solve a problem that has the potential to grow into a very complicated beast.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41775482
... potential to grow ...
Yes, exactly.  If the instant answers do not solve the issues, these questions might be good candidates for E-E Gigs.
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41776166
Yup, I am going to object - don't know what

"The format inside is wrong"
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now