【发布时间】:2019-07-04 04:48:38
【问题描述】:
我正在尝试制作一个表单,将图像上传到域“/uploads”根目录上的文件夹,并将信息上传到数据库(在解决此问题后执行此操作)。但是每次我尝试提交数据时,上传的 PHP 都会打印:
数组 ( ) 文件存在
注意:未定义索引:profileUpload in [my pc directory to host]/data/db-updates/addAccount.php on line 29
注意:未定义索引:profileUpload in [my pc directory to host]/data/db-updates/addAccount.php on line 30
注意:未定义索引:profileUpload in [my pc directory to host]/data/db-updates/addAccount.php on line 31
抱歉您的文件未上传
我目前正在 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(带表格的文件)
<?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(完成上传和其他操作的文件)
<?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.";
}
}
?>
我已经在这里搜索过堆栈溢出问题,我发现的唯一解决方案是包含enctype="multipart/form-data",但我已经找到了它,它一直向我显示错误。
[编辑 1:向输入添加名称]
【问题讨论】:
-
你的
<input>元素需要name属性,所以你可以在php中引用它们:<input type="text" name="somename"/> -
它仍然给我同样的错误@user3647971
-
stackoverflow.com/questions/3586919/… 看看这个,我确定有你的解决方案 :) 干杯
-
我已经检查过了,我的 php.ini 符合您链接的帖子上的设置,其余的,如我所见,也遵循那里的所有内容...@user3647971