图像无法上传/移动到文件夹中

时间:2015-02-14 15:06:57

标签: php html mysqli

我正在上传图片,图片标题正在添加到数据库但文件(图片没有上传/移动到文件夹),我收到404错误的图像,我已将该文件夹权限设置为0777并且最大上传量为1024MB

$article_image = $_FILES['image']['name'];
$image_tmp = $_FILES['image']['tmp_name'];


define ('SITE_ROOT', realpath(dirname('_FILE_')));
move_uploaded_file($image_tmp,SITE_ROOT.'/images/$article_image');
$add="insert into articles(article_title,article_date,article_author,category,article_image,article_keywords,article_content) values ('$article_title','$article_date','$article_author','$article_category','$article_image','$article_keywords','$article_content')" ;
if(mysqli_query($conn,$add)== 1 ){
echo "<script> alert('article added')</script>";
}
else{
echo "failed".mysqli_error($conn) ;
}
}
我在做什么错误?

编辑这里是我的HTML代码

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

<table align="center">
      <tr>
                   <td align="center"><h1> ADD ARTICLE</h1></td>
              </tr>
              <tr>
                   <td>Article Title</td>
                       <td><input type="text" name="title"></td>
              <tr>

                   <td>Article Keyword</td>
                       <td><input type="text" name="keywords"></td>
              <tr>
                   <td>Article Image</td>
                       <td><input type="file" name="image"></td>
              </tr>
                           <td>Article Content</td>
                       <td><textarea name="content" cols="90" rows="30"></textarea></td>
              </tr>
              <tr>
                   <td><input type="submit" name="submit" value="submit"></td>
              </tr>
</table>
</form>

2 个答案:

答案 0 :(得分:0)

试试这个,变量连接问题'/images/'.$article_image

move_uploaded_file($image_tmp,SITE_ROOT.'/images/'.$article_image);

而不是

 move_uploaded_file($image_tmp,SITE_ROOT.'/images/$article_image');

答案 1 :(得分:0)

检查您的行:

realpath(dirname('__FILE__'));

__FILE__ 

是一个魔术常量,不应该用单引号或双引号括起来。

如果您要回显该函数调用的结果,您可能会看到与您期望的路径不同的路径。

您还尝试在变量周围使用单引号进行字符串插值,而不是使用double:

SITE_ROOT.'/images/$article_image';

应该是:

SITE_ROOT."/images/$article_image";

示例:

if (!empty($_FILES['image'])) {
  $tmp_file_to_upload = $_FILES['image'];
  if ($_FILES['image']['error'] == UPLOAD_ERR_OK) {
     $uploaded_name = $tmp_file_to_upload['name'];
     $tmp_name = $tmp_file_to_upload['tmp_name'];
     $destination = realpath(dirname(__FILE__))."images/$uploaded_name";
     if (!move_uploaded_file($tmp_name, $destination)) {
        die('Error uploading file.');
     }
  } else {
    die('Error uploading file.');
  }
}