Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
893 views
in Technique[技术] by (71.8m points)

image - PHP Upload IF ISSET always says it is?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...