Omnimaga

General Discussion => Technology and Development => Web Programming and Design => Topic started by: Munchor on September 11, 2011, 03:24:49 pm

Title: [PHP] Checking if file was submitted
Post by: Munchor on September 11, 2011, 03:24:49 pm
Code: [Select]
<form action="upload.php" method="POST" enctype="multipart/form-data">
  <input type="file" name="file" id="file"><br>
  <input type="submit" name="submit" value="Submit">
</form>

I have this form and I would like to know, in the upload.php file if anything was submitted in the type="file".

This tells me nothing was submitted, even when I submitted a file:
Code: [Select]
<?php
  
if (empty($_FILES["file"]["file"]))
  {
    echo 
"No file was submitted, try again.";
  }
?>


This also tells me nothing was submitted, even when I submitted a file:
Code: [Select]
<?php
  
if (empty($_POST["file"])
  {
    echo 
"No file was submitted, try again.";
  }
?>


Thanks in advance!
Title: Re: [PHP] Checking if file was submitted
Post by: Deep Toaster on September 11, 2011, 03:34:11 pm
http://www.php.net/manual/en/function.is-uploaded-file.php

Is that what you're looking for? is_uploaded_file($_FILES['file']['tmp_name']), in your case.
Title: Re: [PHP] Checking if file was submitted
Post by: Munchor on September 11, 2011, 03:39:45 pm
http://www.php.net/manual/en/function.is-uploaded-file.php

Is that what you're looking for? is_uploaded_file($_FILES['file']['tmp_name']), in your case.

Great, that worked. But that doesn't work in all servers does it? I am thinking it may only work in Linux servers, but I might be wrong. Thanks! I am saying this because you said "in your case".
Title: Re: [PHP] Checking if file was submitted
Post by: alberthrocks on September 11, 2011, 04:00:12 pm
Should work for all. "tmp_name" doesn't mean /tmp in *nix, it just refers to where the temp area for the uploaded file may be. ;)
Typically, for Windows the temp dir would be C:\Windows\Temp (but don't try accessing it directly :P).
Title: Re: [PHP] Checking if file was submitted
Post by: Deep Toaster on September 11, 2011, 04:23:28 pm
http://www.php.net/manual/en/function.is-uploaded-file.php

Is that what you're looking for? is_uploaded_file($_FILES['file']['tmp_name']), in your case.
Great, that worked. But that doesn't work in all servers does it? I am thinking it may only work in Linux servers, but I might be wrong. Thanks!
The PHP manual will tell you when it will and won't work with certain servers. I don't see any notes on this page, so I'm guessing it should work.
I am saying this because you said "in your case".
Oh no, all I meant was that since you're uploading files under the name "file," your $_FILES variable would be $_FILES['file'].
Title: Re: [PHP] Checking if file was submitted
Post by: Munchor on September 11, 2011, 04:50:31 pm
Thanks everybody, I managed to do it, and I understood how it works ;)