在php中上传图片 [英] Upload image in php

查看:98
本文介绍了在php中上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在目录中添加图像。该目录是动态生成的。但是,在目录和图像上传图像时出现错误,因为此错误,错误如下:



警告:mkdir():文件存在在第21行C:\wamp\www\test\index.php



我的代码在这里:

 < body> 
< form method =postaction =enctype =multipart / form-data>
< input type =filename =filenameid =filename/>
< input type =submitname =pic/>
< / form>

< / body>
< / html>

<?php
if(isset($ _ POST ['pic'])){
$ comimages = $ _FILES ['filename'] ['tmp_name'];
$ targetpath = mkdir(pageimage / pageid);
$ compath = $ targetpath。/。$ _ FILES ['filename'] ['name'];

$ comFileType = $ _ FILES ['filename'] ['type'];
$ comFileSize = $ _ FILES ['filename'] ['size'];
$ comFileSize = $ comFileSize / 1024;

if($ comFileSize <1000)
{
$ arrFileType = array(image / jpeg,image / png,image / gif,image / BMP);

if(in_array($ comFileType,$ arrFileType))

{

move_uploaded_file($ comimages,$ compath);
}
else
{
echo(无效文件格式);
}
}
else
{
echo(文件大小错误);
}
}
?>


解决方案

线索存在错误。您尝试使用 $ targetpath = mkdir(pageimage / pageid); 创建的目录已经存在...所以您无法重新创建它!



我建议在尝试创建一个快速文件存在检查之前进行检查。有一个函数: file_exists()
$ b

另外, mkdir()返回一个布尔值(成功或失败);而不是文件目录,因此您将无法像预期的那样使用 $ targetpath 变量。



试试这个...

  $ targetpath =pageimage / pageid; 
if(!file_exists($ targetpath)){
mkdir($ targetpath);
}
...


I want to add image in directory. The directory makes dynamically. But there is an error while uploading the image in directory and image can't upload due to this error the error is given below:

Warning: mkdir(): File exists in C:\wamp\www\test\index.php on line 21

My code is here:

 <body>
            <form method="post" action="" enctype="multipart/form-data">
            <input type="file" name="filename" id="filename" />
            <input type="submit" name="pic" />
            </form>

    </body>
    </html>

    <?php
        if(isset($_POST['pic'])){
            $comimages = $_FILES['filename']['tmp_name'];
            $targetpath = mkdir("pageimage/pageid");
                $compath = $targetpath."/".$_FILES['filename']['name'];

                $comFileType=$_FILES['filename']['type'];
                $comFileSize=$_FILES['filename']['size'];
                $comFileSize=$comFileSize/1024;

                if($comFileSize<1000)
                {
                    $arrFileType=array("image/jpeg","image/png","image/gif","image/bmp");

                    if(in_array($comFileType,$arrFileType))

                    {

                        move_uploaded_file($comimages,$compath);
                    }
                    else
                    {
                        echo("invalid file format");    
                    }
                }
                else
                {
                    echo("File Size Error");    
                }
        }
    ?>

解决方案

The clue is in the error. The directory you are trying to create with $targetpath = mkdir("pageimage/pageid"); already exists... so you can't make it again!

I would suggest doing a quick file exists check before trying to make it. There is a function for that: file_exists()

Also, mkdir() returns a boolean (success or fail); not a file directory, so you won't be able to use your $targetpath variable as you expect.

Try this instead...

$targetpath = "pageimage/pageid";
if (!file_exists($targetpath)) {
    mkdir($targetpath);
}
...

这篇关于在php中上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆