I get an error from file upload I have only a file input i get the error
An error occurred. Please contact the administrator.
my code:
//html your text <input type="file" name="the_file[]" multiple >
//php $currentDirectory = getcwd();
$uploadDirectory = "/uploads/";
$fileExtensionsAllowed = ['image/png, image/jpeg']; // These will be
the only file extensions allowed
$fileName = $_FILES['the_file[]']['name'];
$fileSize = $_FILES['the_file[]']['size'];
$fileTmpName = $_FILES['the_file[]']['tmp_name[]'];
$fileName = $prenume . $nume . $fileName . ".png";
$fileExtension = strtolower(end(explode('.',$fileName)));
$uploadPath = $currentDirectory . $uploadDirectory . $fileName;
if(isset($_POST['submit'])){
//upload fisiere
if ( in_array($fileExtension,$fileExtensionsAllowed)) {
$errors[] = "This file extension is not allowed. Please upload a JPEG or PNG file";
}
if ($fileSize > 400000000000) {
$errors[] = "File exceeds maximum size (4MB)";
}
if (empty($errors)) {
$didUpload = move_uploaded_file($fileTmpName, $uploadPath);
if ($didUpload) {
echo "Upload-ul fisierului " . $fileName . " s-a efectuat cu succes <br>";
} else {
echo "An error occurred. Please contact the administrator.";
}
} else {
foreach ($errors as $error) {
echo $error . "These are the errors" . "\n";
}
}
i expected to get the multiple file upload,if i upload a single file it works,it might be beacuse of the name or someting
CodePudding user response:
Apparently the issue is with this line:
$fileTmpName = $_FILES['the_file[]']['tmp_name[]'];
It should be:
$fileTmpName = $_FILES['the_file']['tmp_name'];
You should also update the file name to:
$fileName = $prenume . $nume . $fileName;
Finally, you should handle the case where the user selects multiple files to upload. You can do this by looping through the $_FILES
array:
foreach ($_FILES['the_file']['name'] as $i => $name) {
if (strlen($_FILES['the_file']['name'][$i]) > 1) {
if (move_uploaded_file($_FILES['the_file']['tmp_name'][$i], $uploadDirectory.$name)) {
echo 'File was uploaded successfully';
}
}
}
CodePudding user response:
The final code:
$currentDirectory = getcwd();
$uploadDirectory = "/uploads/";
$errors = []; // Store errors here
$fileExtensionsAllowed = ['image/png, image/jpeg']; // These will be the only file extensions allowed
$fileName = $_FILES['the_file']['name'];
$fileSize = $_FILES['the_file']['size'];
$fileTmpName = $_FILES['the_file']['tmp_name'];
$fileExtension = strtolower(end(explode('.',$fileName)));
$fileName = $prenume . $nume . $fileName;
$uploadPath = $currentDirectory . $uploadDirectory . $fileName;
//
if ( in_array($fileExtension,$fileExtensionsAllowed)) {
$errors[] = "This file extension is not allowed. Please upload a JPEG or PNG file";
}
if ($fileSize > 400000000000) {
$errors[] = "File exceeds maximum size (4MB)";
}
foreach ($_FILES['the_file']['name'] as $i => $name) {
if (strlen($_FILES['the_file']['name'][$i]) > 1) {
if (move_uploaded_file($_FILES['the_file']['tmp_name'][$i], $uploadPath. $name .$filename)) {
echo 'File was uploaded successfully';
}
}
}