PHP中的move_uploaded_file不起作用?

时间:2016-06-02 09:47:53

标签: php html mysql file-upload

我正在积极学习并正在开展CMS项目。我坚持上传图片。

PHP

 if ( $_POST['img'])
 $uploads_dir = '/images';
 $tmp_name = $_FILES["img"]["tmp_name"];
 $name = $_FILES["img"]["name"];
 move_uploaded_file($tmp_name, "$uploads_dir/$name");

HTML

 <img src="images/$image" />

的MySQL

 $sql = "INSERT INTO affordplan VALUES('$title','$name','$bodytext','$created')";
  return mysql_query($sql);

文件的名称已上传到数据库,但文件本身未上传到目标文件夹。

6 个答案:

答案 0 :(得分:0)

在表单和表格之间输入您的html代码,如下所示

<form action="" method="post" enctype="multipart/form-data">
<img src="images/$image" name="img" />
...
</form>

答案 1 :(得分:0)

使用$_FILES检查文件的发布与否,而不是$_POST。同时对变量进行适当的引用。然后为echo php变量使用php标签。  尝试

<强> PHP:

if ( $_FILES['img'])
 $uploads_dir = 'images'; // will be on same location where php file exist.
 $tmp_name = $_FILES["img"]["tmp_name"];
 $name = $_FILES["img"]["name"];
 move_uploaded_file($tmp_name, $uploads_dir.'/'.$name);

<强> HTML:

 <img src="images/<?php echo $image;?>" />

<强> MySQL的:

 $sql = "INSERT INTO affordplan VALUES('$title','$name','$bodytext','$created')";
  return mysql_query($sql);

答案 2 :(得分:0)

//将多个文件上传到文件夹

   $upload_dir = '/images';
   if ( $_FILES['img']){
    foreach ($_FILES["img"]["error"] as $key => $error) {
        if ($error == UPLOAD_ERR_OK) {
            $tmp_name = $_FILES["img"]["tmp_name"][$key];
            $name = $_FILES["img"]["name"][$key];
            move_uploaded_file($tmp_name, "$upload_dir/$name");
        }
    }
   }

// MySQL的 MySQL的:

$sql = "INSERT INTO affordplan VALUES('$title','$name','$bodytext','$created')";
  return mysql_query($sql);

表单提交中使用的HTML

<input type="file" name="img" multiple>

以HTML格式显示图片

$dir = "/images/";
$images = glob($dir."*.jpg");
foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}

答案 3 :(得分:0)

您正在分配一个等同于文件名的目录。试试这个

&#13;
&#13;
<?php 
  if (!file_exist("your main directory/the file to story")) {
    mkdir("your main directory/the file to story", 0777, true);
  }

  // then you start uploading your once the folder is created
?>
&#13;
&#13;
&#13;

此处的过程是,如果文件夹目录不存在,mkdir()函数将创建该文件夹。然后就是你开始将文件移动到创建的文件夹的时间。

答案 4 :(得分:0)

将所有代码放在if语句中:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>${maven-shade-plugin-version}</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>log4j2.xml</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
        </execution>
    </executions>
</plugin>

答案 5 :(得分:0)

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

<input  type="file" name="file" id="file" />	
<input type="submit" value="Upload Image" name="submit">

	

$file=$_FILES['file']['name'];
$dest="uploads/$file";
$src=$_FILES['file']['tmp_name'];
move_uploaded_file($src,$dest);

$result=mysql_query("insert into tablename(dbfieldname) values('$dest')");