PHP上传图片->未定义索引

时间:2019-02-10 15:24:22

标签: php forms image upload

我正在尝试做一个将图像上载到域“ / uploads”的根目录上的文件夹,并将信息上载到数据库的表单(解决此问题后再这样做)。但是每次我尝试提交数据时,上传的PHP都会输出以下内容:

  

数组   (   )   文件存在

     

注意:未定义的索引:第29行上[我的PC目录中的profileUpload到主机] /data/db-updates/addAccount.php

     

通知:未定义的索引:在第30行的[我的PC目录中的profileUpload到主机] /data/db-updates/addAccount.php

     

注意:未定义的索引:在第31行的[我的PC目录中的profileUpload到主机] /data/db-updates/addAccount.php

     

对不起,您的文件尚未上传

我目前正在MAMP(mac)上开发包含此页面的网站。

.htaccess

List

admin-addAccount.php(带有表单的文件)

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

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>

我已经在此处搜索了堆栈溢出问题,发现的唯一解决方案是包含<?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."; } } ?> ,但是我已经有了,并且一直在向我显示错误。

[编辑1:在输入中添加名称]

1 个答案:

答案 0 :(得分:0)

找到了解决我问题的方法!删除了表单操作的扩展名“ .php”。

现在我是这样的:

<form action="/data/db-updates/addAccount" enctype="multipart/form-data" method="post">