I'm trying to do a form to upload a image to a folder on the root directory of the domain '/uploads' and the information to a database (doing that after solving this problem). But everytime I try to submit the data, the upload PHP prints this:
Array ( ) files exist
Notice: Undefined index: profileUpload in [my pc directory to host]/data/db-updates/addAccount.php on line 29
Notice: Undefined index: profileUpload in [my pc directory to host]/data/db-updates/addAccount.php on line 30
Notice: Undefined index: profileUpload in [my pc directory to host]/data/db-updates/addAccount.php on line 31
Sorry your file was not uploaded
I'm currently developing the website that contains this page on MAMP (mac).
.htaccess
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
Header set Access-Control-Allow-Origin "*"
AddType application/vnd.ms-fontobject .eot
AddType font/ttf .ttf
admin-addAccount.php (file with form)
<?php
//ini_set("display_errors","on");
//error_reporting(E_ALL);
?>
<link rel="stylesheet" type="text/css" href="/data/css/admin-addAccount.css">
<link rel="stylesheet" media="screen" href="https://fontlibrary.org/face/beon" type="text/css"/>
<section class="content">
<div class="addAccount_holder">
<div class="addAccount_holder_title">Adicionar Conta</div>
<div class="addAccount_holder_content">
<form action="/data/db-updates/addAccount.php" enctype="multipart/form-data" method="post">
<input type="file" name="profileUpload" id="profileUpload" class="profileUpload" />
<label for="profileUpload"><i class="fas fa-upload"></i> Foto de Perfil</label><br />
<span>Nome</span><input name="nome" type="text" required/><br />
<span>Email</span><input name="email" type="email" required/><br />
<span>URL</span><input name="url" type="text" required/><br />
<span>Instagram</span><input name="instagram" type="text" /><br />
<span>Facebook</span><input name="facebook" type="text" /><br />
<span>Twitter</span><input name="twitter" type="text" /><br />
<span>Telemóvel</span><input name="telm" type="text" /><br />
<input type="submit" name="submit" value="Registar">
</form>
</div>
</div>
</section>
addAccount.php (file that does the upload and the rest)
<?php
ini_set("display_errors","on");
error_reporting(E_ALL);
if (TRUE) {
echo '<pre>';
print_r($_FILES);
echo '<pre>';
}
$ok = TRUE;
// Check if there is an uploaded file.
if (!array_key_exists('profileUpload', $_FILES) && !empty($_FILES['profileUpload'])) {
echo "Sorry, no file seems to be uploaded.";
$ok = FALSE;
} else {
echo "files exist";
}
$charactersId = '0123456789abcdefghijklmnopqrstuvwxyz';
$charactersLength = strlen($charactersId);
$userid = '';
for ($i = 0; $i < 8; $i++) { $userid .= $charactersId[rand(0, $charactersLength - 1)]; }
$target = "/uploads/";
$target = $target . 'usrProfile_'.$userid.$_FILES['profileUpload']['type'] ;
$uploaded_type = $_FILES['profileUpload']['type'];
$uploaded_size = $_FILES['profileUpload']['size'];
// This is our size condition.
if ($uploaded_size > 350000) {
echo "Your file is too large.<br>";
$ok = FALSE;
}
//This is our limit file type condition
if ($uploaded_type =="text/php") {
echo "No PHP files<br>";
$ok = FALSE;
}
// Here we check that $ok was not set to 0 by an error
if ($ok) {
echo "Sorry your file was not uploaded";
}
// If everything is ok we try to upload it
else {
if (move_uploaded_file($_FILES['profileUpload']['tmp_name'], $target)) {
echo "The file ". basename( $_FILES['profileUpload']['name']). " has been uploaded";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
}
?>
I've already searched here on stack overflow and the only solution that i found was including enctype="multipart/form-data"
but I've already had that and it keeps showing me the errors.
[edit 1: added names to inputs]