473,382 Members | 1,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How to check if a file was sent by a form?

Markus
6,050 Expert 4TB
I'm developing a small image hosting project (not commercial just a project to test my php abilities)

and i want to check that the user has entered a file

the code:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $lengthCheck = $_POST['file'];
  3. if(strlen($lengthCheck) < 1){
  4. setcookie("shortLength", "No File Innput", time()+3600);
  5. header('Location: http://mahcuz.com/upload.php');
  6. }
  7. ?>
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  9.         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  10. <html xmlns="http://www.w3.org/1999/xhtml">
  11. <head>
  12.  
i want to check that the user has input a file and if not redirect back to the previous page. There's a cookie so i can display an error on the previous page.

Even if there is a file entered the page is still redirected..

Any ideas?

Much thanks.
Sep 9 '07 #1
5 2670
Atli
5,058 Expert 4TB
Hi Markus.

Try using the isset() function.

Also, files are not passed as text variables, via the $_POST array. They are sent as a part of the $_FILES array.

So try something like this:
Expand|Select|Wrap|Line Numbers
  1. # Check if the file element is set
  2. if(isset($_FILES['myFile'])) {
  3.   # Check if the upload was successful.
  4.   if($_FILES['myFile']['error'] == 0) {
  5.     # File was uploaded successfully
  6.   }
  7.   else {
  8.     # File was sent but failed to be uploaded!
  9.   }
  10. }
  11. else {
  12.   # File was not sent!
  13. }
  14.  
Sep 9 '07 #2
Atli
5,058 Expert 4TB
I've changed the title of this thread to better describe it's topic.
Using good, descriptive titles that follow the Posting Guidelines will increase your chances of getting you questions answered!

Also, please use [code] tags when posting code. It is barely readable without them!

Moderator
Sep 9 '07 #3
Markus
6,050 Expert 4TB
I've changed the title of this thread to better describe it's topic.
Using good, descriptive titles that follow the Posting Guidelines will increase your chances of getting you questions answered!

Also, please use [code] tags when posting code. It is barely readable without them!

Moderator

Sorry! I am a noob >.<

Noted though, and thanks alot!
Sep 9 '07 #4
Markus
6,050 Expert 4TB
Hi Markus.

Try using the isset() function.

Also, files are not passed as text variables, via the $_POST array. They are sent as a part of the $_FILES array.

So try something like this:
Expand|Select|Wrap|Line Numbers
  1. # Check if the file element is set
  2. if(isset($_FILES['myFile'])) {
  3.   # Check if the upload was successful.
  4.   if($_FILES['myFile']['error'] == 0) {
  5.     # File was uploaded successfully
  6.   }
  7.   else {
  8.     # File was sent but failed to be uploaded!
  9.   }
  10. }
  11. else {
  12.   # File was not sent!
  13. }
  14.  

Ahh but ofcourse! Thanks alot! :D

Ahh thanks so much! :D:D
Sep 9 '07 #5
Atli
5,058 Expert 4TB
Glad I could help :)
Please don't hesitate to post again if you have any more questions or problems we can help with!
Sep 9 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

27
by: mrbog | last post by:
Tell me if my assertion is wrong here: The only way to prevent session hijacking is to NEVER store authentication information (such as name/password) in the session. Well, to never authenticate...
2
by: Dknight | last post by:
Hi, all! I have a problem, how can I check a form data before sending it to server. I have the example below, but first data is sent to server, but after JavaScript checks the data :( I need...
5
by: Neo | last post by:
Hello, I am receiving a file as a binary stream from a C++ Client Application. >From the application, the file is being sent as a "Multipart/form-data". I want to receive this file on the...
1
by: scprosportsman | last post by:
Please help guys, i am trying to set up a database here at work and im fairly new to access in terms of writing functions and queries and stuff. I have 2 different places on my design that will...
25
by: pamelafluente | last post by:
Hi Guys, I have the following HTML code which is doing a GET to a page, say MyUrl.aspx : <body> <form name="form1" method="get" action="MyUrl.aspx" id="form1"> <input type="hidden"...
0
by: karen987 | last post by:
Please could someone help me with this code? The problem appears to be a simple one, but i'm not html or ASP literate and need to modify the below code to add 2 things. First a bit about the...
2
by: Ken Fine | last post by:
I want to add the security question and answer security feature to the ChangePassword control. I am aware that this functionality is built into the PasswordRecovery tool. I have implemented the...
7
by: Vishal | last post by:
Hi, I am writing a CGI to serve files to the caller. I was wondering if there is any way to tell in my CGI if the client browser is still connected. If it is not, i want to execute some special...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.