Web Analytics Made Easy -
StatCounter Clean up script throwing Warning: Wrong parameter count for strstr() - CodingForum

Announcement

Collapse
No announcement yet.

Clean up script throwing Warning: Wrong parameter count for strstr()

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Clean up script throwing Warning: Wrong parameter count for strstr()

    Hello All,

    I have a small script that is supposed to move all .JPG files in a directory into a folder dated for today. It makes the directory but doesn't move any files and throws this error.

    Warning: Wrong parameter count for strstr() in /home4/cyberpup/public_html/starbannerphoto/livephoto/cleanup.php

    This is a bluehost account. PHP5, Linux/apache environment

    Here's the script
    Code:
    <?php
     
    error_reporting(E_ALL);
    $imgpath="/home4/cyberpup/public_html/starbannerphoto/livephoto";
    $imgext="JPG";
    $newdirname=date("y.m.d");
    $pwd="/home4/cyberpup/public_html/starbannerphoto/livephoto";
     
    if(mkdir("{$pwd}/{$newdirname}", 0755)){
            echo "Directory Created \n";
            if ($handle = opendir($imgpath)) {
                    while (false != ($file = readdir($handle))) {
                            $imagename=strstr($file,'.',true);
                            echo "$imagename\n";
                            if($imagename != ""){
                                    if(copy("{$pwd}/{$imagename}.{$imgext}" ,"{$pwd}/{$newdirname}/{$imagename}.{$imgext}")){
                                            echo "Copy sucessfull\n";
                                            if(unlink("{$pwd}/{$imagename}.{$imgext}")){
                                                    echo "Delete sucessfull";
                                            }else{
                                                    echo "Delete failed: {$pwd}/{$imagename}.{$imgext}";
                                            }
                                    }else{
                                            echo "Copy failed {$pwd}/{$imagename}.{$imgext} to {$pwd}/{$newdirname}\n";
                                    }
                            }
           
                    }
           
            closedir($handle);
    }else{
            echo "Directory creation failed: {$pwd}/{$newdirname}";
    }
     
    }
    ?>
    Thanks you for your help,

    Alan

  • #2
    Τhe optional parameter "before_needle" needs at least php 5.3.0.
    I would use strrpos instead of strstr in order to extract the filename without the extension. The reason is that strstr, with true as 3rd argument, returns the part of the haystack before the first occurrence of the needle.
    eg. strstr("test.foo.jpg", ".", true) returns "test" instead of "test.foo".

    You can use the following function, after adding better error handling.

    PHP Code:
    // This function needs better error handling
    function CopyFiles($from$to$validExtensions = array("jpg"))
    {
            if ([email protected]
    is_readable($from))
                    return 
    false;
            if ([email protected]
    is_readable($to) && !mkdir($to0755))
                    return 
    false;
            if (
    $from == $to)
                    return 
    false;

            if (((
    $c substr($from, -11)) != "/") && $c != "\\")
                    
    $from .= "/";
            if (((
    $c substr($to, -11)) != "/") && $c != "\\")
                    
    $to .= "/";

            if (
    $handle opendir($from))
            {
                    while ((
    $file readdir($handle)) !== false)
                    {
                            
    $pos strrpos($file".");
                            
    $ext $pos !== false substr($file$pos 1) : "";

                            if (
    $file == "." || $file == ".." || !$ext || !in_array(strtolower($ext), $validExtensions))
                                    continue;
                            if (!
    copy($from $file$to $file))
                                    echo 
    "error";

                    }

                    
    closedir($handle);
            }

    Comment


    • #3
      Traded one error for another

      Thank you for taking the time to answer with such a well thought out response. I replaced the code were it looked like it should go (but apparently not) and now I get this error

      Warning: mkdir() [function.mkdir]: No such file or directory in /home4/cyberpup/public_html/starbannerphoto/livephoto/cleanup2.php on line 9
      Directory creation failed: /home4/cyberpup/public_html/starbannerphoto/livephoto/11.08.27

      Here's how the script looks now

      Code:
      <?php
       
      error_reporting(E_ALL);
      $imgpath=" /home4/cyberpup/public_html/starbannerphoto/livephoto";
      $imgext="JPG";
      $newdirname=date("y.m.d");
      $pwd=" /home4/cyberpup/public_html/starbannerphoto/livephoto";
       
      if(mkdir("{$pwd}/{$newdirname}", 0755)){
              echo "Directory Created \n";
              function CopyFiles($from, $to, $validExtensions = array("JPG")) 
      { 
              if ([email protected]_readable($from)) 
                      return false; 
              if ([email protected]_readable($to) && !mkdir($to, 0755)) 
                      return false; 
              if ($from == $to) 
                      return false; 
      
              if ((($c = substr($from, -1, 1)) != "/") && $c != "\\") 
                      $from .= "/"; 
              if ((($c = substr($to, -1, 1)) != "/") && $c != "\\") 
                      $to .= "/"; 
      
              if ($handle = opendir($from)) 
              { 
                      while (($file = readdir($handle)) !== false) 
                      { 
                              $pos = strrpos($file, "."); 
                              $ext = $pos !== false ? substr($file, $pos + 1) : ""; 
      
                              if ($file == "." || $file == ".." || !$ext || !in_array(strtolower($ext), $validExtensions)) 
                                      continue; 
                              if (!copy($from . $file, $to . $file)) 
                                      echo "error"; 
      
                      } 
      
                      closedir($handle); 
              } 
      } 
      }else{
              echo "Directory creation failed: {$pwd}/{$newdirname}";
       
      }
      ?>
      Thanks for your help

      Alan

      Comment


      • #4
        I've never seen that error from a mkdir before. Looks to me like its choking on the preceding space before the / in the file paths. Remove the space and see if that fixes the problem.
        PHP Code:
        header('HTTP/1.1 420 Enhance Your Calm'); 
        Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

        Comment


        • #5
          Directory was created

          Hi Fou-Lou

          Good catch. The directory was created and there were no other errors given but the script still didn't collect any JPEGs and put them in the directory.

          Alan

          Comment


          • #6
            There is no call to your function to actually process the directory.
            PHP Code:
            header('HTTP/1.1 420 Enhance Your Calm'); 
            Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

            Comment


            • #7
              OK, thanks for taking a look. This goes beyond my skill level at this point.

              Alan

              Comment

              Working...
              X