0

I am trying to upload a file to my server using the following code:

$feedInstanceID = random_str(32,'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');

$target_dir = "/home/mysite/public_html/content";



 if(!file_exists($target_dir)) {
   mkdir($target_dir, 0777, true);

 }

 if ($resourceType == "image") {

   $my_file_name = $feedInstanceID . ".jpg";

 }

 else {

   $my_file_name = $feedInstanceID . ".mov"; 

 }

 $target_dir = $target_dir . "/" . basename($_FILES["file"][$my_file_name]);

if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_dir)) 
{
 if ($result) {
  $response["error"] = FALSE;
  echo json_encode($response);
}
else {
  $response["error"] = TRUE;
  $response["error_msg"] = "Can't insert!";
  echo json_encode($response);
}

} else {
 $response["error"] = TRUE;
 $response["error_msg"] = $_FILES["file"]["error"];
 echo json_encode($response);
}

and I receive the following error output :

[25-Aug-2016 14:18:59 UTC] PHP Notice:  Undefined index: RsmyqMeswmZZ4eo44X14D6bYEgPiobvV.jpg in /home/mysite/public_html/new_upload.php on line 81
[25-Aug-2016 14:18:59 UTC] PHP Warning:  move_uploaded_file(): The second argument to copy() function cannot be a directory in /home/mysite/public_html/new_upload.php on line 83
[25-Aug-2016 14:18:59 UTC] PHP Warning:  move_uploaded_file(): Unable to move '/tmp/phpKMPply' to '/home/mysite/public_html/content/' in /home/mysite/public_html/new_upload.php on line 83
Alk
  • 5,215
  • 8
  • 47
  • 116
  • If you'd done ANY kind of basic debugging, like `var_dump($_FILES)`, you'd see why `$_FILES['file'][$my_file_name]` is completely and utterly wrong, causing your undefined index error. – Marc B Aug 25 '16 at 14:33
  • What should it be then? – Alk Aug 25 '16 at 14:34
  • do do the var_dump, and see for yourself. We can't tell you, since you haven't provided any of the html would indirectly produce the $_FILES entries. – Marc B Aug 25 '16 at 14:35
  • Try to change the target in: `$target_dir = $target_dir . "/" . $my_file_name;` – Riccardo Aug 25 '16 at 14:37
  • Thanks @Riccardo, that solved it, post as an answer so I can accept :) – Alk Aug 25 '16 at 14:40
  • 1
    @MarcB closed the question because is a duplicate, don't worry mankee. Happy to hear that you have a solution now. – Riccardo Aug 25 '16 at 14:42
  • Really isn't a duplicate as the question he linked does not solve the problem, just provides a high level description of these errors... – Alk Aug 25 '16 at 14:43
  • @MarcB is right about the duplicate, the first error you have is exactly a `Notice: Undefined index`. I gave you only the solution to your case. – Riccardo Aug 25 '16 at 14:45

0 Answers0