2 qq 34599655 qq_34599655 于 2016.09.08 21:50 提问

关于PHP图片上传的问题

PHP使用move_uploaded_file返回true,为什么在文件夹里没有图片
$time = time();
$UploadPath = upload;
if(!file_exists($UploadPath)){
mkdir(upload);

        }
        $new_img = $time.rand(1000,9999).'.'.strtolower($type);
        $stored_path = __ROOT__.'/upload/';
        $res = move_uploaded_file($_FILES['img']['tmp_name'],$stored_path.$new_img);

// var_dump($res);exit();

2个回答

huangzhengdoc
huangzhengdoc   2016.09.11 14:19
已采纳

这个是普通的上传代码
如果你要只上传jpg的可以加入判断
mysql是连接数据库的文件,自己加入,或者不用也可以

 <?php
    include("mysql.php");
    if(!empty($_GET['userid']))
    {
        $userid=$_GET['userid'];
    }
    else
    {
        $userid=0;
?>
    <script language="javascript" type="text/javascript">window.location.href="waitphp.php?ps=你没有登陆!&userid=<?php echo $userid; ?>"; </script>;
<?php
    }
?>
<?php
    if(!empty($_POST['sub']))
    {
        echo "文件名: ",$_FILES["file"]["name"],"<br>";
        echo "大小:",($_FILES["file"]["size"]/1024),"KB<br>";
        if(file_exists("uploads/".$_FILES["file"]["name"]))
        {
            echo $_FILES["file"]["name"],"已存在!","<br>";
        }
        else
        {
            $name=$_FILES["file"]["name"];
            $big=$_FILES["file"]["size"]/1024;
            move_uploaded_file($_FILES["file"]["tmp_name"],"uploads/".$_FILES["file"]["name"]);
            $sql="INSERT INTO `file`(`id`, `user`, `name`, `big`) VALUES (null,'$userid','$name','$big KB')";
            mysql_query($sql);
?>
            <script language="javascript" type="text/javascript">window.location.href="waitphp.php?ps=上传成功!&userid=<?php echo $userid; ?>"; </script>;
<?php
        }
    }
?>
<form action="up.php?userid=<?php echo $userid; ?>" method="post" enctype="multipart/form-data">
    文件:<input type="file" name="file" id="file" /><br>
    <input type="submit" name="sub" value="提交" />
</form>
devmiao
devmiao   Ds   Rxr 2016.09.09 23:46
Csdn user default icon
上传中...
上传图片
插入图片