0

I have this piece of code:

  for($d = 0 ; $d < count($_FILES['uploadedfile']['name']) ; ++$d)
  { $imagenamecount = $d + 1;
    $imagedetails = pathinfo( $_FILES['uploadedfile']['name'][$d]);
    $finalpathfortempimagefile = $pathfortempimagefile."image"."$imagenamecount".".".$imagedetails['extension'];
    if(!move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$d], $finalpathfortempimagefile))
    { 
      header('Location: http://localhost/stuff.php?ue=xx');
    }
  }

The main part of it revolves around using move_uploaded_file() to rename and transfer multiple uploaded images. I had no problems with it on Chrome, Firefox, and Opera. But when I tested it on Safari header('Location: http://localhost/stuff.php?ue=xx'); was activated. (Haven't tested it on IE yet, its not opening.)

I have codes before the code provided above that deals with the uploaded files (checks for extensions and etc.) but it had no problems, no errors or whatever. Safari also had no problems with the file upload form:

<form enctype="multipart/form-data" action="post.php" method="POST">
<input id="uploadInput" name="uploadedfile[]" type="file" multiple>
<input class="submit_css" type="submit" value="PREVIEW POST">
</form>

So I think it really has something to do with the move_uploaded_file() function. Any ideas?

ADDITIONAL:

Tested it again with Safari but this time with only 1 image, test was successful no errors.

Tested it again but with multiple uploads (2 and 5), test was unsuccessful it redirected.

Tried both tests on the other browsers, test was successful.

Haven't tried it on IE yet still having trouble opening it.


EDIT

For the benefit of any future viewers of this post.

This is not a php problem of the move_uploaded_file() - I have just recently confirmed that it this was actually a SAFARI v5.1.+ BUG on the html5 multiple attribute. Few info here

Community
  • 1
  • 1
Jo E.
  • 7,822
  • 14
  • 58
  • 94
  • 2
    The php code is executed server side, I don't think this has anything to do with the browser. Do you see any error in the logs? – romainberger Mar 28 '13 at 17:29
  • @romainberger Yes, but it just happens with safari. I tested it just now but with just 1 image and it didn't have any problems, it worked, but when I tested it again with multiple images it redirected. Everything is running smoothly for the other browsers. (still no idea for IE) Error log is saying no value. – Jo E. Mar 28 '13 at 17:40
  • Does your HTML form element have the `enctype` attribute set to `multipart/form-data`? – Populus Mar 28 '13 at 17:45
  • Yes it does. I'll add the code for the form. – Jo E. Mar 28 '13 at 17:47
  • Try dumping all the headers sent from the browser to a log file. Note how the browsers behave. See if you spot anything weird with the Safari headers. – Mr. Llama Mar 28 '13 at 17:48

1 Answers1

1

Does this work?

<?php

$finalpath = $_SERVER['DOCUMENT_ROOT'].'/image/';

$errors = array();

foreach ($_FILES["uploadedfile"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["uploadedfile"]["tmp_name"][$key];
        $name = $_FILES["uploadedfile"]["name"][$key];
        if(!move_uploaded_file($tmp_name, "$finalpath.$name")){
          //header('Location: /stuff.php?ue=xx');
          $errors[]="Problem moving file to $finalpath.$name";
        }
    }
}

if(empty($errors)){
  //success
} else {
  print_r($errors);
}
?>
AbsoluteƵERØ
  • 7,816
  • 2
  • 24
  • 35