Tried your code. I got this error:
Warning: pathinfo() expects parameter 1 to be string, object given
Tried your code. I got this error:
Warning: pathinfo() expects parameter 1 to be string, object given
Did you copy-and-paste without reading? That never works.
There's a typo in what's posted: [font=monospace]$image[/font] in that line should be [font=monospace]$src[/font] as it is in all the other cases where [man]pathinfo[/man] has been mentioned in this thread.
Actually, I did read his comments. It suggested another way to write my code. I don't understand it so yes, I'm going to copy it and see what the result gives me. I'm just trying to learn, please be patient.
sorry! I actually fixed that when I tested before posting, but forgot to correct it in my actual post. So!
//$arr2[] = pathinfo( $image,PATHINFO_BASENAME );
// _should_ be:
$arr2[] = pathinfo( $src,PATHINFO_BASENAME );
did you get it to work?
Yes but now I don't know how to compare the two. $result is an array comparison, correct? And what's in the $result are strings? So, like an array of strings?
This is what I get with $result, which is right.
path/to/images/a1.gif
path/to/images/a2.gif
path/to/images/a5.gif
path/to/images/a6.gif
a1.gif
a2.gif
a3.gif
a4.gif
When I do a check to see if an image exist would I do in_array or string comparison like strcmp? Or would I check to see if a1.gif, for example, is in_array? This is where I'm confused. Are these just strings, array of strings, or arrays?
crump;11011449 wrote:When I do a check to see if an image exist would I do in_array or string comparison like strcmp?
To compare arrays, you use array comparison. To compare strings, you use string comparison. Usually. It all depends on for what reason YOU need to compare them. More on this later.
crump;11011449 wrote:Or would I check to see if a1.gif, for example, is in_array?
If you want to know if it exists in the array, then of course you will have to check if it's in the array. So if you have some images in one array and want to see what images from another array are there, you have to check if they are there... The filenames are strings, so to see if one filename matches another filename, string comparison is used. To see what is present (or absent) from one array in another array where the elements contain these strings, array comparison is performed using string comparison on the individual elements.
Let's say you have two small boxes (in real life). One contains 3 fruits, and the other also contain 3 fruits. How do YOU (not a computer) see if a single fruit is the same as another fruit? How do you check if each box has the same contents as the other box? Now, the computer has to do the exact same thing, but it can only look at one single object at a time and compare it against one other single object at a time (your eyes may take in more things simultaenously).
As for being more specific on determining if one fruit is the same as another fruit, YOU may, just like you do when comparing things using a computer, set up whatever criteria you want to! An apple may be the same as another apple. Or a red apple may be the same as a red strawberry. Or a red apple may not be the same as a green apple. Or a Granny Smith (green apple) may not be the same as an Aromatic Russet (another green apple).
crump;11011449 wrote:Are these just strings, array of strings, or arrays?
These what? An array is an array, no matter what it contains. A string is a string, no matter if it's stored directly in a "normal" variable, in an array or in a constant.
define('CONSTANT_CONTAINTING_THREE_AS_INT', 3);
define('SAME_BUT_STRING', '3');
$array1 = array(1, 'two', CONSTANT_CONTAINTING_THREE_AS_INT, 'four', 5.5);
$array2 = array('alpha', 'bravo', SAME_BUT_STRING);
$array3 = array($array1, $array2);
function get_type($val)
{
if (is_string($val))
return 'string';
if (is_int($val))
return 'integer';
if (is_double($val))
return 'double';
if (is_bool($val))
return 'boolean';
if (is_array($val))
return 'array';
if (is_null($val))
return 'null';
if (is_object($val))
return get_class($val);
if (is_resource($val))
return 'resource';
}
foreach ($array1 as $v)
{
printf("%-8s(%s)%s",
$v, get_type($v), PHP_EOL
);
}
echo PHP_EOL;
foreach ($array2 as $v)
{
printf("%-8s(%s)%s",
$v, get_type($v), PHP_EOL
);
}
echo PHP_EOL;
foreach ($array3 as $v)
{
printf("%-8s(%s)%s",
$v, get_type($v), PHP_EOL
);
}
Do note that in the above case, you have two types for the value 3. 3 is an integer, while '3' is a string. Both echo simply as 3 (without the quotes).
So how are arrays compared? Well, as with the apples, it's up to you. The way array_diff works is to use a string comparison, as per the documentation for [man]array_diff[/man]
Note:
Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same.
Thus, to know if two elements will be considered equal, cast them to strings and see for yourself.
$a1 = array(1, 2, 3.0, 4);
$a2 = array(1, '2', '3', 'four');
# What will array diff yield for array_diff($a1, $a2) and for array_diff($a2, $a1)?
# Well, cast to strings and print
echo 'elements of $a1 cast to string:'.PHP_EOL;
foreach ($a1 as $v)
{
echo (string) $v . "\t";
}
echo PHP_EOL.'elements of $a2 cast to string:'.PHP_EOL;
foreach ($a2 as $v)
{
echo (string) $v . "\t";
}
echo PHP_EOL.PHP_EOL;
$diff = array_diff($a1, $a2);
print_r($diff);
echo PHP_EOL.PHP_EOL;
$diff = array_diff($a2, $a1);
print_r($diff);
Thus, if you store basenames in both arrays, array_diff will work in this case. If you store complete path info, it will not (since the paths differ). You would then have to either strip the path info from each array element before using array_diff, or you'd have to use another comparison method which only compares basename (which means writing it yourself).
crump;11011449 wrote:Yes but now I don't know how to compare the two. $result is an array comparison, correct? And what's in the $result are strings? So, like an array of strings?
This is what I get with $result, which is right.
path/to/images/a1.gif
path/to/images/a2.gif
path/to/images/a5.gif
path/to/images/a6.gif
a1.gif
a2.gif
a3.gif
a4.gifWhen I do a check to see if an image exist would I do in_array or string comparison like strcmp? Or would I check to see if a1.gif, for example, is in_array? This is where I'm confused. Are these just strings, array of strings, or arrays?
the filename list you gave above is the "correct result" (i.e., it is what you want)?
My impression was that that was not what you're looking for.
$dir = "path/to/image/folder/";
$imgs = glob($dir . "*.{jpg,gif,png}", GLOB_BRACE);
foreach($imgs as $img){
// I think we need to change this
// $arr1[] = $img;
// to this
$arr1[] = pathinfo( $img,PATHINFO_BASENAME );
}
which will give you a combined list more like
a1.gif
a2.gif
a5.gif
a6.gif
a1.gif
a2.gif
a3.gif
a4.gif
which, of course, the script (in its present form) will reduce to
a1.gif
a2.gif
a5.gif
a6.gif
a3.gif
a4.gif
right?
to answer your other question, yes, this result set is an array of strings. [man]in_array/man is for checking if a particular value (string, in this case) is present in an array: so, if you have a file named a3.gif and you want to see if the filename already exists:
// $result is from the script above, using the results we just discussed)
if( in_array( 'a3.gif',$result ) ){
/* filename "a3.gif" already exists */
}
traq;11011489 wrote:the filename list you gave above is the "correct result" (i.e., it is what you want)?
My impression was that that was not what you're looking for.
$dir = "path/to/image/folder/"; $imgs = glob($dir . "*.{jpg,gif,png}", GLOB_BRACE); foreach($imgs as $img){
// I think we need to change this // $arr1[] = $img;
// to this $arr1[] = pathinfo( $img,PATHINFO_BASENAME ); }which will give you a combined list more like which, of course, the script (in its present form) will reduce to right?
[/code]
When I echo $img I still get path/to/image/a1.gif instead of just a1.gif. It's not grabbing the basenames.
Wow. Thank you for the thorough explanation. I'll have to reread this a couple of times to digest everything.
check the contents of the $arr1 array (for efficiency, after the loop is done) instead of the variable $img.
the code does not change the contents of $img, nor is it intended to.
Ok, so I had to stop working on this for a bit because it was hurting my head. LOL. The following code compares 4 arrays and spits out image basenames that don't match. Yay!
$diff = array_diff($arr1,$arr2, $arr3, $arr4);
echo "<pre>";
print_r(array_merge($diff));
echo "</pre>";
Now I'm faced with one issue. I want to use unlink() to remove image files that don't match but unlink doesn't work with arrays. The following code is poor attempt at unlink. :-(
$dir = "path/to/image/folder/";
foreach ($diff as $key => $value) {
if (array_key_exists($value, $diff)){
echo "Exist: $value\n";
}else{
$filename = glob($dir . $value);
unlink($filename);
};
};
If $value is a string (the name of a file to be unlinked), [font=monospace]$dir . $value[/font] is also string - exactly what [man]unlink[/man] wants. Passing it through [man]glob[/man] is only going to turn it into an array, which [man]unlink[/man] does not want.
I figured out how to unlink the files from array but I want to move the image files to a new directory instead of just deleting them. I read that you could use rename() to move files across directories but it seems weird to use rename for moving files. Or, should I go through the is_dir, mkdir process? What's the best way to move files from one directory to another?
Ok, I tried something like this. Also tried mkdir but didn't create new directory or move the files. Does anyone know? Thanks!
$diff = array_diff($arr1,$arr2, $arr3, $arr4);
echo "<pre>";
print_r(array_merge($diff));
echo "</pre>";
$img_dir = new RecursiveDirectoryIterator("path/to/image/");
foreach(new RecursiveIteratorIterator($img_dir) as $file) {
if (in_array(basename($file), $diff)) {
$old_dir = path/to/image/' . $img_dir . '/old/'. basename($file);
$new_dir = path/to/image/' . $img_dir . '/new/'. basename($file);
if (copy($old_dir,$new_dir)) {
unlink($old_dir);
};
};
};
Doh! I tried mkdir again and played around with the file path and got it to work! It now creates a new directory and moves desired images to new directory. Also, RecursiveDirectoryIterator is a hard one to digest.
What's strange about renaming a file from [font=monospace]/current/full/name/of/file[/font] to [font=monospace]/new/full/name/of/file[/font]?
I guess I just think of rename as well renaming and not moving files. Also, if I were to look up PHP function to move files I wouldn't think to look up rename. But then again I'm not a very skilled PHP'er. Haha. I ended up using copy and unlink and works ok.
Now I'm facing a problem with mkdir using a wildcard query. Is that even possible? So, I have something like mkdir('path//newdir/', 0700); It doesn't recognize '.' Anyone know?
crump;11011949 wrote:Also, if I were to look up PHP function to move files I wouldn't think to look up rename.
Second result when searching "php move file" is for [man]rename/man. The first result is for [man]copy/man which has the following note:
PHP Manual wrote:If you wish to move a file, use the rename() function
at the top of the page.
crump;11011949 wrote:Now I'm facing a problem with mkdir using a wildcard query. Is that even possible? So, I have something like mkdir('path//newdir/', 0700); It doesn't recognize '.' Anyone know?
What is it you're trying to do? I have to ask, because what you said doesn't make any sense. What would you expect mkdir() to do with a path like 'path/*/newdir' ? Make every single possible directory? So 'path/a/newdir', 'path/b/newdir', ..., 'path/aaaaaaaaaaaaaaa/newdir', 'path/aaaaaaaaaaaaaab/newdir', ..., etc. ad nauseum?
Yes, you're right about rename(). My point is when I think of rename I don't think of moving files, but clearly my assumption is faulty -- that's all. Phew tough crowd in here. :-) Now, for the mkdir question...
This is the scenario:
upload zipped folder
unzip folder, which looks like this:
-> main folder
-> images folder
-> css folder
-> js folder
-> new folder (I want to create this folder)
->html files
So, if I want to create another folder inside main folder same level as the images/css/js/ folders I would hard code the main folder's name something like this (it works for me when I hard-code in the main folder's name):
mkdir('/main folder/new folder', 0700);
Every time I upload a zipped folder I want to create a new directory inside it. Since it's hard coded next time I upload a zipped folder with a different name it won't create that "new folder" directory for me. So, I thought I could use the wildcard "*" query in place of "main folder." I don't know if I'm on the right path but this is what I want to accomplish.
crump;11011953 wrote:Yes, you're right about rename(). My point is when I think of rename I don't think of moving files, but clearly my assumption is faulty -- that's all.
You're just thinking in terms of the GUI - directories aren't literally folders, they're just part of the file address. When you "move" a file from one directory to another, you don't actually move it, you just give it a different name. It stays in the same place on disk (which may or may not be anywhere "near" other files in the same directory).
crump;11011953 wrote:Every time I upload a zipped folder I want to create a new directory inside it. Since it's hard coded next time I upload a zipped folder with a different name it won't create that "new folder" directory for me. So, I thought I could use the wildcard "" query in place of "main folder." I don't know if I'm on the right path but this is what I want to accomplish.
Use a variable. You'll need to pass/determine the folder name somehow.
<?php
$uploadedfolder = /* name of uploaded folder */
mkdir( "path/$uploadedfolder/new folder",0700 );
// note that using whitespace in file names isn't really a good idea
// - it can cause problems, especially on non-windows systems
// I recommend using an _underscore_ or camelCase instead