Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 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
    Regular Coder
    Join Date
    May 2011
    Posts
    240
    Thanks
    1
    Thanked 56 Times in 55 Posts
    Τ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 (!@
    is_readable($from))
                    return 
    false;
            if (!@
    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);
            }


  • Users who have thanked gvre for this post:

    ayoungblood (08-27-2011)

  • #3
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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 (!@is_readable($from)) 
                    return false; 
            if (!@is_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

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • #5
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    There is no call to your function to actually process the directory.

  • #7
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    OK, thanks for taking a look. This goes beyond my skill level at this point.

    Alan


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •