Author Topic: [PHP] Checking if file was submitted  (Read 4798 times)

0 Members and 1 Guest are viewing this topic.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
[PHP] Checking if file was submitted
« 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!

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: [PHP] Checking if file was submitted
« Reply #1 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.




Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: [PHP] Checking if file was submitted
« Reply #2 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".

Offline alberthrocks

  • Moderator
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 876
  • Rating: +103/-10
    • View Profile
Re: [PHP] Checking if file was submitted
« Reply #3 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).
Withgusto Networks Founder and Administrator
Main Server Status: http://withg.org/status/
Backup Server Status: Not available
Backup 2/MC Server Status: http://mc.withg.org/status/


Proud member of ClrHome!

Miss my old signature? Here it is!
Spoiler For Signature:
Alternate "New" IRC post notification bot (Newy) down? Go here to reset it! http://withg.org/albert/cpuhero/

Withgusto Networks Founder and Administrator
Main Server Status: http://withg.org/status/
Backup Server Status: Not available
Backup 2/MC Server Status: http://mc.withg.org/status/

Activity remains limited due to busyness from school et al. Sorry! :( Feel free to PM, email, or if you know me well enough, FB me if you have a question/concern. :)

Don't expect me to be online 24/7 until summer. Contact me via FB if you feel it's urgent.


Proud member of ClrHome!

Spoiler For "My Projects! :D":
Projects:

Computer/Web/IRC Projects:
C______c: 0% done (Doing planning and trying to not forget it :P)
A_____m: 40% done (Need to develop a sophisticated process queue, and a pretty web GUI)
AtomBot v3.0: 0% done (Planning stage, may do a litmus test of developer wants in the future)
IdeaFrenzy: 0% done (Planning and trying to not forget it :P)
wxWabbitemu: 40% done (NEED MOAR FEATURES :P)

Calculator Projects:
M__ C_____ (an A____ _____ clone): 0% done (Need to figure out physics and Axe)
C2I: 0% done (planning, checking the demand for it, and dreaming :P)

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: [PHP] Checking if file was submitted
« Reply #4 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'].




Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: [PHP] Checking if file was submitted
« Reply #5 on: September 11, 2011, 04:50:31 pm »
Thanks everybody, I managed to do it, and I understood how it works ;)