tags:

views:

448

answers:

4

Anyone know why this:

<?PHP
$title = trim($_POST['title']);
$description = trim($_POST['description']);

// Array of allowed image file formats 
$allowedExtensions = array('jpeg', 'jpg', 'jfif', 'png', 'gif', 'bmp');

foreach ($_FILES as $file) {
  if ($file['tmp_name'] > '') {
    if (!in_array(end(explode(".",
            strtolower($file['name']))),
            $allowedExtensions)) {
      echo '<div class="error">Invalid file type.</div>';
    }
  }
}

if (strlen($title) < 3)
  echo '<div class="error">Too short title</div>';
else if (strlen($description) > 70)
  echo '<div class="error">Too long desccription.</div>';

else {
  move_uploaded_file($_FILES['userfile']['tmp_name'], 'c:\wamp\www\uploads\images/');
}

Gives:

Warning: move_uploaded_file() [function.move-uploaded-file]: The second argument to copy() function cannot be a directory in C:\wamp\www\upload.php on line 41
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\wamp\tmp\php1AB.tmp' to 'c:\wamp\www\uploads\images/' in C:\wamp\www\upload.php on line 41
A: 

it's because your moving a file and it thinks your trying to rename that file to the second parameter (in this case a director).

it should be:

move_uploaded_file($_FILES['userfile']['tmp_name'], 'c:/wamp/www/uploads/images/'.$file['name']);
Samuel
this will produce a parse error
Col. Shrapnel
use forward slashes instead of backslashes
Samuel
+3  A: 

You are specifying to move a file to a directory; neither PHP's move_uploaded_file nor its copy is as smart as a shell's copy -- you have to specify a filename, not a directory, for the destination.

So, one simple solution would be to take the basename of the source file and append that to the destination directory.

Mark Rushakoff
A: 

It sounds like the second argument to move_uploaded_file should be a full file name instead of just the directory name. Also, probably only a style issue, but you should use consistent slashes in 'c:\wamp\www\uploads\images/'

sblom
A: 

Because PHP is not a shell. You're trying to copy the file into the c:\wamp\www\uploads\images directory, but PHP doesn't know you mean that when you execute (within the move_uploaded_file function):

copy($_FILES['userfile']['tmp_name'], 'c:\wamp\www\uploads\images/');

This command tells it to rename the file to c:\wamp\www\uploads\images/, which it can't do because that's the name of an existing directory.

Instead, do this:

  move_uploaded_file($_FILES['userfile']['tmp_name'], 
    'c:\wamp\www\uploads\images/' . basename($_FILES['userfile']['tmp_name']));
Tyler McHenry