在php中上传图片

编程入门 行业动态 更新时间:2024-10-11 15:21:14
本文介绍了在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中上传图片

本文发布于:2023-10-10 09:44:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1478321.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上传图片   php

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!