PHP图片上传

时间:2014-10-20 07:10:04

标签: php mysql

我尝试将图片上传到文件夹并将路径保存在表格中。我试图上传jpg图片,但它显示错误:

  

不允许您尝试上传的文件。

  

未定义的索引:第21行的C:\ wamp \ www \ web \ db_add_page.php中的用户文件。

这是我的代码:

add_page.php

<form action="db_sql/db_add_page.php" method="post">
    <input type="text" size="100" name="tittle" required />
    <input type="file" name="userfile"/>
    <input type="submit" value="Submit">
</form>

db_add_page.php

$tittle = $_POST['tittle'];
$allowed_filetypes = array('.jpg','.jpeg','.png','.gif');
$max_filesize = 10485760;
$upload_path = 'uploads/';

$filename = $_FILES['userfile']['name'];
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);

if (!in_array($ext,$allowed_filetypes))
  die('The file you attempted to upload is not allowed.');

if (filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
  die('The file you attempted to upload is too large.');

if (!is_writable($upload_path))
  die('You cannot upload to the specified directory');

if (move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
{
    $acc_status = "INSERT INTO add_services (aid,tittle,name)
    VALUES(NULL,'".$tittle."','".$filename."')";
    db::getInstance()->exec($acc_status);
}

2 个答案:

答案 0 :(得分:2)

&lt; form&gt;中有一个缺少的属性标签。您需要设置 envtype 来获取文件。 试试这个:

<form action="db_sql/db_add_page.php" method="post" enctype="multipart/form-data">

答案 1 :(得分:0)

最后我发现了我的错误

$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);

应该是

$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));

$allowed_filetypes = array('.jpg','.jpeg','.png','.gif');

应该是

$allowed_filetypes = array('jpg','jpeg','png','gif');

感谢您的支持。