...

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



ayoungblood
08-27-2011, 06:00 AM
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


<?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

gvre
08-27-2011, 12:26 PM
Τhe optional parameter "before_needle" needs at least php 5.3.0.
I would use strrpos (http://gr.php.net/manual/en/function.strrpos.php)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.


// This function needs better error handling
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);
}
}

ayoungblood
08-27-2011, 03:30 PM
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




<?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

Fou-Lu
08-27-2011, 04:25 PM
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.

ayoungblood
08-27-2011, 05:04 PM
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

Fou-Lu
08-27-2011, 09:43 PM
There is no call to your function to actually process the directory.

ayoungblood
08-27-2011, 09:54 PM
OK, thanks for taking a look. This goes beyond my skill level at this point.

Alan



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum