SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Aug 2007
    Location
    Sao Paulo, Brazil
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    While loop and copy function on Windows

    Hello all

    I'm having a problem with the copy() function inside a while loop. I'm running PHP on Windows Vista. PHP is only performing the copy() on the last loop iteration. The code looks like this:

    PHP Code:
    $path 'folder';
    $path2 'folder2';

    $query "SELECT * FROM indice";
    if(
    $result $mysqli->query($query))
        {
        while(
    $row $result->fetch_assoc())
            {
            
    $prodcode$row['prodcode'];
            
    $filename$row['filename'];
            
            
    copy("$path/$filename""$path2/$prodcode'.zip');
            }
        
    $result->close();
        } 
    I'm trying to copy files from folder $path, rename them and put the copied files in $path2. I have 1000+ files but only the last one gets copied and put in the $path2 folder

    I also tried the rename() function and got the same results. PHP only renames the last iteration.

    PHP displays a warning like this for every iteration:
    Warning: copy(folder/085.zip ) [function.copy]: failed to open stream: Invalid argument in C:\htdocs\provas1\lastrename.php on line 27

    Anyone know what could be wrong? It doesn't look like a path problem because the last iteration gets copied/renamed.

    thanks

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since php is telling you the arguments you're giving to the function are invalid, you should look at the arguments. Use var_dump() to inspect the values.
    PHP Code:
    var_dump("$path/$filename");
    var_dump("$path2/$prodcode'.zip'); 

  3. #3
    SitePoint Member
    Join Date
    Aug 2007
    Location
    Sao Paulo, Brazil
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did a var_dump for each iteration and there was nothing wrong with the arguments. And if there was something wrong with the arguments, why would only the last iteration get copied? There was nothing different with the arguments comparing the last iteration with random ones.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    var_dump(getcwd() . "$path/$filename"); 
    var_dump(getcwd() . "$path2/$prodcode'.zip'); 
    Do those paths really exist?
    Pay attention to the length of the string var dump is reporting, making sure it matches your visual count. Certain bytes may not be rendered visually by your web browser.

    The last one being successful could be a coincidence that you have a stray file in the wrong place at the right time.

  5. #5
    SitePoint Member
    Join Date
    Aug 2007
    Location
    Sao Paulo, Brazil
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Quote Originally Posted by crmalibu View Post
    PHP Code:
    var_dump(getcwd() . "$path/$filename"); 
    var_dump(getcwd() . "$path2/$prodcode'.zip'); 
    Do those paths really exist?
    Pay attention to the length of the string var dump is reporting, making sure it matches your visual count. Certain bytes may not be rendered visually by your web browser.

    The last one being successful could be a coincidence that you have a stray file in the wrong place at the right time.
    ah that was it, thanks a lot! There was a whitespace in there; a trim() solved the problem


Bookmarks

Posting Permissions

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