PHP上传如果ISSET总是说它是? [英] PHP Upload IF ISSET always says it is?

查看:101
本文介绍了PHP上传如果ISSET总是说它是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我的代码有一些奇怪的问题。我正在尝试使用照片上传文本上传表单。现在,我需要检查照片是否已上传或仅插入文字。



简化代码如下所示:

  if(isset($ _FILES ['image'])){
echo'yes';


}其他{
echo'no';
}

表格如下:

 < form action =method =postenctype =multipart / form-dataclass =contact-form> 
< form action =method =postenctype =multipart / form-dataclass =contact-form>


< label for =title>< strong> Title< / strong> (需要)LT /标签>
< input type =textname =titlevalue =required>


< label>< span class =small>< strong>添加照片< / strong>< / span>< / label>
< input type =filename =image/>


< input type =submitnamesubmitvalue =Submit>
< / form>

现在,基本上,我要做的是检查文件是否附加,因为我需要一个备用选项,以相同的形式发布没有照片的帖子;但是,问题在于,如果附加或不附加,它总是说YES。



帮助,任何人。

解决方案

要检查文件是否已上传,如何:

  if (!file_exists($ _ FILES ['image'] ['tmp_name'])||!is_uploaded_file($ _ FILES ['image'] ['tmp_name']))
{
echo'No upload' ;
}
其他
{
//您的文件已上传
}

is_uploaded_file () 是这里的选择,请查看文档。



$ _ FILES 是一个文件数组,第二维中自动为数组第一维中的每个文件提供一个 ['tmp_name'] 键,值是它的临时位置。



显然你需要在临时文件上使用 move_uploaded_file()。 / p>

OK, i have some kind of strange problem with my code. I'm trying to make uploading form with text along photos. Now, I need to check if the photo is uploaded or the text is inserted only.

The simplified code looks like this:

if (isset ($_FILES['image'])){
echo 'yes';


} else {    
echo 'no';      
}

the form looks like:

   <form action=""  method="post" enctype="multipart/form-data" class="contact-form">
   <form action=""  method="post" enctype="multipart/form-data" class="contact-form">


   <label for="title"><strong>Title</strong> (required)</label>
   <input type="text" name="title" value="" required>


   <label><span class="small"><strong>Add a photo</strong></span></label>
   <input type="file" name="image"/>


   <input type="submit" name"submit" value="Submit">    
   </form>

Now, basically, what I'm trying to make is a check if file is attached or not, because I need an alternate option for posting posts without photos, in the same form; but, the problem is that no mather if it is attached or not it always says YES.

Help, anyone.

解决方案

To check if the file has been uploaded, how about:

if (!file_exists($_FILES['image']['tmp_name']) || !is_uploaded_file($_FILES['image']['tmp_name'])) 
{
    echo 'No upload';
}
else
{
    // Your file has been uploaded
}

is_uploaded_file() is the choice here, check the docs out for it.

$_FILES is an array of files, and each file in the first dimension of the array is automatically given a ['tmp_name'] key in the second dimension, with the value being it's temporary location.

Obviously you then need to use move_uploaded_file() on the temporary file.

这篇关于PHP上传如果ISSET总是说它是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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