0

I'm trying to use the exact same code I use elsewhere on a site that works and it's now stopped working. I don't know if it's because the directories are deeper but I can't figure it out.

For the form element I'm just using this;

<input type="file" id="logo" name="logo" required>

And for the upload code I'm using this;

<?php
$target_dir = "/home/example.com/path/files/logos/";
$target_file = $target_dir . basename($_FILES["logo"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

if  (move_uploaded_file($_FILES["logo"]["tmp_name"], $target_file)) {
        echo "";
    } else {
        echo "";
    }
?>

This exact method works fine with no errors at all elsewhere. I've removed validation because it isn't needed and it's not likely files will have the same names anyway. I just want to be able to upload to the directory. I'm not against appending something like _timestamp to remove the need for validation as all files will be renamed uniquely anyway. I'm storing submitted information in a CSV and emailing a link to the image later on so this isn't needed as that would mean the new file name needs to be grabbed.

I'm facing the errors Notice: Undefined index: logo which makes no sense to me because it's defined within the form so that's what it's grabbing that from and Notice: Trying to access array offset on value of type null in which I still don't understand an hour into searching because no one seems to know.

Anything would be appreciated.

  • Most likely you have the incorrect form `enctype` attribute so the `$_FILES` array is empty. As for "Trying to access array offset on value of type null" it's self-explanatory. You're trying to treat a null value `$_FILES["logo"]` like an array when you do `$_FILES["logo"]["name"]`. It's same as `$foo = null; echo $foo["name"];` – miken32 May 19 '21 at 21:11
  • I'm using `enctype="multipart/form-data"` @miken32. It may be self explanatory but I don't understand what that means in context. – learningtoanimate May 19 '21 at 21:15
  • Go through the answers to the linked duplicate and ensure you are uploading correctly. If you're still having problems, update your question to include some more relevant details such as the form HTML and results of the debugging you have no doubt done before posting (e.g. dumping contents of $_FILES and $_POST) – miken32 May 19 '21 at 21:21
  • I have, they're no help as they're not duplicates given the issue is different. What futher information would be needed? – learningtoanimate May 19 '21 at 21:25

0 Answers0