PHP File Input Validation - php

How can I create a function in PHP that checks if a file input is selected? The following is what I have but its not working.
Thank you!
function mcFileUp($mcFile){
if(empty($_FILES[$mcFile]['name'])){
echo 'empty';
}
else{
echo 'Ok!';
}
}
I figured it out:
function mcFileUp($mcFile){
if(empty($mcFile))
{
echo 'empty';
}
else
{
echo 'Ok!';
}
}
mcFileUp($_FILES['mcFileUpload']['name']);

Here is a possible solution
function mcFileUp($mcFile)
{
if(strlen($_FILES[$mcFile]['name'])==0)
{
echo 'empty';
}
else
{
echo 'Ok!';
}
}

if(isset($_FILES["uploaded_file"]))
{
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0))
{
............
}
else
{
echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
}
}
else
{
echo "Error: No file uploaded";
}

Check THIS page if you want to know props and corns about the superglobal array "$_FILE". Also, you can have a look # THIS tutorial. Hope it helps you...

Related

Use PHP $_GET[ for displaying different content in the same PHP file - is there another way to do it?

Normally I use $_GET to catch the URL parameters and display content dependend on what the parameter is.
<?php
function isCmd($cmd) {
if(isset($_GET["cmd"]))
if($_GET["cmd"] == $cmd)
return TRUE;
return FALSE;
}
if(isCmd("page1")) {
echo "<p>That is page 1</p>";
}
else if(isCmd("page2")) {
echo "<p>That is page 2</p>";
}
else if(isCmd("page3")) {
echo "<p>That is page 3</p>";
}
else {
echo "<p>That is the normal page</p>";
}
The url would look like these
example.com/script.php
example.com/script.php?cmd=page1
example.com/script.php?cmd=page2
example.com/script.php?cmd=page3
And it works as expected, but I see on websites they can do just:
example.com/script.php?page1
example.com/script.php?page2
example.com/script.php?page3
and it would work, without cmd=
How can I do the same in PHP?
I answered it myself after looking into the $_GET supervariable. It was easier than I thought.
function isCmd($cmd) {
if(isset($_GET["$cmd"]))
return TRUE;
return FALSE;
}
if(isCmd("page1")) {
echo "<p>That is page 1</p>";
}
else if(isCmd("page2")) {
echo "<p>That is page 2</p>";
}
else if(isCmd("page3")) {
echo "<p>That is page 3</p>";
}
else {
echo "<p>That is the standard page</p>";
}
Also works with $_GET.
It would work with $_SERVER["QUERY_STRING"] too.
You can use $_SERVER["QUERY_STRING"] which will return the string after the "?" in the URL.

How to store $_FILES superglobal variables in an array and loop through them to find a match in PHP?

I'm running if statements to identify each variable independently. My code is a bit messy and long but it works with no problem. I'd like to see if it is possible to use an array to store them and run a loop to see if there is a match, instead of using if statements.
PHP code...
if (move_uploaded_file($_FILES["index_deslizador_Cfile1"]["tmp_name"], $index_deslC1)) {
echo "<script>window.open('../index.php','_self')</script>";
}
else if (move_uploaded_file($_FILES["index_deslizador_Cfile2"]["tmp_name"], $index_deslC2)) {
echo "<script>window.open('../index.php','_self')</script>";
}
else if (move_uploaded_file($_FILES["index_deslizador_Cfile3"]["tmp_name"], $index_deslC3)) {
echo "<script>window.open('../index.php','_self')</script>";
}
else if (move_uploaded_file($_FILES["index_deslizador_Cfile4"]["tmp_name"], $index_deslC4)) {
echo "<script>window.open('../index.php','_self')</script>";
}
else if (move_uploaded_file($_FILES["index_deslizador_Mfile1"]["tmp_name"], $index_deslM1)) {
echo "<script>window.open('../index.php','_self')</script>";
}
else if (move_uploaded_file($_FILES["index_deslizador_Mfile2"]["tmp_name"], $index_deslM2)) {
echo "<script>window.open('../index.php','_self')</script>";
}
else if (move_uploaded_file($_FILES["index_deslizador_Mfile3"]["tmp_name"], $index_deslM3)) {
echo "<script>window.open('../index.php','_self')</script>";
}
else if (move_uploaded_file($_FILES["index_deslizador_Mfile4"]["tmp_name"], $index_deslM4)) {
echo "<script>window.open('../index.php','_self')</script>";
}
else {
echo "Sorry, there was an error uploading your file.";
}
I'm fairly new to PHP, so I may get scolded for this suggestion :)
If you could get $index_deslC# variables into an array, you might be able to do something like this:
$i = 1
$destination = array($index_deslC...
foreach($_FILES["index_deslizador_Mfile" . $i]["tmp_name"] as $filename) {
isset(move_uploaded_file($filename, $destination[$i]) ?
echo "<script>window.open('../index.php','_self')</script>" : echo '';
$i++;
}

How to know when the $_FILES array is empty

How can I know that the FILES does not contain an image, and adapt my code accordingly?
I want something like:
if(expression_that_check_FILES_array_isnot_empty)
{
do sth;
}
else
{
do sthElse;
}
Thanks Pavel Janicek for his answer in comment.
I used this:
if (!empty($_FILES)){
do sth;
}
else{
do sthElse;
}
Please try :
if($_FILES['file']['name']!="")
{
do sth;
}
else
{
do sthElse;
}
Try this
if(!empty($_FILES['file']['name']))
{
do sth;
}
else
{
do sthElse;
}
Can you try it?
if(!isset($_FILES["file"]["size"]) || ($_FILES["my_file_name"]["size"] == 0) || (!empty($_FILES["file"]["name"]) {
do sth; // Throw Error
} else {
do sthElse; //Upload
}

PHP if statement errmsg

I have this if statment
if(!empty($URL) && ($safe===true)){
//lots of code
}
Is it possible to show different error messages depending on what condition failed?
For example if $URL is empty echo "URL empty";
and if $safe===false echo "GTFO";
Just add this to your code
else if(empty($URL)
{
echo "url empty";
}
else if($safe===false)
echo "Get Out"; // be polite ;)
if (empty($url))
{
echo "URL empty";
}
elseif ($safe === false)
{
echo "GTFO";
}
else
{
//lots of code
}
} else {
if($safe === false){
die("GTFO");
}
if (empty($url)){
echo "URL Empty.";
}
}
Yes; you could make use of an else if statement.
if (!empty($URL) && ($safe===true)) {
//lots of code
} else if (empty($URL)) {
// report that url is empty
} else if ($safe === false) {
// report that safe is false
}
Alternatively, you could just use an else statement to report that the if condition was false.
I propose the following solution. It will allow you to show multiple errors and set each condition only once (instead of having so many conditions and anti-conditions as other solutions proposed).
$errors = array();
if(empty($URL) {
$errors[] = 'URL empty';
}
if($safe !== true) {
$errors[] = 'GTFO';
}
if(empty($errors)) {
//lots of code
} else {
echo '<ul>';
foreach($errors as $error_message) {
echo '<li>' . $error_message . '</li>';
}
echo '</ul>';
}

How to do an if statement on a function in PHP?

I just realized that you can't just use an if statement on a function, for example this doesn't work:
function sayHello()
{
echo "Hello World";
}
if(sayHello())
echo "Function Worked";
else
echo "Function Failed";
I also saw that a function can't be put as the value of a variable. So how can I do an if statement to check if a function has executed properly and display it to the browser?
It's not working since sayHello() doesn't return anything place return true in there or something.
if (sayHello() === FALSE)
echo "Function Failed";
else
echo "Function Worked";
this will work
<?php
$name1 = "bobi";
function hello($name) {
if($name == "bobi"){
echo "Hello Bob";
} else {
echo "Morning";
}
}
hello($name1);
?>
function selam($isim)
{
if ($isim == 'ugur') {
return 'Selam '.$isim.' :)';
}
return 'Sen kimsin kardes?';
}
echo selam('ugur');

Categories