Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-08-29
15
Medium Priority
?
50 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
14 Comments
 
LVL 111

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 111

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 111

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 111

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
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

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 60

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 111

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 60

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 111

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 60

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

773 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